Awk and foreach Magic



Beginners foreach:

fdufour@neutrino:awk>    foreach file (name*.file)
foreach?     cat $file | awk '{print$1}'                                    ! Will print what is in colomn 1
foreach?     cat $file | awk '{print$1}'  >  file.out                   ! Will print what is in colomn 1 in a new file.out (not convenient, bug if do somehting
                                                                                                        for several files in a row)
foreach?     cat $file | awk '{print$1}'  >>  file.out                 ! Will print what is in colomn 1 in a existing file.out
foreach?     end


If I want to sum number of a given colomn:
foreach?     cat $file | awk '{s += $4} end {print s}'
foreach?     end



If I want to convert a bunch of eps files to png:

fdufour@neutrino:likeli> foreach file (*.eps)
foreach? convert -density 120x120 $file $file:r.png
foreach? end

Awk only:

If I want to print the second and forth work of each line of a file do:
Where the file format is:
       1   31134       1     556     106       
       2   31134       1     590     106    
       3   31134       1     667     106     

awk '{print $2,$4}' pcevent_2_4_031134.log.07 > run-ev.07 &
  
The the output will be
31134 556
31134 590
31134 667

-----------------------------------------------------------
If I have a file like:
      allbutone     31012         2     16887    0    0    0    0    0    1    0    0    0    0
      allbutone     31012         2     23383    0    0    0    0    0    1    0    0    0    0
      allbutone     31012         2     25303    0    0    0    0    0    1    0    0    0    0
      allbutone     31012         3     32690    0    0    0    0    0    1    0    0    0    0
      allbutone     31012         3     38366    0    0    0    0    0    0    0    1    0    0

where the list of 0 and 1 are a flag which cut which cut failed (faile =1) and I want to know how many event
failed cut 6 (which is in word 10) then

awk '{print $10}' file.name | grep 1 | wc

Great isn't it?
Add that in a foreach loop to do it over a big number of files...



In a list like:

 Muboy results general: 129 3  3723.  0.472095281
 Muboy results general: 248 3  1420.  0.522155225
 Muboy results general: 254 1  2812.  0.517079592
 Muboy results general: 317 2  1350.  0.572555542
 Muboy results general: 412 1  2175.  0.610163808
 Muboy results general: 414 1  1116.  0.475387782
 Muboy results general: 594 2  2250.  0.579116642
 Muboy results general: 904 2  3950.  0.394619644
 Muboy results general: 937 2  1400.  0.47980985
 Muboy results general: 944 1  407.  0.547416747
 Muboy results general: 1025 2  1750.  0.457787395
 Muboy results general: 1075 1  2858.  0.4864254


I want to write the event number (word 4) if word 5 is equal to 3.
awk '{ if ($5 == 3) {print  $4;}}' filename



If I just want to add all the number which are in the file (in one column)
awk '{x+=$1} END{print x}' file

(I can just which column I sum by changing $1 to $2 as usual.)