Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-29-2007, 02:14 PM   #1
LQ Newbie
Registered: Oct 2007
Posts: 2

Rep: Reputation: 0
How to parameterize which field awk should print?

I have a script that runs awk and i want to parameterize which field awk prints. For example, contains:

echo one two three | awk ' {print $2} '

I want to be able to pass set the field that awk prints at run time, eg, this would tell awk to print the 3rd field: 3

I can't figure out how to do this.
Old 10-29-2007, 02:23 PM   #2
LQ Guru
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Would this work, maybe?

# $1 is the first argument given; $0 is the script itself

echo one two three | awk ' {print $('$1')} '
Just a wild guess..

EDIT: now that I copypasted it, chmod'ed and ran, it seems it works - for me, at least..

$ ./ 1
$ ./ 2
$ ./ 3
Of course you'll want to test if the user gave an argument at all, or if the argument is a number at all, or if it's a sane number..things like that. Or at least write the program so that it's not doing anything nuts.

Last edited by b0uncer; 10-29-2007 at 02:25 PM.
Old 10-29-2007, 03:49 PM   #3
LQ Newbie
Registered: Oct 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks -- that's exactly what I wanted

P.S. Is there a way to mark the thread as "answered"


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
awk: how to find expression in a certain field? Micro420 Programming 5 08-08-2007 05:59 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM
Multiple Field Seperators in Awk... TheDarktrooper Programming 6 05-06-2004 04:50 AM
My field separator changes when using awk Helene Programming 3 05-01-2004 08:10 AM
Two field seperators in awk?? Astro Programming 2 11-09-2003 10:12 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:05 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration