5

I would like to tab my printout:

1 2
3 4
5 6
7 8

to

1 2 5 6
3 4 7 8

How to adjust it using simple command line (awk or sed)??

  • 1
    If an answer solved your problem, please remember to mark it as accepted and/or upvote it instead of posting a thank you comment. That is the way thanks are expressed on SE sites. – terdon Feb 14 '14 at 14:48
6

One way would be to use awk and paste:

paste -sd'\t' <(awk 'NR%2' inputfile) <(awk '!(NR%2)' inputfile)

The idea is to get odd and even lines separately and paste those.

For your input, it'd produce:

1 2 5 6
3 4 7 8
6

With sed:

sed 'N;N;N;s/\n\(.*\)\n\(.*\n\)/ \2\1 /'
  • This is the only answer here that actually fulfills the requirements: format not just one but each two-line sequence (translated, that is four lines) into two columns. The other answers fall short if number of lines is a multiple of 4 (some fail even with OPs input). – don_crissti Jul 18 '15 at 23:34
2

Here is another way with awk:

$ cat file
1 2
3 4
5 6
7 8

$ awk 'NR%2 { even[++x] = even[x] ? even[x] FS $0 : $0 ; next }
            { odd[++y] = odd[y] ? odd[y] FS $0 : $0 }
       END  { printf "%s\n%s\n", even[x], odd[y] }' file
1 2 5 6
3 4 7 8
2

Here's a Perl way:

perl -ne 's/\n/ / if $.%2!=0; print' file 

It simply replaces the newline chartacter (\n) with a space on odd numbered lines. Run on your data, it produces:

$ perl -ne 's/\n/ / if $.%2!=0; print' file 
1 2 3 4
5 6 7 8
0

Solution in the TXR language:

Naive:

@(repeat)
@a @b
@c @d
@e @f
@g @h
@(output)
@a @b @e @f
@c @d @g @h
@(end)
@(end)

Run:

$ txr reshape.txr -
1 2
3 4
5 6
7 8
1 2 5 6
3 4 7 8
A B
C D
E F
G H
A B E F
C D G H

Issue: it consumes data which doesn't have the right "shape". This version falls back on consuming and printing a line if there is no match for the eight-element block shape that is being transformed.

@(repeat)
@  (cases)
@a @b
@c @d
@e @f
@g @h
@    (output)
@a @b @e @f
@c @d @g @h
@    (end)
@  (or)
@line
@    (do (put-line line))
@  (end)
@(end)
$ cat data
foo

a l
m o
s t
!
# $
% ^
* (
@ +

bar
$ txr reshape8.txr data
foo

a l
m o
s t
!
# $ * (
% ^ @ +

bar

Just in case the question actually means that consecutive pairs of lines are made adjacent, the code would be modified like this:

@(repeat)
@  (cases)
@a
@b
@    (output)
@a @b
@    (end)
@  (or)
@line
@    (do (put-line line))
@  (end)
@(end)
  • This does not seem to give the output the OP wants. The question asks for each consecutive set of two lines to be concatenated. As far as I can tell, your script only does this for certain lines. The expected output would be foo a l\nm o s t\n. – terdon Feb 14 '14 at 15:01
  • @terdon I don't see that anywhere. The code works for the example given. – Kaz Feb 14 '14 at 22:44
  • @terdon I added a simplified version which simply joins pairs of lines into a single line. However, I don't see the requirement you're hinting at that blank lines should be deleted (that foo, a blank line and a l become foo a l). – Kaz Feb 14 '14 at 22:55
  • No, I meant that the OP is asking for every second line to be concatenated. Look at the desired output in the OP. Nothing about ignoring blank lines. This looks fine but I don't have a txr interpreter installed and none seems to be available on the debian repositories so I can't check (I added a link to where you can get one). – terdon Feb 15 '14 at 14:06

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.