Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing ksh variables into awk

Status
Not open for further replies.

MHThomas

Technical User
Nov 25, 2002
26
GB
Hi,
hopefully this is a quick and easy problem; I want to pass a variable into an awk script so I can select the Nth field in a one record file. Basically I need somebody to fix the syntax of the following example, or tell me I'm barking up the wrong tree...

#!/bin/ksh
n=10
cat /tmp/test.file | awk '{print $n}'

Cheers,

MarkT.
 
You're close - but the variable needs to be passed to awk
#!/bin/ksh
n=10
awk -v TMP="$n" '{print $TMP}' /tmp/test.file

No need for cat (UUOC)
HTH

Dickie Bird (:)-)))
 
Thanks for the quick response DB,
Works perfectly (of course).
Thanks for the heads-up on the UUOC, I'm new to scripting and am still a bit clunkie.

Cheers,

Mark T.
 
Believe it or not - the following works as well . .

#!/bin/ksh
n=10
cat silly.in |awk '{print $'"$n"'}'

Enclosing a ksh var in '" "' seems to do the trick. This way you're not forced into using all of awks clumsy syntax.
 
Thanks, it looks stupid, but it works great...

Cheers,

Mark T.
 
Hi
I am using bourne shell and awk embedded in a shell script. I have been trying to use the trick of parameter passing to the awk, but it does not seem to work. Anyone has any idea?

What I did was:
#!/bin/bash
data="some_value"
awk -v var="$data"
'{print $var}'

The output says the {print $var}:command not found
 
Just use :
'{print var}'

The variable is known to awk as var, not $var



Dickie Bird (:)-)))
 
Thanks DB,

I used the same as you suggested i.e; used '{print var}' instead of '{print $var}', but says the same thing.

Any other idea

Sucheta
 
I created this :
#!/bin/bsh
data="some_value"
awk -v var="$data" '{print var}' db2


db2 is an small text file of two lines
result :
some_value
some_value


NB #!/bin/bsh
awk evaluates each line and does what it's told - to print var.
HTH


Dickie Bird (:)-)))
 
Hi,
is the output from awk '{printf var}' always redirected to a file.
In this case when I exactly replicated your code, it says can't open db2 for reading or writing(This file was not there before). When I created a file called as db2, it does not throw any error, but still returns an empty db2 file.

What you say?

Sucheta
 
Nowhere have you mentioned any redirection
To get output to another file do :
#!/bin/bsh
data="some_value"
awk -v var="$data" '{print var}' db2 > db3



Dickie Bird (:)-)))
 
Hi,
I am new to system administration stuff. I just wanted to store some files in the FTP sites of the server. My question is how do I do that, and where the files are actually stored.

Thanks in advance

Sucheta
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top