JavaNewb
JavaNewb

Reputation: 35

Merge specific lines - awk/sed

I have a text file with several thousand numbers arranged like this:

line 1: #0 #1 #2 #3 #4 #5
line 2: #6 #7 #8 #9 #10 #11
...
line 111: #106 #107 #108 #109 #110
line 112: #111 #112 #113 #114 #115
...

What I would like to do is format my file to look like this:

line 1: #0 #1 #2 #3 #4 #5 #111 #112 #113 #114 #115 ...
line 2: #6 #7 #8 #9 #10 #11 #116 #117 #118 #119 #120 #121 ...
...
line 111: #106 #107 #108 #109 #110 #218 #219 #220 #221 #222 #223...

basically I want to arrange my numbers in a 111x111 matrix (so after the first 111 lines which would stay in place line 112 would be merged with the first line, line 113 with the second etc and this would happen for every 111 lines). Is there any way of doing this with awk/sed?

Any help would be appreciated!

Upvotes: 0

Views: 81

Answers (2)

leekaiinthesky
leekaiinthesky

Reputation: 5593

If you're ok with having temporary files, you can use split then paste. split splits a file into smaller files of n lines each, and paste takes input files and concatenates them vertically.

Choosing an arbitrary prefix tmp for our temporary files:

split -l 111 input.txt tmp
paste tmp* >output.txt
rm tmp*

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203209

Try this, untested since you didn't provide testable sample input/output:

awk -v RS='\\s' '{ORS=(NR%111?"\n":OFS)}1' file

The above uses GNU awk for multi-char RS and \s.

Upvotes: 1

Related Questions