Saturday, April 12, 2008

Join multiple lines using AWK


Some lines in output.txt are broken.

$ cat output.txt
a:b:c:1:2:3:2.3:henry
s:d:f:2:1:4:
54:user5
d:q:w:5:6:
3:5.2:alex
y:m:n:3:4:1:5.6:eiam

Output Required:
----------------
a:b:c:1:2:3:2.3:henry
s:d:f:2:1:4:54:user5
d:q:w:5:6:3:5.2:alex
y:m:n:3:4:1:5.6:eiam


$ awk '
BEGIN {
FS=":";
maxFLD=8;
}
{
while (NF < maxFLD || $0 ~ /\:$/ ) {
getline record;
$0 = $0 record
}
print $0
}
' output.txt


** While current record ($0) is less than 8 fields (NF) or is incomplete (ending with :), update the current record by appending the next record.

1 comment:

awkseeker said...

cool you are! this code bring me more easy.
thank jadu

© Jadu Saikia www.UNIXCL.com