Hi,
I have an issue to combine multiple lines of a file. I have records as below.
Fields are delimited by TAB. Each lines are ending with a new line char (\n)
Input
--------
ABC 123456 abcde 987
890456 7890 xyz
ght gtuv
ABC 5tyin 1234 789
ghty kuio
ABC ghty jind 1234
678 ght
I have to combine these lines based on first field. Whenever ABC comes a first field it should a new line. Delimiter should be retained as TAB.
My output should be
--------------------
ABC 123456 abcde 987 890456 7890 xyz ght gtuv
ABC 5tyin 1234 789 ghty kuio
ABC ghty jind 1234 678 ght
Please help me to resolve this.
Thanks,
Ratheesh
Try:
perl -lp0e 's/^ABC/\x00ABC/gm;s/\n/\t/g;s/\x0/\n/g' input > output
awk 'END {
if (r)
print r
}
/^ABC/ && r {
print r
r = x
}
{
r = r ? r $0 : $0
}' infile
paste -sd'\t' inputfile|sed 's/ ABC/\
ABC/g'
Use the Tab key between "/" and "ABC".
Another one:
sed -e :a -e '$!N;/\nABC/!s/\n//;ta' -e 'P;D' infile
Thanks all for your quick reply. But these codes are not working as I expected.
All are writing the input records to output as it is.
Solution from elixir_sinari prefixed a TAB for each record except 1st.
ABC 123456 abcde 987
890456 7890 xyz
ght gtuv
ABC 5tyin 1234 789
ghty kuio
ABC ghty jind 1234
678 ght
Please help me.
If you have GNU awk:
$ awk -v RS="ABC" '{ $1="ABC " $1 } NF>1' data
ABC 123456 abcde 987 890456 7890 xyz ght gtuv
ABC 5tyin 1234 789 ghty kuio
ABC ghty jind 1234 678 ght
$
1 Like
And if you don't:
awk '{$1=$1}; /^ABC/ { print P; P=$0; next }; { P=P " " $0 }; END { print P }' OFS="\t" data
ABC 123456 abcde 987 890456 7890 xyz ght gtuv
ABC 5tyin 1234 789 ghty kuio
ABC ghty jind 1234 678 ght
$
Thanks Corona688. This gives the desired output for the given input, it insert a blank line as first record.
Also this is not working if ABC comes as field other than first. I need to consider ABC as delimiter only if it comes as first field of the line.
For example
ABC 123 xyz ABC 678
ftc 345
ABC rty gty
567 778
I need output as
ABC 123 xyz ABC 678 ftc 345
ABC rty gty 567 778
but I get the output as
ABC 123 xyz
ABC 678 ftc 345
ABC rty gty 567 778
Please reply.
---------- Post updated at 02:39 PM ---------- Previous update was at 02:30 PM ----------
Sorry Corona688. It worked successfully. I made a mistake in the data.
The exception is output is started with blank line.
Thanks a lot.
RAR:b:
1 Like