Script to sum values across columns if they have the same row title
Hi all,
I have a large file that looks like this: Code:
0007 10 Code:
7 26 Thank so much! Kevin |
What is your attempt and where are you stuck?
|
awk would be the most straightforward way. it could match the regular expressions and sum the fields that match that regex.
|
Got something that works! Thanks all!
Code:
sed -n '/^0007/p' number_of_positions_cleaned_in_each_file.txt | awk '{ sum+=$2} END {print sum}' |
Quote:
Code:
awk '/^0007/ { sum+=$2} END {print sum}' kmkocot.txt |
I think the OP made a good-faith effort, so here's how I would solve it. We use an array instead of a simple variable.
Code:
gawk 'BEGIN{ FS=OFS="\t" ; PROCINFO["sorted_in"]="@ind_num_asc" } { sum[$1]+=$2 } END{ for ( i in sum ){ print int( i ) , sum[i] } }' infile.txt Code:
BEGIN{ FS=$OFS="\t" ; PROCINFO["sorted_in"]="@ind_num_asc" } http://www.gnu.org/software/gawk/man...y-Sorting.html We could use the asorti function instead, but I find this way to be easier. If you're using a version of awk that doesn't support sorting, then the easiest option is probably to just pipe the output through sort -n -k1 afterwards. Code:
{ sum[$1]+=$2 } Code:
END{ for ( i in sum ){ print int( i ) , sum[i] } } |
How about:
Code:
awk -vidx="0007" '{sum[$1]+=$2}END{print sum[idx]}' file |
Moved: This thread is more suitable in <programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
|
All times are GMT -5. The time now is 02:43 AM. |