Rows to column conversion of file
Clash Royale CLAN TAG#URR8PPP
up vote
14
down vote
favorite
Suppose I have a file:
File1:
PAPER TEAM MANISH NISHA GARIMA JYOUTI ........etc
File2 I want:
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
Rows to column conversion of File1.
awk sed text-formatting
add a comment |Â
up vote
14
down vote
favorite
Suppose I have a file:
File1:
PAPER TEAM MANISH NISHA GARIMA JYOUTI ........etc
File2 I want:
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
Rows to column conversion of File1.
awk sed text-formatting
If your file consists of more than one line and your output should thus have more than one column, then try this AWK script.
â Dennis Williamson
Nov 26 '14 at 16:16
Very much related question: askubuntu.com/q/461144/295286
â Sergiy Kolodyazhnyy
Jan 16 '17 at 13:39
add a comment |Â
up vote
14
down vote
favorite
up vote
14
down vote
favorite
Suppose I have a file:
File1:
PAPER TEAM MANISH NISHA GARIMA JYOUTI ........etc
File2 I want:
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
Rows to column conversion of File1.
awk sed text-formatting
Suppose I have a file:
File1:
PAPER TEAM MANISH NISHA GARIMA JYOUTI ........etc
File2 I want:
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
Rows to column conversion of File1.
awk sed text-formatting
awk sed text-formatting
edited Sep 19 at 16:45
ñÃÂsýù÷
16k92563
16k92563
asked Nov 26 '14 at 5:12
yisha
2464513
2464513
If your file consists of more than one line and your output should thus have more than one column, then try this AWK script.
â Dennis Williamson
Nov 26 '14 at 16:16
Very much related question: askubuntu.com/q/461144/295286
â Sergiy Kolodyazhnyy
Jan 16 '17 at 13:39
add a comment |Â
If your file consists of more than one line and your output should thus have more than one column, then try this AWK script.
â Dennis Williamson
Nov 26 '14 at 16:16
Very much related question: askubuntu.com/q/461144/295286
â Sergiy Kolodyazhnyy
Jan 16 '17 at 13:39
If your file consists of more than one line and your output should thus have more than one column, then try this AWK script.
â Dennis Williamson
Nov 26 '14 at 16:16
If your file consists of more than one line and your output should thus have more than one column, then try this AWK script.
â Dennis Williamson
Nov 26 '14 at 16:16
Very much related question: askubuntu.com/q/461144/295286
â Sergiy Kolodyazhnyy
Jan 16 '17 at 13:39
Very much related question: askubuntu.com/q/461144/295286
â Sergiy Kolodyazhnyy
Jan 16 '17 at 13:39
add a comment |Â
11 Answers
11
active
oldest
votes
up vote
14
down vote
Using tr
, replace each repeated space character() with a single new-line(
n
) character.
tr -s ' ' 'n'< infile > outfile
But I think you want something like this?
1 2 3 4 1 a #
a b c d --> 2 b $
# $ @ % 3 c @
4 d %
With awk
we could do:
awk ' for (i=1; i<=NF; i++) RtoC[i]= (RtoC[i]? RtoC[i] FS $i: $i)
END for (i in RtoC) print RtoC[i] ' infile
This joins each same filed number positon into together and in END
prints the result that would be first row in first column , second row in second column, etc. Of course the input file is limited to your memory size.
add a comment |Â
up vote
7
down vote
You could simply do this through grep. By default grep, would print the match in a separate newline .
grep -oP 'S+' infile > outfile
OR
grep -o '[^[:space:]]+' infile > outfile
1
+1 for creative use ofgrep
â Volker Siegel
May 21 '16 at 0:25
add a comment |Â
up vote
7
down vote
You could also use the fmt
command:
~$ cat f
PAPER TEAM MANISH NISHA GARIMA JYOUTI
~$ fmt -1 f
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
add a comment |Â
up vote
7
down vote
With GNU datamash:
$ datamash -W transpose <file
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!
â Mark Stewart
Sep 19 at 20:07
add a comment |Â
up vote
6
down vote
You can also do this using sed
:
$ sed -e 's/ */n/g' file1 > file2
NOTE: Doesn't handle the situation where the words contain spaces.
add a comment |Â
up vote
5
down vote
Using awk
, setting the output field separator (OFS
) as the record (line) separator (RS
):
awk 'OFS=RS;$1=$11' file > file2
add a comment |Â
up vote
2
down vote
Using a for
loop:
for val in `cat file1` ; do echo $val >> file2; done;
add a comment |Â
up vote
0
down vote
You can also try using sed
$ sed -i.bak s@' '@'n'@g infile.txt
Please note that I am using @
as a separator for the substitution operation.
This will also create a backup file. In case you don't need a backup remove .bak
$ sed -i s@' '@'n'@g infile.txt
add a comment |Â
up vote
0
down vote
Python version:
python -c "import sys;lines=[l.replace(' ','n') for l in sys.stdin.readlines()];print(''.join(lines))" < input.txt > output.txt
This uses <
redirection into python's stdin from input.txt
and writes to output.txt
using >
redirection. The one-liner itself reads in all lines from stdin
into a list of strings,where all spaces are replaced with newlines, and we rebuild whole text using .join()
function.
Alternative approach to avoid multiple spaces in series being replaced with newlines is to use .split()
to break line into list of words. That way , we can ensure that each word is separated only by one newline
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt > output.txt
add a comment |Â
up vote
0
down vote
Using xargs
, (stolen from souravc's answer):
xargs -n 1 < File1 > File2
Or if any minor reformatting is needed, use printf
format strings as however might be needed:
xargs printf '%sn' < File1 > File2
add a comment |Â
up vote
0
down vote
My solution would be:
#!/bin/bash
cols=$(head -1 file.txt | wc -w)
for i in $(seq 1 $cols); do
cut -d ' ' -f$i file.txt | tr 'n' ' ' | sed s'/.$//'
echo
done
add a comment |Â
11 Answers
11
active
oldest
votes
11 Answers
11
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
14
down vote
Using tr
, replace each repeated space character() with a single new-line(
n
) character.
tr -s ' ' 'n'< infile > outfile
But I think you want something like this?
1 2 3 4 1 a #
a b c d --> 2 b $
# $ @ % 3 c @
4 d %
With awk
we could do:
awk ' for (i=1; i<=NF; i++) RtoC[i]= (RtoC[i]? RtoC[i] FS $i: $i)
END for (i in RtoC) print RtoC[i] ' infile
This joins each same filed number positon into together and in END
prints the result that would be first row in first column , second row in second column, etc. Of course the input file is limited to your memory size.
add a comment |Â
up vote
14
down vote
Using tr
, replace each repeated space character() with a single new-line(
n
) character.
tr -s ' ' 'n'< infile > outfile
But I think you want something like this?
1 2 3 4 1 a #
a b c d --> 2 b $
# $ @ % 3 c @
4 d %
With awk
we could do:
awk ' for (i=1; i<=NF; i++) RtoC[i]= (RtoC[i]? RtoC[i] FS $i: $i)
END for (i in RtoC) print RtoC[i] ' infile
This joins each same filed number positon into together and in END
prints the result that would be first row in first column , second row in second column, etc. Of course the input file is limited to your memory size.
add a comment |Â
up vote
14
down vote
up vote
14
down vote
Using tr
, replace each repeated space character() with a single new-line(
n
) character.
tr -s ' ' 'n'< infile > outfile
But I think you want something like this?
1 2 3 4 1 a #
a b c d --> 2 b $
# $ @ % 3 c @
4 d %
With awk
we could do:
awk ' for (i=1; i<=NF; i++) RtoC[i]= (RtoC[i]? RtoC[i] FS $i: $i)
END for (i in RtoC) print RtoC[i] ' infile
This joins each same filed number positon into together and in END
prints the result that would be first row in first column , second row in second column, etc. Of course the input file is limited to your memory size.
Using tr
, replace each repeated space character() with a single new-line(
n
) character.
tr -s ' ' 'n'< infile > outfile
But I think you want something like this?
1 2 3 4 1 a #
a b c d --> 2 b $
# $ @ % 3 c @
4 d %
With awk
we could do:
awk ' for (i=1; i<=NF; i++) RtoC[i]= (RtoC[i]? RtoC[i] FS $i: $i)
END for (i in RtoC) print RtoC[i] ' infile
This joins each same filed number positon into together and in END
prints the result that would be first row in first column , second row in second column, etc. Of course the input file is limited to your memory size.
edited Sep 19 at 16:43
answered Nov 26 '14 at 5:59
ñÃÂsýù÷
16k92563
16k92563
add a comment |Â
add a comment |Â
up vote
7
down vote
You could simply do this through grep. By default grep, would print the match in a separate newline .
grep -oP 'S+' infile > outfile
OR
grep -o '[^[:space:]]+' infile > outfile
1
+1 for creative use ofgrep
â Volker Siegel
May 21 '16 at 0:25
add a comment |Â
up vote
7
down vote
You could simply do this through grep. By default grep, would print the match in a separate newline .
grep -oP 'S+' infile > outfile
OR
grep -o '[^[:space:]]+' infile > outfile
1
+1 for creative use ofgrep
â Volker Siegel
May 21 '16 at 0:25
add a comment |Â
up vote
7
down vote
up vote
7
down vote
You could simply do this through grep. By default grep, would print the match in a separate newline .
grep -oP 'S+' infile > outfile
OR
grep -o '[^[:space:]]+' infile > outfile
You could simply do this through grep. By default grep, would print the match in a separate newline .
grep -oP 'S+' infile > outfile
OR
grep -o '[^[:space:]]+' infile > outfile
answered Nov 26 '14 at 6:18
Avinash Raj
2,58031127
2,58031127
1
+1 for creative use ofgrep
â Volker Siegel
May 21 '16 at 0:25
add a comment |Â
1
+1 for creative use ofgrep
â Volker Siegel
May 21 '16 at 0:25
1
1
+1 for creative use of
grep
â Volker Siegel
May 21 '16 at 0:25
+1 for creative use of
grep
â Volker Siegel
May 21 '16 at 0:25
add a comment |Â
up vote
7
down vote
You could also use the fmt
command:
~$ cat f
PAPER TEAM MANISH NISHA GARIMA JYOUTI
~$ fmt -1 f
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
add a comment |Â
up vote
7
down vote
You could also use the fmt
command:
~$ cat f
PAPER TEAM MANISH NISHA GARIMA JYOUTI
~$ fmt -1 f
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
add a comment |Â
up vote
7
down vote
up vote
7
down vote
You could also use the fmt
command:
~$ cat f
PAPER TEAM MANISH NISHA GARIMA JYOUTI
~$ fmt -1 f
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
You could also use the fmt
command:
~$ cat f
PAPER TEAM MANISH NISHA GARIMA JYOUTI
~$ fmt -1 f
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
answered Nov 26 '14 at 7:42
fredtantini
2,493715
2,493715
add a comment |Â
add a comment |Â
up vote
7
down vote
With GNU datamash:
$ datamash -W transpose <file
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!
â Mark Stewart
Sep 19 at 20:07
add a comment |Â
up vote
7
down vote
With GNU datamash:
$ datamash -W transpose <file
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!
â Mark Stewart
Sep 19 at 20:07
add a comment |Â
up vote
7
down vote
up vote
7
down vote
With GNU datamash:
$ datamash -W transpose <file
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
With GNU datamash:
$ datamash -W transpose <file
PAPER
TEAM
MANISH
NISHA
GARIMA
JYOUTI
answered Nov 26 '14 at 18:55
cuonglm
98.7k21188285
98.7k21188285
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!
â Mark Stewart
Sep 19 at 20:07
add a comment |Â
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!
â Mark Stewart
Sep 19 at 20:07
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!â Mark Stewart
Sep 19 at 20:07
datamash
seems like the best tool for the task, but fascinating how many other tools could be used!â Mark Stewart
Sep 19 at 20:07
add a comment |Â
up vote
6
down vote
You can also do this using sed
:
$ sed -e 's/ */n/g' file1 > file2
NOTE: Doesn't handle the situation where the words contain spaces.
add a comment |Â
up vote
6
down vote
You can also do this using sed
:
$ sed -e 's/ */n/g' file1 > file2
NOTE: Doesn't handle the situation where the words contain spaces.
add a comment |Â
up vote
6
down vote
up vote
6
down vote
You can also do this using sed
:
$ sed -e 's/ */n/g' file1 > file2
NOTE: Doesn't handle the situation where the words contain spaces.
You can also do this using sed
:
$ sed -e 's/ */n/g' file1 > file2
NOTE: Doesn't handle the situation where the words contain spaces.
edited Aug 1 '17 at 14:27
Philippos
5,95711546
5,95711546
answered Nov 26 '14 at 6:13
slmâ¦
239k65495665
239k65495665
add a comment |Â
add a comment |Â
up vote
5
down vote
Using awk
, setting the output field separator (OFS
) as the record (line) separator (RS
):
awk 'OFS=RS;$1=$11' file > file2
add a comment |Â
up vote
5
down vote
Using awk
, setting the output field separator (OFS
) as the record (line) separator (RS
):
awk 'OFS=RS;$1=$11' file > file2
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Using awk
, setting the output field separator (OFS
) as the record (line) separator (RS
):
awk 'OFS=RS;$1=$11' file > file2
Using awk
, setting the output field separator (OFS
) as the record (line) separator (RS
):
awk 'OFS=RS;$1=$11' file > file2
answered Nov 26 '14 at 5:30
jasonwryan
47.6k14131180
47.6k14131180
add a comment |Â
add a comment |Â
up vote
2
down vote
Using a for
loop:
for val in `cat file1` ; do echo $val >> file2; done;
add a comment |Â
up vote
2
down vote
Using a for
loop:
for val in `cat file1` ; do echo $val >> file2; done;
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Using a for
loop:
for val in `cat file1` ; do echo $val >> file2; done;
Using a for
loop:
for val in `cat file1` ; do echo $val >> file2; done;
answered Nov 26 '14 at 6:55
Mandar Shinde
1,38772746
1,38772746
add a comment |Â
add a comment |Â
up vote
0
down vote
You can also try using sed
$ sed -i.bak s@' '@'n'@g infile.txt
Please note that I am using @
as a separator for the substitution operation.
This will also create a backup file. In case you don't need a backup remove .bak
$ sed -i s@' '@'n'@g infile.txt
add a comment |Â
up vote
0
down vote
You can also try using sed
$ sed -i.bak s@' '@'n'@g infile.txt
Please note that I am using @
as a separator for the substitution operation.
This will also create a backup file. In case you don't need a backup remove .bak
$ sed -i s@' '@'n'@g infile.txt
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can also try using sed
$ sed -i.bak s@' '@'n'@g infile.txt
Please note that I am using @
as a separator for the substitution operation.
This will also create a backup file. In case you don't need a backup remove .bak
$ sed -i s@' '@'n'@g infile.txt
You can also try using sed
$ sed -i.bak s@' '@'n'@g infile.txt
Please note that I am using @
as a separator for the substitution operation.
This will also create a backup file. In case you don't need a backup remove .bak
$ sed -i s@' '@'n'@g infile.txt
edited Jan 16 '17 at 13:33
Kusalananda
108k14209332
108k14209332
answered Jan 16 '17 at 13:09
Vaibhav Shetye
93
93
add a comment |Â
add a comment |Â
up vote
0
down vote
Python version:
python -c "import sys;lines=[l.replace(' ','n') for l in sys.stdin.readlines()];print(''.join(lines))" < input.txt > output.txt
This uses <
redirection into python's stdin from input.txt
and writes to output.txt
using >
redirection. The one-liner itself reads in all lines from stdin
into a list of strings,where all spaces are replaced with newlines, and we rebuild whole text using .join()
function.
Alternative approach to avoid multiple spaces in series being replaced with newlines is to use .split()
to break line into list of words. That way , we can ensure that each word is separated only by one newline
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt > output.txt
add a comment |Â
up vote
0
down vote
Python version:
python -c "import sys;lines=[l.replace(' ','n') for l in sys.stdin.readlines()];print(''.join(lines))" < input.txt > output.txt
This uses <
redirection into python's stdin from input.txt
and writes to output.txt
using >
redirection. The one-liner itself reads in all lines from stdin
into a list of strings,where all spaces are replaced with newlines, and we rebuild whole text using .join()
function.
Alternative approach to avoid multiple spaces in series being replaced with newlines is to use .split()
to break line into list of words. That way , we can ensure that each word is separated only by one newline
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt > output.txt
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Python version:
python -c "import sys;lines=[l.replace(' ','n') for l in sys.stdin.readlines()];print(''.join(lines))" < input.txt > output.txt
This uses <
redirection into python's stdin from input.txt
and writes to output.txt
using >
redirection. The one-liner itself reads in all lines from stdin
into a list of strings,where all spaces are replaced with newlines, and we rebuild whole text using .join()
function.
Alternative approach to avoid multiple spaces in series being replaced with newlines is to use .split()
to break line into list of words. That way , we can ensure that each word is separated only by one newline
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt > output.txt
Python version:
python -c "import sys;lines=[l.replace(' ','n') for l in sys.stdin.readlines()];print(''.join(lines))" < input.txt > output.txt
This uses <
redirection into python's stdin from input.txt
and writes to output.txt
using >
redirection. The one-liner itself reads in all lines from stdin
into a list of strings,where all spaces are replaced with newlines, and we rebuild whole text using .join()
function.
Alternative approach to avoid multiple spaces in series being replaced with newlines is to use .split()
to break line into list of words. That way , we can ensure that each word is separated only by one newline
python -c "import sys;lines=['n'.join(l.strip().split()) for l in sys.stdin.readlines()];print('n'.join(lines))" < input.txt > output.txt
answered Jan 16 '17 at 13:46
Sergiy Kolodyazhnyy
7,95511848
7,95511848
add a comment |Â
add a comment |Â
up vote
0
down vote
Using xargs
, (stolen from souravc's answer):
xargs -n 1 < File1 > File2
Or if any minor reformatting is needed, use printf
format strings as however might be needed:
xargs printf '%sn' < File1 > File2
add a comment |Â
up vote
0
down vote
Using xargs
, (stolen from souravc's answer):
xargs -n 1 < File1 > File2
Or if any minor reformatting is needed, use printf
format strings as however might be needed:
xargs printf '%sn' < File1 > File2
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using xargs
, (stolen from souravc's answer):
xargs -n 1 < File1 > File2
Or if any minor reformatting is needed, use printf
format strings as however might be needed:
xargs printf '%sn' < File1 > File2
Using xargs
, (stolen from souravc's answer):
xargs -n 1 < File1 > File2
Or if any minor reformatting is needed, use printf
format strings as however might be needed:
xargs printf '%sn' < File1 > File2
edited Apr 9 at 19:36
answered Apr 9 at 19:03
agc
4,3221935
4,3221935
add a comment |Â
add a comment |Â
up vote
0
down vote
My solution would be:
#!/bin/bash
cols=$(head -1 file.txt | wc -w)
for i in $(seq 1 $cols); do
cut -d ' ' -f$i file.txt | tr 'n' ' ' | sed s'/.$//'
echo
done
add a comment |Â
up vote
0
down vote
My solution would be:
#!/bin/bash
cols=$(head -1 file.txt | wc -w)
for i in $(seq 1 $cols); do
cut -d ' ' -f$i file.txt | tr 'n' ' ' | sed s'/.$//'
echo
done
add a comment |Â
up vote
0
down vote
up vote
0
down vote
My solution would be:
#!/bin/bash
cols=$(head -1 file.txt | wc -w)
for i in $(seq 1 $cols); do
cut -d ' ' -f$i file.txt | tr 'n' ' ' | sed s'/.$//'
echo
done
My solution would be:
#!/bin/bash
cols=$(head -1 file.txt | wc -w)
for i in $(seq 1 $cols); do
cut -d ' ' -f$i file.txt | tr 'n' ' ' | sed s'/.$//'
echo
done
answered Jun 20 at 21:31
Saumyakanta Sahoo
1
1
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f169995%2frows-to-column-conversion-of-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
If your file consists of more than one line and your output should thus have more than one column, then try this AWK script.
â Dennis Williamson
Nov 26 '14 at 16:16
Very much related question: askubuntu.com/q/461144/295286
â Sergiy Kolodyazhnyy
Jan 16 '17 at 13:39