i'm trying to pass a numerical argument with function xyz to print specfic lines of filename, but my 'awk' syntax is incorrect.
ie
xyx 3 (prints the 3rd line, separated by ':' of filename)
function xyz() {
arg1=$1
cat filename | awk -F: -v x=$arg1 '{print $x}'
}
any ideas?
Change print $x to print x
won't that only print out the number '3' and not the 3rd field??
I tried it anyway and i am still receiving
awk: syntax error near line 1
awk: bailing out near line 1
You mean the 3rd field?
$ cat filename
field1:field2:field3
$ function xyz() {
> awk -F: -v x="$1" '{print $x}' filename
> }
$ xyz 3
field3
If you want the 3rd line:
$ cat filename
line1
line2
line3
$ awk -F: -v x="3" 'NR==x' filename
line3
P.S. Given the error, it seems that you're using an old, broken awk,
on Solaris: /usr/bin/awk. In that case you can change the syntax like this:
awk -F: '{print $x}' x="$1" filename
But you'd better use nawk (/usr/bin/nawk) or /usr/xpg4/bin/awk.
yes i'm sorry I meant 'field' not line. I will try
$ cat filename
field1:field2:field3
$ function xyz() {
> awk -F: -v x="$1" '{print $x}' filename
> }
$ xyz 3
field3
and
awk -F: '{print $x}' x="$1" filename
when I get a chance
thanks
edit*
the second method works! Thanks!