Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grabbing output from grep and awk into a variable...

Status
Not open for further replies.

bondtrails

Technical User
Nov 19, 2002
31
US
Hi everyone,

if the file inFile.txt containts:
0001 GOODBYE a001.txt
0002 HELLO b001.txt
0003 STAY c001.txt

and when I type this on the command line,
grep 'HELLO' inFile.txt | awk '{ print $3 }'

the output is a001.txt

but how can I do this in a script?
I have tried:
#!/bin/sh
A="grep 'HELLO' inFile.txt | awk '{ print \$3 }'"
B="$A"

this errors out. I want to be able to capture the value a001.txt into the variable B

thanks!!

--Bondster!!
 
Your variable "A" is written incorrectly. Try this:

A=$(/usr/bin/grep 'HELLO' inFile.txt|/usr/bin/awk '{print $3}')

or

A=`/usr/bin/grep "HELLO" inFile.txt|/usr/bin/awk '{print $3}'`


` == backticks

One or both of these solutions should solve your problem. When setting a variable to execute a command, you have to inform the shell that you are attempting to do this either with the backticks or the $() otherwise the shell doesn't know what you are attempting to do.
 
Bondster:

What Pmcmicha just described is called command substitution. The backticks solution A=`..` is for traditional Bourne shell, and A=$(..) is for the modern Korn/Bash shells.

Regards,

Ed
 
please if you use awk, let awk do the job!
don't grep|awk, awk CAN grep !!!!
A=`awk '/Hello/{print $3}' filename`
i prefer sed (it's faster) -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Hi everyone,
after some time, I need to revisit this problem.
The suggested solution:
A=`awk'/HELLO/{ print $3 } inFile.txt`
does a fine job of storing the text "b001.txt" in variable A

However, suppose instead of hardcoding "HELLO", I wish to use a variable, say $B followed to store my pattern. The following doesn't work:
A=`awk '/$B/{ print $3 } inFile.txt`

How can I make this work?? Thanks everyone!!

--Bondster!!
 
Hey, Bondster:

If I'm following you correctly, you want to call a shell variable in an awk script. Place two double quotes are the variable should work:

# untested
A=`awk '/""$B""/{ print $3 } inFile.txt`

There's more than one way of using shell variables in awk. You might want to check out this faq:

faq271-1281

Regards,

Ed
 
Ed,
thank you for your prompt response. I tried the double quotes but didn't have much luck. I also tried the link you posted but it just took me back to the tek-tips home page.

Any other suggestions would be greatly appreciated!!

--Bondster!!
P.S. Here is what I am trying to do (with your suggestion):
#! /bin/sh
SRC=$1
PATTERN=$SRC"_"
FILENAME=`awk '/""$PATTERN""/{ print $9 }' ./namfile_out.txt`
echo $FILENAME


 
Okay, after some experimenting I got it to work!
It had to do with a subtle change:
Instead of:
A=`awk '/""$B""/{ print $3 } inFile.txt`

I used:
A=`awk '/'"$B"'/{ print $3 } inFile.txt`

Can someone explain why this works?

Thanks!!

--Bondster!!
 
Bondster:

The FAQ I was trying to direct you to was the "How can I access shell variables in awk" over in the awk forum.

Regards,

Ed
 
as jamisar said, use sed
XX=abba
eval `sed -ne "s/.*$XX [ ]*\(.*\)/a=\1/p"`
echo $a
untested, i am actually not on a unix box
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top