How to concat lines that have the same key field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to concat lines that have the same key field
# 8  
Old 03-30-2010
Hi Franklin52 & Alister,

We would sort the file by key field first.

Alister,

Could you please explain your code a little bit?

Thanks
# 9  
Old 03-30-2010
It's Franklin52's code, with a minor tweak, but I'll explain it Smilie

Quick primer, in case it's needed. AWK scripts consist of a series of pattern-action pairs. For each input line, awk will evaluate the pattern-action pairs in the order they occur in the script. The action (the code within curly braces) will only execute for a given input line if its pattern expression evalutes to a boolean true value. Either the pattern or the action may be absent, but not both. An absent pattern defaults to true and therefore an action without a pattern will execute for every line read. An absent action defaults to printing the current line, so a pattern without an action will print out the current line, $0, if the pattern evaluates to true.

Now, on to this particular script.

The first field of each input line is treated as a key value. The active key, the key for which values are being collected at any given time is stored in k.

i is a counter that keeps track of how many values we've collected for the active key.

s is a string containing the current key followed by its value (or two values delimited with a comma).

Given the way this code works, it makes sense to start with the last pattern-action pair before the END pattern-action pair.

Code:
{s=$0;k=$1;i=1}

This pattern-action pair is missing it's pattern, so it will match every line by default. This is the first action to execute. When the script first starts, the previous pattern-action pairs do not match because the values of k and s are still unset. This line initializes (and later, whenever there is a key change, reinitializes) s to the current line; the active key, k, to the first field of the line, $1; and the counter, i, that tracks how many values have been collected in s.

Code:
$1==k {if (++i<3) s=s "," $2; next}

If the current line's key ($1) is equal to the active key, k, and if less than three values (including the current line's value) have been seen for this key, then we want to append its value ($2) to the string s. Regardless of how many values have been seen for this key, skip the rest of the pattern-action pairs (the 'next' statement), read in the next line from the input, and continue repeating this first step as long as the new input line's key is equal to the active key.

Code:
s{print s}

If we reached this point in the awk script, it's because the current line's key is not the same as the active key. Time to print the old key and its values. The next step will be to execute the pattern-action pair with which I began this explanation (to reinitialize all of the script's variables).

Code:
END{print s}

All the input has been read, so print the key which was active when we reached the end of the file.

I hope that helped,
Alister

Last edited by alister; 03-30-2010 at 04:06 PM..
# 10  
Old 03-30-2010
Quote:
Originally Posted by alister
Hello, Franklin52:

Actually, that code isn't quite right.
Hi alister,

Your right, I misread the question. Thanks for the fix and explanation. Smilie

Regards
# 11  
Old 03-30-2010
Hi Alister & Franklin52,

Thanks so much to you both for the code and detailed explanation. Appreciate your help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. UNIX for Dummies Questions & Answers

concat any two lines in a file

I have a file with line 4 : F SITE SPA_M2 SPA_M3 SPA_M4 and a line 237 with: BV_N4 VbdGO_PW Rs_NW_STI Rc_N+OD need a awk liner to concat the two lines so that line 2 sits next to line1 and looks like: F SITE SPA_M2 SPA_M3 SPA_M4 BV_N4 VbdGO_PW ... (8 Replies)
Discussion started by: awkaddict
8 Replies

4. Shell Programming and Scripting

awk or sed? change field conditional on key match

Hi. I'd appreciate if I can get some direction in this issue to get me going. Datafile1: -About 4000 records, I have to update field#4 in selected records based on a match in the key field (Field#1). -Field #1 is the key field (servername) . # of Fields may vary # comment server1 bbb ccc... (2 Replies)
Discussion started by: RascalHoudi
2 Replies

5. Shell Programming and Scripting

Split file when the key field change !

Hello, I have the following example data file: Rv.Global_Sk,1077.160523,D,16/09/2011 Rv.Global_Sk,1077.08098,D,17/09/2011 Rv.Global_Sk,1077.001445,D,18/09/2011 Rv.Global_Sk,1072.660733,D,19/09/2011 Rv.Global_Sk,1070.381557,D,20/09/2011 Rv.Global_Sk,1071.971747,D,21/09/2011... (4 Replies)
Discussion started by: csierra
4 Replies

6. Shell Programming and Scripting

awk concat lines between 2 sequent digits

I would like to print string between two sequent digits and concatenate it into one single line. input.txt 99 cord, rope, strand, twine, twist, 100 strand, twine, twist, cord, rope 101 strand, twine, twist, twine, twist, cord, rope 105 cord, rope ,twi ... (8 Replies)
Discussion started by: sdf
8 Replies

7. Shell Programming and Scripting

Using AWK to format output based on key field

I have file which contains gene lines something like this Transcript Name GO POPTR_0016s06290.1 98654 POPTR_2158s00200.1 11324 POPTR_0004s22390.1 12897 POPTR_0001s11490.1 POPTR_0016s13950.1 14532 POPTR_0015s05840.1 13455 POPTR_0013s06470.1 12344... (6 Replies)
Discussion started by: shen
6 Replies

8. Shell Programming and Scripting

Need help in concat of two lines in a file

Hi , Need help in concating two lines based on certain character, for example my file has the messages : :57A:qweqweww :58A:qeqewqeqe -}$ {1:fffff2232323}{2:123123dasds}{4: :20:121323232323232 :21:sdsadasdasddadad if the line ends with "-}$" or if a line starts with "{1:" then it... (5 Replies)
Discussion started by: ulin
5 Replies

9. Shell Programming and Scripting

Conditional concat lines awk

Hello, I have a text file like this: NONE FILE_Rename frompath: /log_audit/AIX/log/current/AIXAFTPP.log NONE FILE_Unlink filename /audit/tempfile.14041142 NONE FILE_Rename ... (8 Replies)
Discussion started by: carloskl
8 Replies

10. UNIX for Dummies Questions & Answers

Please Help:Need to Split the file into mutliple files depends on the KEY field value

Hi Gurus, I am new to UNIX(HP). my requirmnet is File needs to needs to be split into multiple files dependa on one key value. example 1 abc 333 us 2 bbc 444 ch 5 nnn 333 kk 7 hhh 555 ll 3 hhh 333 hh now the requirment is line with 333 should be copied into test1.txt and... (14 Replies)
Discussion started by: arund_01
14 Replies
Login or Register to Ask a Question