LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 11-16-2011, 11:15 AM   #1
Nerdio
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;

Code:
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, 11:24 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,986
Blog Entries: 11

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

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

Original Poster
Rep: Reputation: Disabled
Wink

Thanks Tinkster and colucix. Wrks a treat.

Quite agree with your comment Tinkster about enabling people etc.
 
Old 11-16-2011, 01: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: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
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.

http://mywiki.wooledge.org/BashGuide/Arrays

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:

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

http://mywiki.wooledge.org/BashFAQ/100

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 01:17 PM.
 
  


Reply


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


All times are GMT -5. The time now is 10:33 PM.

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