请求编写Awk脚本:合并重复列1行并求和指定列,列8替换为列1值
Awk Script to Merge and Sum Duplicate Rows in Pipe-Separated Files
Here's a straightforward Awk script that handles all your requirements, plus some extra flexibility for edge cases:
The Script
#!/usr/bin/awk -f BEGIN { FS = "|" # Split input fields by pipe OFS = "|" # Join output fields with pipe order_idx = 0 # Track order of first key occurrence (optional) } # Process each line of input { key = $1 # If this is the first time we've seen this key if (! (key in col2)) { # Store the order to preserve input sequence (optional) order[order_idx++] = key # Save non-summed columns (take first occurrence values) col2[key] = $2 col4[key] = $4 # Initialize sum columns (convert to numbers with +0 to avoid string issues) sum3[key] = $3 + 0 sum5[key] = $5 + 0 sum6[key] = $6 + 0 sum7[key] = $7 + 0 sum9[key] = $9 + 0 sum10[key] = $10 + 0 } # If key already exists, add to the sums else { sum3[key] += $3 + 0 sum5[key] += $5 + 0 sum6[key] += $6 + 0 sum7[key] += $7 + 0 sum9[key] += $9 + 0 sum10[key] += $10 + 0 } } # After processing all lines, output merged results END { # Loop through keys in the order they first appeared (optional) for (i = 0; i < order_idx; i++) { key = order[i] # Print merged line: key, col2, sum3, col4, sum5, sum6, sum7, key (replaces col8), sum9, sum10 print key, col2[key], sum3[key], col4[key], sum5[key], sum6[key], sum7[key], key, sum9[key], sum10[key] } }
Key Features Explained
- Sum Handling: Automatically sums columns 3,5,6,7,9,10 whenever the first column value repeats. The
+0ensures empty or non-numeric values are treated as 0. - Merge Logic: Keeps the first occurrence of non-summed columns (2 and 4) and combines all duplicate rows into one.
- Column 8 Replacement: The 8th column in output is always set to the value of the first column (the unique key).
- Order Preservation: The optional
orderarray ensures output rows match the order of the first occurrence of each key in the input. Remove this if order doesn't matter.
How to Use
- Save the script as
merge_sums.awk - Make it executable:
chmod +x merge_sums.awk - Run it on your input file:
./merge_sums.awk input.txt > output.txt
Example Test
Input Lines:
IN27201800024099|a|2.01|ad|5|56|6|rr|1|5 IN27201800023963|b|3|4|rt|67|6|61|ty|6 IN27201800024099|x|1.99|yz|10|44|4|xx|2|5
Output Lines:
IN27201800024099|a|4|ad|15|100|10|IN27201800024099|3|10 IN27201800023963|b|3|4|rt|67|6|IN27201800023963|ty|6
内容的提问来源于stack exchange,提问作者as7951
相关产品推荐
相关产品推荐

