Convert single column file into multiple columns

微笑、不失礼 提交于 2019-12-01 20:16:53

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.

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

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

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!