Re: bash for loop and awk

Trevor Little <[email protected]>
Newsgroups gmane.org.user-groups.jaxlug
Message-ID <CAEDsR8LoTdrRVaduL2GSRjzKHt0NKXLOL3oH+pT67EO+qc0puA@mail.gmail.com>
How about this?

awk '{if(min==""){min=max=$'$fieldnum'}; if($'$fieldnum'>max)
{max=$'$fieldnum'};} END {print max}' ./file.csv

Trevor

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 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
>
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.