Nawk One Liner to Count Field Combinations


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nawk One Liner to Count Field Combinations
# 1  
Old 02-26-2013
Question Nawk One Liner to Count Field Combinations

I have text files, fields separated by commas, and there are 115 fields.
I need to use nawk to look at each line, if field $4==10, then count how many times the combination of field $5 and field $11 occur in the file.

I tried the following:
Code:
nawk -F, '{if($4==10){tg[$5]++;cd[$11]++,tgcd[$5 $11]++}}END{for(i in tgcd){print i"\t"tgcd[i]}}' *20130226093*

With the following error:

Code:
nawk: syntax error at source line 1
 context is
         >>> {if($4==10){tg[$5]++;cd[$11]++, <<< 
nawk: illegal statement at source line 1

I am way out of my depth here. Smilie

Last edited by Scrutinizer; 02-26-2013 at 03:28 PM.. Reason: extra code tags
# 2  
Old 02-26-2013
Please post sample input and desired output.
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-26-2013
The comma before tgcd[$5 $11]++ should be a semicolon
# 4  
Old 02-26-2013
I cannot upload a sample text file because it has customer proprietary infomation. Each line has 115 fields ($1 - $115) but fields $5 and $11 would look like so:
Code:
4012 1070397
4012 1070397
4014 1070397
4014 1070397
4014 1070398
4014 1070398
4018 1070398
40184 1070398

And I would like the output to say something along the lines of "4012 1070397" combination happened X times, "4014 1070397" combination happened y times, etc.

---------- Post updated at 01:01 PM ---------- Previous update was at 12:46 PM ----------

I can't believe that was the only problem! Maybe I dont suck as bad as I thought, lol.

Thanks so much!

:-)

Last edited by Franklin52; 02-27-2013 at 03:20 AM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

FYI - gotcha when using a nawk one liner to convert a processes RSS to MB

I used the following one liner (obtained from an old thread on this site) to look for RSS size of a process and convert it to Mb (I am using process nfsmapid as an example): ps -eo rss,args | nawk 'END { print s/1024, "Mb" } /nfsmapid/ { s += $1 }' I found that the RSS Mb displayed was always... (1 Reply)
Discussion started by: thaebich
1 Replies

2. UNIX for Dummies Questions & Answers

Generating all possible combinations of values in field 1 (awk)

Input: A|1 B|2 C|3 D|4 Output: A+B|3 A+C|4 A+D|5 B+C|5 B+D|6 C+D|7 A+B+C|6 A+B+D|7 A+C+D|8 B+C+D|9 A+B+C+D|10 I only managed to get the output for pairs of $1 values (i.e. combination of length 2): (4 Replies)
Discussion started by: beca123456
4 Replies

3. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

4. Shell Programming and Scripting

awk (nawk) field separator

Hi; i have a file and i want to get; - If the last word in line 14 is NOT equal to "Set."; then print 2nd, 3rd, 4th and 5th values of 3rd line. and my code is: nawk 'NR==14 {if ($NF!="Set.") (NR==3{print $2,$3,$4,$5}) }' file.txt but no result?? :confused::(:confused::( (4 Replies)
Discussion started by: gc_sw
4 Replies

5. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

6. UNIX for Dummies Questions & Answers

Count of Field for Non-Empty

Hi Guys, I wanted to count the number of records for a particular field of a file. whose fields are separated by comma"," I fI use this command. cat "filename" cut -sd "," -f13 | wc -l This shows all the lines count including the blank values for the field number 13. I wanted to count... (2 Replies)
Discussion started by: Swapna173
2 Replies

7. Shell Programming and Scripting

nawk-how count the number of occurances of a pattern, when don't know the pattern

I've written a script to count the total size of SAN storage LUNs, and also display the LUN sizes. From server to server, the LUNs sizes differ. What I want to do is count the occurances as they occur and change. These are the LUN sizes: 49.95 49.95 49.95 49.95 49.95 49.95 49.95 49.95... (2 Replies)
Discussion started by: cyber111
2 Replies

8. Shell Programming and Scripting

I need help counting the fields and field separators using Nawk

I need help counting the fields and field separators using Nawk. I have a file that has multiple lines on it and I need to read the file 1 at a time and then count the fields and field separators and then store those numbers in variables. I then need to delete the first 5 fields and the blank... (3 Replies)
Discussion started by: scrappycc
3 Replies

9. Shell Programming and Scripting

append field to file(nawk)

I have two files. File A and File B. File A has two fields separated by comma 352020252365988, 652020100572356 546876543215667, 652065465654686 ... File B has many Fields separate by spaces Date Name 352020252365988 Reference Date2 Name2 546876543215667 Reference I want to... (4 Replies)
Discussion started by: axl
4 Replies
Login or Register to Ask a Question