Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
awk summing col1 for similar col2
Name: samit04 Date: February 27, 2007 at 02:47:37 Pacific OS: linux CPU/Ram: dual rpocessor 2Gb ram Product: HP workstation xw4100
Comment:
I have the following 2 cols in my file.
2 /url 5 /url1 3 /url 4 /url2 5 /url2
I want to sum up the col1 of simlar col2's and print it as 1 row. So the output of the above should be
Name: James Boothe Date: February 27, 2007 at 16:46:07 Pacific
Reply:
And here's another approach. Rather than control-break logic, it just summarizes into an array. Both approaches have their advantages, depending on your data.
awk '{col2[$2]+=$1} END {for(i in col2) printf "%5d %-8s\n",col2[i],i}' infile | sort
0
Response Number 3
Name: samit04 Date: February 28, 2007 at 01:53:13 Pacific
Summary: Failing that, you could transplant the awk method into a similar pipeline: cat -n file | awk '{k=substr($0,8,8) ; line[k]=$0 ; freq[k]++ } END { for (x in freq) if (freq[x]==1) print line[x]}' | sort ...
Summary: awk provides NF for the number of fields. So it will give you the integer for the number of fields on a line. Use as below: awk -F/ '{print $NF}' <filename> Hope it helps, Rajesh ...