views:

235

answers:

2

Hi I want to use gawk in a for loop. Something like this:

for i in gawk {print $1} | tr '\n' ' '
do something using $i

this isn't working of course. Ideas?

+2  A: 

You are missing backquotes or $(...) to evaluate the awk command.

for i in $(ls -l | gawk '{print $1}' | tr '\n' ' ')
do
       echo $i
done

Although the last tr command is not needed since the output from a $(...) command implicit will have newlines converted to space in this context.

hlovdal
A: 

shell code with awk / gawk example

for i in `echo how now | gawk '/how/ { print $2 }'`; do
    echo ...$i
done

and

$ sh test.sh
...now
$
DigitalRoss