Monday, November 17, 2008
Concatenate lines using awk in bash
Input file:
$ cat rft01.txt
data set 01
unid=ef023; pid=34
data set 03
data set 09
unid=ef028; pid=36
data set 02
unid=ef021; pid=54
Output Required:
concatenate lines in the above file such that the o/p looks like this:
data set 01 unid=ef023; pid=34
data set 03
data set 09 unid=ef028; pid=36
data set 02 unid=ef021; pid=54
Awk solution:
$ awk 'END{print RS}$0=(/^data set/?NR==1?_:RS:FS)$0' ORS= rft01.txt
data set 01 unid=ef023; pid=34
data set 03
data set 09 unid=ef028; pid=36
data set 02 unid=ef021; pid=54
And if you want the o/p like this:
data set 01 unid=ef023; pid=34
data set 09 unid=ef028; pid=36
data set 02 unid=ef021; pid=54
The awk solution would be:
$ awk '/^data set/{s=$0;next}{print s " "$0}' rft01.txt
Related post:
- Merging lines using awk
- Merge previous line using sed
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
1 comment:
sed -e '
$q; N
/\ndata set [0-9][0-9]*$/{P;D;}
s/\n/ /;s/^/\n/;D
' < rft01.txt
will give
data set 01 unid=ef023; pid=34
data set 03
data set 09 unid=ef028; pid=36
data set 02 unid=ef021; pid=54
To get the following output, modify the sed to as shown:
data set 01 unid=ef023; pid=34
data set 09 unid=ef028; pid=36
data set 02 unid=ef021; pid=54
sed -e '
$q; N
/^data set [0-9][0-9]*\ndata set [0-9][0-9]*$/D
/\ndata set [0-9][0-9]*$/{P;D;}
s/\n/ /;s/^/\n/;D
' < rft01.txt
Post a Comment