awk on multiples files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk on multiples files
# 1  
Old 07-21-2011
awk on multiples files

Ques from newbie

I want to total column X from large number of files, and view totals for each file separately with the filename.

I have tried:

Code:
for i in `ls -1 *.pattern`; do cat "$i" | awk '{SUM += $4} END { printf("%8d\t%8d\n", $i,SUM) }'; done

does not work. appreciate your help

Last edited by radoulov; 07-21-2011 at 12:07 PM.. Reason: Code tags.
# 2  
Old 07-21-2011
Try:
Code:
for i in *.pattern; do awk '{SUM += $4} END { printf("%16s\t%8d\n", FILENAME,SUM) }' $i; done

# 3  
Old 07-21-2011
thanks bartus.

though its giving me counts but filename is 0, like this:

Code:
       0          538934
       0          542708
       0          551563
       0          553847
       0          552746
       0          563863
       0          512551
       0          516235


Last edited by radoulov; 07-21-2011 at 12:17 PM.. Reason: Code tags, please!
# 4  
Old 07-21-2011
Something like this:

Code:
awk 'END {
  print fn, f
  print "total", t  
  }
  FNR == 1 { 
    if ( f ) {
      print fn, f
      f = x
      }
    fn = FILENAME	  
	}
{ 
  f += $4
  t += $4
  }' *pattern

With GNU awk 4:

Code:
awk 'END {
  print "total", t  
  }
ENDFILE {
  print FILENAME, f
  f = x
  }
{ 
  f += $4
  t += $4
  }' *pattern


Last edited by radoulov; 07-21-2011 at 12:32 PM..
# 5  
Old 07-21-2011
Check again the code. I modified it a bit.
# 6  
Old 07-21-2011
wokes well.

tx a ton

both codes work, bartus's modification reminds me of how silly i was being.
Thanks to you, and to radoulov as well

Last edited by analyst; 07-21-2011 at 12:32 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Join, merge, fill NULL the void columns of multiples files like sql "LEFT JOIN" by using awk

Hello, This post is already here but want to do this with another way Merge multiples files with multiples duplicates keys by filling "NULL" the void columns for anothers joinning files file1.csv: 1|abc 1|def 2|ghi 2|jkl 3|mno 3|pqr file2.csv: 1|123|jojo 1|NULL|bibi... (2 Replies)
Discussion started by: yjacknewton
2 Replies

2. UNIX for Dummies Questions & Answers

Separate file into multiples Linux

Hi guys I am newbie in using linux, how can i Separate file into multiples linux. I want the lines with same parent and id numbers print out into same files. For example Gm17 5135289 5136789 . Parent=533;ID=534;Name=Glyma17g07060 - Gm17 5135289 5136789 . ... (2 Replies)
Discussion started by: grace_shen
2 Replies

3. Shell Programming and Scripting

Extracting/condensing text from multiple files to multiples files

Hi Everyone, I'm really new to all this so I'm really hoping someone can help. I have a directory with ~1000 lists from which I want to extract lines from and write to new files. For simplicity lets say they are shopping lists and I want to write out the lines corresponding to apples to a new... (2 Replies)
Discussion started by: born2phase
2 Replies

4. Programming

multiples of 10 in java

Hi Guys, I wonder how can I determine when a given number is a multiple of another one in java. Let's say if I have 27 how can I determine whether is multiple of 5 using java programming. Thanks. (1 Reply)
Discussion started by: arizah
1 Replies

5. Shell Programming and Scripting

join multiples seds

Hi I have this string with 3 seds.. cat /tmp/roletmp|sed "s/$role2del//" | sed "s/,,/,/" |sed "s/^,//" |sed 's/,$//' How can I join these 3 seds in one? regards Israel. (3 Replies)
Discussion started by: iga3725
3 Replies

6. Shell Programming and Scripting

multiples menu in ksh

Hi, IS possible in ksh to make multiples menus? For example: My menu. 1)Option1 1.1)Option1.1 2.3)Option1.2 2)Option2 2.1)Option2.1 . . . x)Exit I've tried with case but no success. Thanks in advance. (3 Replies)
Discussion started by: iga3725
3 Replies

7. Programming

Pointer addresses in multiples of 32 ?

1. Why are the pointers' addresses every 32 ? 2. Am I correct in stating that memset is writing to memory that is not allocated to any of the 3 pointers ? Is it writing to memory in between the pointers ? 3. Are the 3 pointers contiguous in memory ? 4. I only allocated 10 bytes for each pointer.... (5 Replies)
Discussion started by: cyler
5 Replies

8. UNIX for Dummies Questions & Answers

Using cp for copying multiples files

Hi all, I've got this question about using cp for copying multiples files from the same source directory to another directory, considering that my working directory ain't the same of the source directory. Let me give you a simple example what I'm talking about: Suppose the following files... (2 Replies)
Discussion started by: chapeupreto
2 Replies

9. Shell Programming and Scripting

Matching multiples of a single character using sed and awk

Hi, I have a file 'imei_01.txt' having the following contents: $ cat imei_01.txt a123456 bbr22135 yet223 where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456') I am using the following... (5 Replies)
Discussion started by: royalibrahim
5 Replies

10. UNIX for Dummies Questions & Answers

Email multiples attachment files

I have a program the will split a large file into smaller files. It works great. I, however, have problem of email these file out to other people: I may have 1 or more files afer SPLIT datafileaa datafileab datafileac manually: mailx -s "data" email@email.com<datafileaa mailx -s... (5 Replies)
Discussion started by: bobo
5 Replies
Login or Register to Ask a Question