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
With
awk
output:
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.
Using awk
As a start, you can use sed to get the proper data, e.g.
Then there are many ways to convert a column into a row. To name a few:
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
optionCommand: convert one column file into two columns
output:
Command: convert one column file into two columns with
,
as a delimiterOutput:
What about using
xargs
?two records per line:
three records per line: