Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to convert column with whitespace into multiple rows using awk

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

This may be what you’re trying to do:

awk '
    /./       { row = row " " $1; next }
    row != "" { print "this is row:" row; row="" }
    END       { if (row != "") print "this is row:" row }
' A.dat
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading