Home > bash > paste – Changing columns to rows

paste – Changing columns to rows

Problem:
Converting from columns to rows based on certain criteria of the input data.

From:

USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
USER_ID
12/31/69 19:00:00
12/31/69 19:00:00
...

To:

USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00

Solution:
use paste

user@computer:~$ paste - - -  < infile 
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00
USER_ID	12/31/69 19:00:00	12/31/69 19:00:00

NB:
The assumption is that you will always have two rows after USER_ID. If that is not the case then this will not work.

How it works:
My initial guess after reading the man is that since ‘- means read from standard input, paste gets the first 3 lines ie ‘- ‘- ‘- and puts them all on one row. Repeats the same process for all the rest.

With no FILE, or when
FILE is -, read standard input.

Source:
http://www.unix.com/unix-dummies-questions-answers/192249-append-following-lines-1st-line-every-3-lines.html

Advertisements
Categories: bash
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s