It's Franklin52's code, with a minor tweak, but I'll explain it
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.
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.
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.
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).
All the input has been read, so print the key which was active when we reached the end of the file.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)