If you only want to operate on the file if has less than 100 columns, could
do something similar to this
#!/bin/bash
columns=$(awk 'BEGIN {FS=","} ; END{print NF}' file.csv)
if [ $columns -lt 100 ]
then
for column_number in $(seq 1 $columns)
do
awk 'BEGIN {FS=",";max = 0} {if
($'$column_number'>max) max=$'$column_number'} END {print max}' file.csv
done
else
echo "File contains more than 100 Columns"
fi
--Justin
On Fri, Dec 23, 2011 at 3:26 PM, The Donald Cowart <[email protected]>wrote:
> 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]
>
>