1

I have a 4 line input file and i need to modify the file to combine alternate lines. I want to perform the operation in place.

INPUT:
Tom
Nathan
Jack
Polo

Desired Output:
Tom    Jack
Nathan Polo

One way is to collect odd numbered lines and flip them and cut even numbered lines and combine both files to get the final output. But i am looking for a simpler solution.

5

Given

$ cat INPUT
Tom
Nathan
Jack
Polo

then

$ pr -s -T -2 < INPUT
Tom     Jack
Nathan  Polo

(paginate with single tab spacing between columns, no headers, two columns); or

$ paste -d ' ' - - < INPUT | rs -T
Tom     Jack
Nathan  Polo

(paste then transpose)

  • What package provides the rs command? – Wildcard Jan 12 '18 at 23:19
  • @Wildcard on Ubuntu (so I guess other Debian-based systems) it has its own package called simply rs - my understanding is that it's originally a BSD utility so it may be packaged as such on other platforms? – steeldriver Jan 12 '18 at 23:24
  • Interesting. Yes, it's pre-installed on Mac OS X (which uses BSD tools) but doesn't seem to exist at all for RHEL/CentOS. – Wildcard Jan 12 '18 at 23:34
0

I have used below command to get the same.

I accept its complex one when compared to pr. But this is my try

for i in `sed -n '1~2p' l.txt`;do sed -n "/$i/,+1p" l.txt; done| split -l 2 |paste xaa xab

output

Tom     Jack
Nathan  Polo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.