srmulcahy
srmulcahy

Reputation: 369

Align rows in text file with command line

I have data in a text file where data rows are repeated after several columns and each new block shares the first column of row labels. I'd like to use the command line to get align the rows into a single table.

The data text file looks like:

Values      SampleA     SampleB     SampleC
Value1      1.00        2.00        3.00
Value2      3.00        2.00        1.00
Value3      2.00        1.00        3.00

Values      SampleD     SampleE     SampleF
Value1      1.00        2.00        3.00
Value2      3.00        2.00        1.00
Value3      2.00        1.00        3.00

And I'd like the resulting file to look like:

Values      SampleA     SampleB     SampleC     SampleD     SampleE     SampleF
Value1      1.00        2.00        3.00        1.00        2.00        3.00
Value2      3.00        2.00        1.00        3.00        2.00        1.00
Value3      2.00        1.00        3.00        2.00        1.00        3.00

Upvotes: 0

Views: 115

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

This solution creates lots of temp files, but cleans up after.

# put each paragraph into it's own file:
awk -v RS= '{print > sprintf("%s_%06d", FILENAME, NR)}' data.txt 

# now, join them, and align the columns 
join data.txt_* | column -t | tee data.txt.joined

# and cleanup the temp files
rm data.txt_*

Verify afterwards with: wc -l data.txt data.txt.joined

Upvotes: 1

Related Questions