The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing argument to a pl/sql block er_ashu Shell Programming and Scripting 2 05-02-2008 08:42 AM
Passing more than one argument in a function jisha Shell Programming and Scripting 1 04-23-2008 03:05 AM
Argument passing using for or while loop jisha Shell Programming and Scripting 4 04-08-2008 09:21 PM
Problem with Argument Passing A_Rod Shell Programming and Scripting 4 09-13-2006 08:47 AM
Passing argument from one script to other lloydnwo Shell Programming and Scripting 3 10-27-2005 11:06 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Nov 2006
Posts: 44
passing argument into awk

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)

Code:
function xyz() {
arg1=$1
cat filename | awk -F: -v x=$arg1 '{print $x}'
}
any ideas?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-01-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,883
Change print $x to print x
Reply With Quote
  #3 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Nov 2006
Posts: 44
Quote:
Originally Posted by jim mcnamara
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
Code:
awk: syntax error near line 1
awk: bailing out near line 1

Last edited by prkfriryce; 02-01-2007 at 12:45 PM.
Reply With Quote
  #4 (permalink)  
Old 02-01-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,564
Quote:
Originally Posted by prkfriryce
won't that only print out the number '3' and not the 3rd line??
[...]
You mean the 3rd field?

Code:
$ cat filename
field1:field2:field3

$ function xyz() {
> awk -F: -v x="$1" '{print $x}' filename
> }

$ xyz 3
field3
If you want the 3rd line:

Code:
$ 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:

Code:
awk -F: '{print $x}' x="$1" filename
But you'd better use nawk (/usr/bin/nawk) or /usr/xpg4/bin/awk.

Last edited by radoulov; 02-01-2007 at 01:25 PM.
Reply With Quote
  #5 (permalink)  
Old 02-01-2007
Registered User
 

Join Date: Nov 2006
Posts: 44
Quote:
Originally Posted by radoulov
You mean the 3rd field?

Code:
$ cat filename
field1:field2:field3

$ function xyz() {
> awk -F: -v x="$1" '{print $x}' filename
> }

$ xyz 3
field3
If you want the 3rd line:

Code:
$ cat filename
line1
line2
line3

$ awk -F: -v x="3" 'NR==x' filename
line3
P.S. Given the error, it seams that you're using an old, broken awk,
on Solaris: /usr/bin/awk. In that case you can change the syntax like this:

Code:
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

Code:
$ cat filename
field1:field2:field3

$ function xyz() {
> awk -F: -v x="$1" '{print $x}' filename
> }

$ xyz 3
field3
and
Code:
awk -F: '{print $x}' x="$1" filename
when I get a chance

thanks

edit*

the second method works! Thanks!

Last edited by prkfriryce; 02-01-2007 at 01:29 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0