Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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)??

share|improve this question
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 at 14:48
add comment

5 Answers

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
share|improve this answer
    
Thanks for help me devnull !! –  user60294 Feb 13 at 18:26
add comment

With sed:

sed 'N;N;N;s/\n\(.*\)\n\(.*\n\)/ \2\1 /'
share|improve this answer
    
Thanks a Lot !!! It Works FINE !!!! –  user60294 Feb 13 at 18:26
add comment

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
share|improve this answer
add comment

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
share|improve this answer
add comment

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)
share|improve this answer
    
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 at 15:01
    
@terdon I don't see that anywhere. The code works for the example given. –  Kaz Feb 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 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 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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