Alby
Alby

Reputation: 5742

Convert single column file into multiple columns

Basically, I want to convert one column file into multiple column file specified by number of rows.

I do not want to reinvent the wheel. I want to make sure if there is a unix command / or standard way of doing this before writing a custom script.

For example, let's say I have the following file:

$cat input.txt
tom
jack
kim
bart
foo
bar

I want to turn this into 3 row file

$ cat input.txt | my_script --every=3 --delimiter=tab
tom bart
jack foo
kim bar

or 2 row file with the different delimter:

$ cat input.txt | my_script --every=2 --delimiter=,
tom,kim,foo
jack,bart,bar

Upvotes: 6

Views: 8085

Answers (6)

ssanch
ssanch

Reputation: 425

I tried several of the solutions provided above including paste - - and xargs -n2, but none of these satisfy the OP requirement of having half of the list in one column and the other half in the other column. I did not try the awk solution, as it looked clunky, wordy, and there was not much explanation on what the code is doing.

After some digging, I found this gives a short and elegant solution using pr:

echo "tom
jack
kim
bart
foo
bar" > my_file

cat my_file | pr -2 -t -s

Returns:

tom bart
jack    foo
kim bar

If you want it comma-delimited:

cat my_file | pr -2 -t -s,

Returns:

tom,bart
jack,foo
kim,bar

From the pr manpage:

-s char
      Separate text columns by the single character char instead of by the appropriate number of <space>s (default for char is the <tab> character).

-t    Print neither the five-line identifying header nor the five-line trailer usually supplied for each page.  Quit printing after the last line of each file without
      spacing to the end of the page.

Upvotes: 4

Shabbir
Shabbir

Reputation: 131

You can use paste command in UNIX for converting the file into multiple columns. By default, tab is the delimiter. To change the delimiter, use -d option

Command: convert one column file into two columns

paste - - < input.txt

output:

tom  bart 
jack foo 
kim  bar

Command: convert one column file into two columns with , as a delimiter

paste - - -d, < input.txt

Output:

tom,bart 
jack,foo 
kim,bar

Upvotes: 9

fedorqui
fedorqui

Reputation: 289725

What about using xargs?

two records per line:

$ xargs -n2 < file
tom jack
kim bart
foo bar

three records per line:

$ xargs -n3 < file
tom jack kim
bart foo bar

Upvotes: 14

Fredrik Pihl
Fredrik Pihl

Reputation: 45662

As a start, you can use sed to get the proper data, e.g.

$ sed -n '1~2p' input
tom
kim
foo

$ sed -n '2~2p' input
jack
bart
bar

Then there are many ways to convert a column into a row. To name a few:

tr '\n' ' ' < file

awk -vORS=' ' 1 file

paste -sd" " file

Upvotes: 0

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25875

With awk

 awk -v row=2 '{A[(NR-1)%row]=A[(NR-1)%row]$0" ";next}END{for(i in A)print A[i]}' file

output:

tom  bart 
jack foo 
kim  bar 

here specify no of row you want in raw variable. eg: row=3 for three rows.

Try like this for if you want only break column in specific rows

cat file | xargs -n2

Here 2 for each row contain 2 column, You can use what you want.

Upvotes: 3

BMW
BMW

Reputation: 45243

Using awk

awk '{a[(NR-1)%n]=a[(NR-1)%n]==""?$1:a[(NR-1)%n] OFS $1}END{for (i=0;i<n;i++) print a[i]}' n=3 OFS="\t" file

tom     bart
jack    foo
kim     bar


awk '{a[(NR-1)%n]=a[(NR-1)%n]==""?$1:a[(NR-1)%n] OFS $1}END{for (i=0;i<n;i++) print a[i]}' n=2 OFS="," file

tom,kim,foo
jack,bart,bar

Upvotes: 1

Related Questions