Re: bash for loop and awk

The Donald Cowart <[email protected]>
Newsgroups gmane.org.user-groups.jaxlug
Message-ID <CAFmUSyzOGfMukH2cwpKo3xv9-bbga_N3hY6_Gh0n2zSUbo18Lg@mail.gmail.com>
On Fri, Dec 23, 2011 at 2:25 PM, Marshall Davis <[email protected]> wrote:
> Hey all,
> I'm having some trouble figuring out how to escape variables inside a bash
> for loop used as field names in awk.
> Here is the basic idea: get the max value from each column in a csv file
> with a number of columns less than 100. This is what I have tried so far:
>
> sed -i 's/,/ /g' ./file.csv
> for fieldnum in {1..100}
>     do
>     awk '{if(min==""){min=max=$$fieldnum}; if($$fieldnum>max)
> {max=$$fieldnum};} END {print max}' ./file.csv
>     done
>

I don't think this is going to work like you think it should.

Putting the $fieldnum inside single quotes in the awk string prevents
it from being expanded by the shell.
You can see this by putting:   echo $fieldnum; echo '$fieldnum' and
you will see two different results.

Try this:

for fieldnum in {1..100}; do
awk -v field="$fieldnum"
'{if(max==""){max=$field};if($field>max){max=$field};} END {print
max}' ./file.csv
done

the "-v" argument to awk does the bash to awk variable substitution.

I hope this helps,

--Donald


> I have tried escaping the $ and $fieldnum with backslash, parentheses,
> quotes, I can't use single quotes, as that is what awk uses for it's
> expressions, and every way I have tried that doesn't give a syntax error
> just prints the entire line $fieldnum times. Is there a way to do this, or
> should I look for a tool other than awk? I wouldn't be opposed to using a
> different shell, although I only have experience with bash and a smidgen
> with korn.
> Thanks!
> Marshall



-- 
Donald Cowart
http://www.rdex.net/

---------------------------------------------------------------------
Archive      http://marc.info/?l=jaxlug-list&r=1&w=2
RSS Feed     http://www.mail-archive.com/[email protected]/maillist.xml
Unsubscribe  [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.