I have a data file having few columns and there are white space after few data points. I want to convert 1st column into multiple rows (convert columns after whitespace into rows) .
e.g data file A.dat
2 1
11 1
15 2
24 2
3 1
12 1
16 2
25 2
4 1
13 1
17 2
26 2
using following command I am able to convert 1st column into Rows
awk '{print $1}' A.dat | awk 'BEGIN { ORS = " " } { print }'
this is the output of the above command
2 11 15 24 3 12 16 25 4 13 17 26
Requirements
I want my output like this
this is row: 2 11 15 24
this is row: 3 12 16 25
this is row: 4 13 17 26
Is it possible to convert column into rows with "this is row" in front of that using awk or any other way. I don't have much knowledge about other methods.
This may be what you're trying to do:
awk '
NF { row = row " " $1; next }
row != "" { print "this is row:" row; row="" }
END { if (row != "") print "this is row:" row }
' A.dat
This assumes a blank line finishes the row and a non-blank line starts a new row.
Alternatively, using sed
:
sed -e 's/[[:blank:]]*\([^[:blank:]]*\).*/\1/' \
-e '/./{;H;$!d;}' \
-e 'x;/^$/d;y/\n/ /;s/^/this is row:/' \
A.dat