问题
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
回答1:
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.
回答2:
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
回答3:
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
回答4:
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
回答5:
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
来源:https://stackoverflow.com/questions/23004727/convert-single-column-file-into-multiple-columns