Split single line into multiple lines, Newline character missing for all the lines in input file [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
This question already has an answer here:
How to process a multi column text file to get another multi column text file?
7 answers
I there a way to split single line into multiple lines with 3 columns.
New line characters are missing at the end of all the lines in the file.
I tried using awk, but it is splitting each column as one row instead of 3 columns in each row.
awk ' gsub(",", "n") 6' filename
where filename's content looks like:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O
Desired output has 3 columns in each line:
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
linux text-processing newlines split
marked as duplicate by Sundeep, Shadur, SatÃ
 Katsura, Timothy Martin, G-Man Mar 16 at 22:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
3
down vote
favorite
This question already has an answer here:
How to process a multi column text file to get another multi column text file?
7 answers
I there a way to split single line into multiple lines with 3 columns.
New line characters are missing at the end of all the lines in the file.
I tried using awk, but it is splitting each column as one row instead of 3 columns in each row.
awk ' gsub(",", "n") 6' filename
where filename's content looks like:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O
Desired output has 3 columns in each line:
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
linux text-processing newlines split
marked as duplicate by Sundeep, Shadur, SatÃ
 Katsura, Timothy Martin, G-Man Mar 16 at 22:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please take a look at editing-help.
â Cyrus
Mar 16 at 5:48
Looks like a job forrs(the "reshape" command).rs -c, -C, 0 3 <<<'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O'has you most of the way there.
â Toby Speight
Mar 16 at 13:31
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This question already has an answer here:
How to process a multi column text file to get another multi column text file?
7 answers
I there a way to split single line into multiple lines with 3 columns.
New line characters are missing at the end of all the lines in the file.
I tried using awk, but it is splitting each column as one row instead of 3 columns in each row.
awk ' gsub(",", "n") 6' filename
where filename's content looks like:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O
Desired output has 3 columns in each line:
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
linux text-processing newlines split
This question already has an answer here:
How to process a multi column text file to get another multi column text file?
7 answers
I there a way to split single line into multiple lines with 3 columns.
New line characters are missing at the end of all the lines in the file.
I tried using awk, but it is splitting each column as one row instead of 3 columns in each row.
awk ' gsub(",", "n") 6' filename
where filename's content looks like:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O
Desired output has 3 columns in each line:
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
This question already has an answer here:
How to process a multi column text file to get another multi column text file?
7 answers
linux text-processing newlines split
edited Mar 16 at 10:33
Jeff Schaller
31.2k846105
31.2k846105
asked Mar 16 at 4:58
Rakesh K
5619
5619
marked as duplicate by Sundeep, Shadur, SatÃ
 Katsura, Timothy Martin, G-Man Mar 16 at 22:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Sundeep, Shadur, SatÃ
 Katsura, Timothy Martin, G-Man Mar 16 at 22:17
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Please take a look at editing-help.
â Cyrus
Mar 16 at 5:48
Looks like a job forrs(the "reshape" command).rs -c, -C, 0 3 <<<'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O'has you most of the way there.
â Toby Speight
Mar 16 at 13:31
add a comment |Â
Please take a look at editing-help.
â Cyrus
Mar 16 at 5:48
Looks like a job forrs(the "reshape" command).rs -c, -C, 0 3 <<<'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O'has you most of the way there.
â Toby Speight
Mar 16 at 13:31
Please take a look at editing-help.
â Cyrus
Mar 16 at 5:48
Please take a look at editing-help.
â Cyrus
Mar 16 at 5:48
Looks like a job for
rs (the "reshape" command). rs -c, -C, 0 3 <<<'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O' has you most of the way there.â Toby Speight
Mar 16 at 13:31
Looks like a job for
rs (the "reshape" command). rs -c, -C, 0 3 <<<'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O' has you most of the way there.â Toby Speight
Mar 16 at 13:31
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
6
down vote
accepted
Using awk
$ awk -v RS='[,n]' 'a=$0;getline b; getline c; print a,b,c' OFS=, filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
a=$0; getline b; getline cThis tells awk to save the current line in variable
a, the next line in varaibleb, and the next line after that in variablec.print a,b,cThis tells awk to print
a,b, andcOFS=,This tells awk to use a comma as the field separator on output.
Using tr and paste
$ tr , 'n' <filename | paste -d, - - -
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
tr , 'n' <filenameThis reads from filename while converting all commas to newlines.
paste -d, - - -This
pasteto read three lines from stdin (one for each-) and paste them together, each separated by a comma (-d,).
Alternate awk
$ awk -v RS='[,n]' 'printf "%s%s",$0,(NR%3?",":"n")' filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
printf "%s%s",$0,(NR%3?",":"n")This tells awk to print the current line followed by either a comma or a newline depending the value of the current line number,
NR, modulo 3.
1
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
add a comment |Â
up vote
4
down vote
sed 's/(([^,]+,)3)/1n/g;s/,n/n/g' filename
I know that you asked for an awk solution, and I'll now try to submit that as an edit to this answer, but for me a sed solution was simpler... ... and user john1024 beat me to it, with a fine awk solution. See there. His paste and tr solution is probably the most proper classic unix-ish answer.
This solution uses the extended regex features of GNU sed.
(..)is a regex collection group. Note that the solution uses two, one nested within the other.[^,]+,is any string that doesn't have a comma, followed by a comma. In your case, a column or field.3is a regex multiplier, indicating to use the prior regex expression three times.1is a regex back-reference. to the prior regex.gmeans do it for all instances on the line.s/,n/n/gremoves the trailing comma. It's necessary to include the newline character here, becausesedis still considering the input as a single line.
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
Using awk
$ awk -v RS='[,n]' 'a=$0;getline b; getline c; print a,b,c' OFS=, filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
a=$0; getline b; getline cThis tells awk to save the current line in variable
a, the next line in varaibleb, and the next line after that in variablec.print a,b,cThis tells awk to print
a,b, andcOFS=,This tells awk to use a comma as the field separator on output.
Using tr and paste
$ tr , 'n' <filename | paste -d, - - -
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
tr , 'n' <filenameThis reads from filename while converting all commas to newlines.
paste -d, - - -This
pasteto read three lines from stdin (one for each-) and paste them together, each separated by a comma (-d,).
Alternate awk
$ awk -v RS='[,n]' 'printf "%s%s",$0,(NR%3?",":"n")' filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
printf "%s%s",$0,(NR%3?",":"n")This tells awk to print the current line followed by either a comma or a newline depending the value of the current line number,
NR, modulo 3.
1
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
add a comment |Â
up vote
6
down vote
accepted
Using awk
$ awk -v RS='[,n]' 'a=$0;getline b; getline c; print a,b,c' OFS=, filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
a=$0; getline b; getline cThis tells awk to save the current line in variable
a, the next line in varaibleb, and the next line after that in variablec.print a,b,cThis tells awk to print
a,b, andcOFS=,This tells awk to use a comma as the field separator on output.
Using tr and paste
$ tr , 'n' <filename | paste -d, - - -
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
tr , 'n' <filenameThis reads from filename while converting all commas to newlines.
paste -d, - - -This
pasteto read three lines from stdin (one for each-) and paste them together, each separated by a comma (-d,).
Alternate awk
$ awk -v RS='[,n]' 'printf "%s%s",$0,(NR%3?",":"n")' filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
printf "%s%s",$0,(NR%3?",":"n")This tells awk to print the current line followed by either a comma or a newline depending the value of the current line number,
NR, modulo 3.
1
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
Using awk
$ awk -v RS='[,n]' 'a=$0;getline b; getline c; print a,b,c' OFS=, filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
a=$0; getline b; getline cThis tells awk to save the current line in variable
a, the next line in varaibleb, and the next line after that in variablec.print a,b,cThis tells awk to print
a,b, andcOFS=,This tells awk to use a comma as the field separator on output.
Using tr and paste
$ tr , 'n' <filename | paste -d, - - -
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
tr , 'n' <filenameThis reads from filename while converting all commas to newlines.
paste -d, - - -This
pasteto read three lines from stdin (one for each-) and paste them together, each separated by a comma (-d,).
Alternate awk
$ awk -v RS='[,n]' 'printf "%s%s",$0,(NR%3?",":"n")' filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
printf "%s%s",$0,(NR%3?",":"n")This tells awk to print the current line followed by either a comma or a newline depending the value of the current line number,
NR, modulo 3.
Using awk
$ awk -v RS='[,n]' 'a=$0;getline b; getline c; print a,b,c' OFS=, filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
a=$0; getline b; getline cThis tells awk to save the current line in variable
a, the next line in varaibleb, and the next line after that in variablec.print a,b,cThis tells awk to print
a,b, andcOFS=,This tells awk to use a comma as the field separator on output.
Using tr and paste
$ tr , 'n' <filename | paste -d, - - -
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
tr , 'n' <filenameThis reads from filename while converting all commas to newlines.
paste -d, - - -This
pasteto read three lines from stdin (one for each-) and paste them together, each separated by a comma (-d,).
Alternate awk
$ awk -v RS='[,n]' 'printf "%s%s",$0,(NR%3?",":"n")' filename
A,B,C
D,E,F
G,H,I
J,K,L
M,N,O
How it works
-v RS='[,n]'This tells awk to use any occurrence of either a comma or a newline as a record separator.
printf "%s%s",$0,(NR%3?",":"n")This tells awk to print the current line followed by either a comma or a newline depending the value of the current line number,
NR, modulo 3.
edited Mar 16 at 5:44
answered Mar 16 at 5:33
John1024
44k499115
44k499115
1
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
add a comment |Â
1
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
1
1
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
Thank you for quick response.. Yes,, Its working fine. :)
â Rakesh K
Mar 16 at 6:15
add a comment |Â
up vote
4
down vote
sed 's/(([^,]+,)3)/1n/g;s/,n/n/g' filename
I know that you asked for an awk solution, and I'll now try to submit that as an edit to this answer, but for me a sed solution was simpler... ... and user john1024 beat me to it, with a fine awk solution. See there. His paste and tr solution is probably the most proper classic unix-ish answer.
This solution uses the extended regex features of GNU sed.
(..)is a regex collection group. Note that the solution uses two, one nested within the other.[^,]+,is any string that doesn't have a comma, followed by a comma. In your case, a column or field.3is a regex multiplier, indicating to use the prior regex expression three times.1is a regex back-reference. to the prior regex.gmeans do it for all instances on the line.s/,n/n/gremoves the trailing comma. It's necessary to include the newline character here, becausesedis still considering the input as a single line.
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
add a comment |Â
up vote
4
down vote
sed 's/(([^,]+,)3)/1n/g;s/,n/n/g' filename
I know that you asked for an awk solution, and I'll now try to submit that as an edit to this answer, but for me a sed solution was simpler... ... and user john1024 beat me to it, with a fine awk solution. See there. His paste and tr solution is probably the most proper classic unix-ish answer.
This solution uses the extended regex features of GNU sed.
(..)is a regex collection group. Note that the solution uses two, one nested within the other.[^,]+,is any string that doesn't have a comma, followed by a comma. In your case, a column or field.3is a regex multiplier, indicating to use the prior regex expression three times.1is a regex back-reference. to the prior regex.gmeans do it for all instances on the line.s/,n/n/gremoves the trailing comma. It's necessary to include the newline character here, becausesedis still considering the input as a single line.
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
add a comment |Â
up vote
4
down vote
up vote
4
down vote
sed 's/(([^,]+,)3)/1n/g;s/,n/n/g' filename
I know that you asked for an awk solution, and I'll now try to submit that as an edit to this answer, but for me a sed solution was simpler... ... and user john1024 beat me to it, with a fine awk solution. See there. His paste and tr solution is probably the most proper classic unix-ish answer.
This solution uses the extended regex features of GNU sed.
(..)is a regex collection group. Note that the solution uses two, one nested within the other.[^,]+,is any string that doesn't have a comma, followed by a comma. In your case, a column or field.3is a regex multiplier, indicating to use the prior regex expression three times.1is a regex back-reference. to the prior regex.gmeans do it for all instances on the line.s/,n/n/gremoves the trailing comma. It's necessary to include the newline character here, becausesedis still considering the input as a single line.
sed 's/(([^,]+,)3)/1n/g;s/,n/n/g' filename
I know that you asked for an awk solution, and I'll now try to submit that as an edit to this answer, but for me a sed solution was simpler... ... and user john1024 beat me to it, with a fine awk solution. See there. His paste and tr solution is probably the most proper classic unix-ish answer.
This solution uses the extended regex features of GNU sed.
(..)is a regex collection group. Note that the solution uses two, one nested within the other.[^,]+,is any string that doesn't have a comma, followed by a comma. In your case, a column or field.3is a regex multiplier, indicating to use the prior regex expression three times.1is a regex back-reference. to the prior regex.gmeans do it for all instances on the line.s/,n/n/gremoves the trailing comma. It's necessary to include the newline character here, becausesedis still considering the input as a single line.
edited Mar 16 at 5:41
answered Mar 16 at 5:25
user1404316
2,314520
2,314520
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
add a comment |Â
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
I had the original version inadequately indented so some of the backslashes did not appear in the markdown output.
â user1404316
Mar 16 at 5:36
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
@RakeshK You're quite welcome.
â user1404316
Mar 16 at 6:29
add a comment |Â
Please take a look at editing-help.
â Cyrus
Mar 16 at 5:48
Looks like a job for
rs(the "reshape" command).rs -c, -C, 0 3 <<<'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O'has you most of the way there.â Toby Speight
Mar 16 at 13:31