Welcome to the most active Linux Forum on the web.
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 11-16-2011, 12:15 PM   #1
LQ Newbie
Registered: Sep 2011
Posts: 27

Rep: Reputation: Disabled
How do I make an AWK script parse a Shell Variable

I am a bit of a newbie with AWK, so woudl appreciate some help please.

I have a bash shell script, which has a variable that contains a string like this;

Field1 Field2 Field3 Field4 Field5

Let's say, the variable is called MYVAR.

I wnat to be able to use AWK to extract Field3 from MYVAR into a new variable.

I tried this;

NEWVAR=`awk '{print $3}' $MYVAR`
This doesn't seem to work, as it complains that it cannot open a file called Field1.

I was hoping this would use $MYVAR as the input stream for AWK, and then process the single line returning the 3rd element.

Looks like I have got something wrong somewhere.

The shell script is a BASH Script.

Can somebody help please?
Old 11-16-2011, 12:24 PM   #2
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,047
Blog Entries: 11

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Just echo the variable into awk; awk reads streams/files, not environments.
Old 11-16-2011, 12:24 PM   #3
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966Reputation: 1966
NEWVAR=$(echo $MYVAR | awk '{print $3}')
Old 11-16-2011, 01:39 PM   #4
LQ Newbie
Registered: Sep 2011
Posts: 27

Original Poster
Rep: Reputation: Disabled

Thanks Tinkster and colucix. Wrks a treat.

Quite agree with your comment Tinkster about enabling people etc.
Old 11-16-2011, 02:16 PM   #5
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954Reputation: 1954
You'd probably be better off splitting that variable up into an array. Then you wouldn't have to futz around with awk or anything to grab the individual pieces.

The best thing is if you can set the data into an array initially. But if not, you can divide the thing up afterwards. If we can assume that there are no spaces inside the fields, then do this:

array=( $myvar )

echo "${array[2]}"
Things get a bit trickier if you need to handle internal whitespace, but you can read the link above for that.

Also see here for much more on doing various string manipulations in bash:

P.S. Even if you do go with an external tool, you should consider using cut instead of awk for simple field extractions.

Last edited by David the H.; 11-16-2011 at 02:17 PM.


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
Help needed for using awk to parse a file to make array for bash script tallmtt Programming 12 04-14-2012 02:16 PM
[SOLVED] shell script -- awk variable substitution r2d2#jedi Linux - General 2 10-07-2010 10:04 PM
Shell variable in awk script rohini_sharma Programming 3 02-22-2010 09:32 AM
ssimple shell script to parse a file ~sed or awk stevie_velvet Programming 7 07-14-2006 04:41 AM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM

All times are GMT -5. The time now is 12:35 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration