Operating System - HP-UX
1830231 Members
1704 Online
109999 Solutions
New Discussion

Re: my problems with shell script..awk..

 
SOLVED
Go to solution
amonamon
Regular Advisor

my problems with shell script..awk..

Helloo..
I have directory in which there are 20-30 files..
I would like to sumarise $12 $13 $14 fiels in each file but first line in every file needs to be excluded..

here is what I tried:
dir=/my/files
ls /path/to > f.dat
for file in $(< f.dat);
do
sed -e "1,1d" $dir/file > $dir/file.new

awk '{print $12' file.new >> sum
awk '{print $13' file.novi >> sum
awk '{print $14' file.novi >> sum


done

but this is not working as I would like...

thanks in advance..

7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: my problems with shell script..awk..

Hi:

You can cause 'awk' to skip the first record instead of involving a separate filter like 'sed'.

# awk 'NR>1 {X+=$12};END{print X}' file

...would read 'file' but skip the first record and sum all of field-12 into 'X' printing its summation at the end.

Regards!

...JRF...
amonamon
Regular Advisor

Re: my problems with shell script..awk..

thanks...:) did not know that...
also FS is "|" I forgot to mention...

well..it is not that hard for me to do that for one file..ok it can be done with one awk or as I did for one file with sed and awk...

But what if I have multiple files..in one directory and I need to sum all $12 fields..
Peter Godron
Honored Contributor

Re: my problems with shell script..awk..

Hi,
"But what if I have multiple files..in one directory and I need to sum all $12 fields.."

Redirect the sum of each file into an output file (append mode), then sum that file.

James's solution can be used for this again.
amonamon
Regular Advisor

Re: my problems with shell script..awk..

right...thanks..

i think I figured it..thanks for the clue..:)

for file in $(< f.dat);
do


awk -F "|" 'NR>1 {print $8;}' /path/$file >> loook



done



I guess that is that... ;)

cheers...and many thanks for prompt assist.
Hein van den Heuvel
Honored Contributor

Re: my problems with shell script..awk..

awk will be happy to read all files in one go.
With a minor tweak to JRF's solution that gives:

awk 'NR>1 {X+=$12; Y+=$12; Z+=$14};END{print X "\n" Y "\n" Z}" *


Hein
James R. Ferguson
Acclaimed Contributor

Re: my problems with shell script..awk..

Hi (again):

If you want to sum from multiple files, simply cahnge the NR to FNR. Thus instead of skipping only the first record (recorded as NR>1 you can skip the first record of each *file* by testing FNR>1.

# awk 'FNR>1 {X+=$3};END{print X}' file1 file2 file3 ...

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: my problems with shell script..awk..

Hein wrote>> awk 'NR>1 {X+=$12; Y+=$12; Z+=$14};END{print X "\n" Y "\n" Z}" *


Ooops, I cut & past the wrong line.
Make that:

awk 'FNR>1 {X+=$12; Y+=$12; Z+=$14};END{print X "\n" Y "\n" Z}' *

Hein.