The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:42 AM
Passing more than one argument in a function jisha Shell Programming and Scripting 1 04-23-2008 06:05 AM
Argument passing using for or while loop jisha Shell Programming and Scripting 4 04-09-2008 12:21 AM
Problem with Argument Passing A_Rod Shell Programming and Scripting 4 09-13-2006 11:47 AM
Passing argument from one script to other lloydnwo Shell Programming and Scripting 3 10-27-2005 02:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-01-2007
prkfriryce prkfriryce is offline
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?
  #2 (permalink)  
Old 02-01-2007
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,753
Change print $x to print x
  #3 (permalink)  
Old 02-01-2007
prkfriryce prkfriryce is offline
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 04:45 PM..
  #4 (permalink)  
Old 02-01-2007
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,859
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 05:25 PM..
  #5 (permalink)  
Old 02-01-2007
prkfriryce prkfriryce is offline
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 05:29 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0