Providing variable contents to awk

Hello, All. This is my first post here, and I expect that the answer is simple, but I can't find it. Might be the way I'm searching. I'm fairly new to Unix/Linux, and I'm writing a Korn Shell Script. I am trying to provide a value that is already in a variable to awk so that awk can pull out the second column.

An example of what I'm trying is here:

 
for g in `cat $outfilepath/$outfile`
do
 
$g | awk -F, '{print $2}'
 
done

The line should be comma-separated, and I'm trying to pull out the second entry, but all this does is print out the contents of $g, which doesn't do me any good.

Once I can pull out the second entry, I plan on using it for some decision making, but that's secondary right now.

Any help would be appreciated.

That is a useless use of cat, to avoid unwanted side-effects and splitting for things in backticks you would generally do

while read LINE
do
        echo "Read $LINE"
done < inputfile

...but in this case I'm not sure why you're using a while loop at all. awk is quite capable of reading more than one line by itself.

Anyway.

When you do "$g" by itself, it assumes that's a command. For instance:

$ G="cat"
$G file

file contents.
...
...

$

So you're not actually printing the line into awk here -- you're running it as a command, and piping its output into awk.

What you're trying to do is more like echo $G | awk ...

...but I think your program would be best rewritten as:

awk -F, '{ print $2 }' filename
1 Like

awk is its own entire programming language, incidentally. If all you wanted to do was to extract the second column for further processing, that further processing could probably be done inside awk too.

Or if you wanted to keep it in shell, you can do this wholly in shell without using awk at all:

while IFS="," read COL1 COL2 COL3
do
        echo "column 2 is ${COL2}"
done < inputfile

IFS is a special variable controlling what the shell does variable splitting on. Its value is only temporarily changed here, while read is being run.

Thanks, Corona. As I said, I'm relatively new to Unix, and I'm still learning what can and can't be done, and the best way to do things. I'm still reading up on awk, and learning how to use it, but as a programming language in itself, it's a lot to take in.

I'm trying your solution now, and I'll reply the status when it's done. The script takes a bit of time to run.

---------- Post updated at 02:30 PM ---------- Previous update was at 12:57 PM ----------

Thanks again, Corona. I reworked my script using just awk, and removing the while loop, and it worked as I'd hoped.

I appreciate the help.

1 Like