Replace the third comma on each line with newline
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I have these long IDs which consist of four sections:
AKJHGFGUIKL,OIUYT,KJHBTYUI,98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL,6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH,34567898766
I want to put the numbers in new line and remove the third comma.
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
How can I do this?
text-processing csv
New contributor
add a comment |Â
up vote
7
down vote
favorite
I have these long IDs which consist of four sections:
AKJHGFGUIKL,OIUYT,KJHBTYUI,98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL,6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH,34567898766
I want to put the numbers in new line and remove the third comma.
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
How can I do this?
text-processing csv
New contributor
3
Hello Simon Q. What have you tried so far?
â roaima
17 hours ago
1
@roaima I tried sed and awk but failed, I spent four hours on this!
â Simon Q
13 hours ago
@SimonQ, 4 hours, ouch! Anyway, for next time please remember that it helps other learners (among whom are many good people also) to see the failed code. It helps shows them what to avoid.
â agc
2 hours ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I have these long IDs which consist of four sections:
AKJHGFGUIKL,OIUYT,KJHBTYUI,98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL,6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH,34567898766
I want to put the numbers in new line and remove the third comma.
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
How can I do this?
text-processing csv
New contributor
I have these long IDs which consist of four sections:
AKJHGFGUIKL,OIUYT,KJHBTYUI,98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL,6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH,34567898766
I want to put the numbers in new line and remove the third comma.
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
How can I do this?
text-processing csv
text-processing csv
New contributor
New contributor
edited 12 mins ago
don_crissti
47.7k15126155
47.7k15126155
New contributor
asked 20 hours ago
Simon Q
503
503
New contributor
New contributor
3
Hello Simon Q. What have you tried so far?
â roaima
17 hours ago
1
@roaima I tried sed and awk but failed, I spent four hours on this!
â Simon Q
13 hours ago
@SimonQ, 4 hours, ouch! Anyway, for next time please remember that it helps other learners (among whom are many good people also) to see the failed code. It helps shows them what to avoid.
â agc
2 hours ago
add a comment |Â
3
Hello Simon Q. What have you tried so far?
â roaima
17 hours ago
1
@roaima I tried sed and awk but failed, I spent four hours on this!
â Simon Q
13 hours ago
@SimonQ, 4 hours, ouch! Anyway, for next time please remember that it helps other learners (among whom are many good people also) to see the failed code. It helps shows them what to avoid.
â agc
2 hours ago
3
3
Hello Simon Q. What have you tried so far?
â roaima
17 hours ago
Hello Simon Q. What have you tried so far?
â roaima
17 hours ago
1
1
@roaima I tried sed and awk but failed, I spent four hours on this!
â Simon Q
13 hours ago
@roaima I tried sed and awk but failed, I spent four hours on this!
â Simon Q
13 hours ago
@SimonQ, 4 hours, ouch! Anyway, for next time please remember that it helps other learners (among whom are many good people also) to see the failed code. It helps shows them what to avoid.
â agc
2 hours ago
@SimonQ, 4 hours, ouch! Anyway, for next time please remember that it helps other learners (among whom are many good people also) to see the failed code. It helps shows them what to avoid.
â agc
2 hours ago
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
15
down vote
accepted
Using GNU sed
:
sed "s/,/n/3; G" file
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
Explanationð¡:
sed
stands for stream editor..sed
has the following syntax:
substitute/match(or pattern)/replacement/position file
In the above command, s
substitute the third ,
by a new line n
.
G
is to append a new line to the contents of the pattern space and then append the contents of the hold space to the pattern space.
2
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
8
down vote
awk -F, -vOFS=, 'print $1,$2,$3; print $4; print ""' file
will produce your desired output
add a comment |Â
up vote
3
down vote
A few Perl approaches:
$ perl -pe 's/,([^,]+)$/n$1n/' file
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
The -p
means "read the input file line by line and print each line after applying the script given by -e
to it". The s/foo/bar/
is the substitution operator which will replace foo
with bar
. Here, we are matching a comma followed by one or more non-comma characters ([^,]+
) until the end of the line ($
). The parentheses around the ([^,]+)
will "capture" whatever is matched so we can refer to it as $1
on the right hand side of the operator. Therefore, this will replace the text after the last comma with a newline, then the matched text and then another newline.
If you can't be sure the third comma is the last one, you can do:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
or
perl -pe 's/(.+?,.+?,.+?),(.+)/$1n$2n/' file
And here are some more, just for fun:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
perl -F, -pe '$k=pop(@F); $_=join(",", @F)."n$kn"' file
perl -F, -le 'print join ",", @F[0..2],"n@F[3..$#F]n"' file
add a comment |Â
up vote
2
down vote
awk -F, 'print $1, $2, $3,"n"$4'
2
You'd need to setOFS=,
as well in order to preserve the other delimiters
â steeldriver
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
... then change the last part to a simple concatenation$3 "n" $4
â steeldriver
20 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
1
down vote
Alternative less succinct sed
code:
sed 's/,([^,]*)/n1n/3' file
...which can be used if the hold buffer were needed for some other purpose, (supposing some additional requirement). If you need portable code (the above is gnu sed
syntax) use literal newlines in the RHS:
sed 's/,([^,]*)/
1
/3' file
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
15
down vote
accepted
Using GNU sed
:
sed "s/,/n/3; G" file
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
Explanationð¡:
sed
stands for stream editor..sed
has the following syntax:
substitute/match(or pattern)/replacement/position file
In the above command, s
substitute the third ,
by a new line n
.
G
is to append a new line to the contents of the pattern space and then append the contents of the hold space to the pattern space.
2
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
15
down vote
accepted
Using GNU sed
:
sed "s/,/n/3; G" file
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
Explanationð¡:
sed
stands for stream editor..sed
has the following syntax:
substitute/match(or pattern)/replacement/position file
In the above command, s
substitute the third ,
by a new line n
.
G
is to append a new line to the contents of the pattern space and then append the contents of the hold space to the pattern space.
2
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
15
down vote
accepted
up vote
15
down vote
accepted
Using GNU sed
:
sed "s/,/n/3; G" file
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
Explanationð¡:
sed
stands for stream editor..sed
has the following syntax:
substitute/match(or pattern)/replacement/position file
In the above command, s
substitute the third ,
by a new line n
.
G
is to append a new line to the contents of the pattern space and then append the contents of the hold space to the pattern space.
Using GNU sed
:
sed "s/,/n/3; G" file
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
Explanationð¡:
sed
stands for stream editor..sed
has the following syntax:
substitute/match(or pattern)/replacement/position file
In the above command, s
substitute the third ,
by a new line n
.
G
is to append a new line to the contents of the pattern space and then append the contents of the hold space to the pattern space.
edited 4 hours ago
answered 20 hours ago
Goro
8,68754384
8,68754384
2
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
add a comment |Â
2
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
2
2
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
It would be great if you could add a short explanation of how this works for users who don't know sed well.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
8
down vote
awk -F, -vOFS=, 'print $1,$2,$3; print $4; print ""' file
will produce your desired output
add a comment |Â
up vote
8
down vote
awk -F, -vOFS=, 'print $1,$2,$3; print $4; print ""' file
will produce your desired output
add a comment |Â
up vote
8
down vote
up vote
8
down vote
awk -F, -vOFS=, 'print $1,$2,$3; print $4; print ""' file
will produce your desired output
awk -F, -vOFS=, 'print $1,$2,$3; print $4; print ""' file
will produce your desired output
answered 20 hours ago
glenn jackman
48.9k466105
48.9k466105
add a comment |Â
add a comment |Â
up vote
3
down vote
A few Perl approaches:
$ perl -pe 's/,([^,]+)$/n$1n/' file
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
The -p
means "read the input file line by line and print each line after applying the script given by -e
to it". The s/foo/bar/
is the substitution operator which will replace foo
with bar
. Here, we are matching a comma followed by one or more non-comma characters ([^,]+
) until the end of the line ($
). The parentheses around the ([^,]+)
will "capture" whatever is matched so we can refer to it as $1
on the right hand side of the operator. Therefore, this will replace the text after the last comma with a newline, then the matched text and then another newline.
If you can't be sure the third comma is the last one, you can do:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
or
perl -pe 's/(.+?,.+?,.+?),(.+)/$1n$2n/' file
And here are some more, just for fun:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
perl -F, -pe '$k=pop(@F); $_=join(",", @F)."n$kn"' file
perl -F, -le 'print join ",", @F[0..2],"n@F[3..$#F]n"' file
add a comment |Â
up vote
3
down vote
A few Perl approaches:
$ perl -pe 's/,([^,]+)$/n$1n/' file
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
The -p
means "read the input file line by line and print each line after applying the script given by -e
to it". The s/foo/bar/
is the substitution operator which will replace foo
with bar
. Here, we are matching a comma followed by one or more non-comma characters ([^,]+
) until the end of the line ($
). The parentheses around the ([^,]+)
will "capture" whatever is matched so we can refer to it as $1
on the right hand side of the operator. Therefore, this will replace the text after the last comma with a newline, then the matched text and then another newline.
If you can't be sure the third comma is the last one, you can do:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
or
perl -pe 's/(.+?,.+?,.+?),(.+)/$1n$2n/' file
And here are some more, just for fun:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
perl -F, -pe '$k=pop(@F); $_=join(",", @F)."n$kn"' file
perl -F, -le 'print join ",", @F[0..2],"n@F[3..$#F]n"' file
add a comment |Â
up vote
3
down vote
up vote
3
down vote
A few Perl approaches:
$ perl -pe 's/,([^,]+)$/n$1n/' file
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
The -p
means "read the input file line by line and print each line after applying the script given by -e
to it". The s/foo/bar/
is the substitution operator which will replace foo
with bar
. Here, we are matching a comma followed by one or more non-comma characters ([^,]+
) until the end of the line ($
). The parentheses around the ([^,]+)
will "capture" whatever is matched so we can refer to it as $1
on the right hand side of the operator. Therefore, this will replace the text after the last comma with a newline, then the matched text and then another newline.
If you can't be sure the third comma is the last one, you can do:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
or
perl -pe 's/(.+?,.+?,.+?),(.+)/$1n$2n/' file
And here are some more, just for fun:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
perl -F, -pe '$k=pop(@F); $_=join(",", @F)."n$kn"' file
perl -F, -le 'print join ",", @F[0..2],"n@F[3..$#F]n"' file
A few Perl approaches:
$ perl -pe 's/,([^,]+)$/n$1n/' file
AKJHGFGUIKL,OIUYT,KJHBTYUI
98765434567
RTYUIKHGFGH,TYUJI,TGHYJKJKLJKL
6789876
ETRYTUUI,YTYUIL,UIOKJHGFGH
34567898766
The -p
means "read the input file line by line and print each line after applying the script given by -e
to it". The s/foo/bar/
is the substitution operator which will replace foo
with bar
. Here, we are matching a comma followed by one or more non-comma characters ([^,]+
) until the end of the line ($
). The parentheses around the ([^,]+)
will "capture" whatever is matched so we can refer to it as $1
on the right hand side of the operator. Therefore, this will replace the text after the last comma with a newline, then the matched text and then another newline.
If you can't be sure the third comma is the last one, you can do:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
or
perl -pe 's/(.+?,.+?,.+?),(.+)/$1n$2n/' file
And here are some more, just for fun:
perl -pe 's/([^,]+,)3K(.+)/n$2n/' file
perl -F, -pe '$k=pop(@F); $_=join(",", @F)."n$kn"' file
perl -F, -le 'print join ",", @F[0..2],"n@F[3..$#F]n"' file
answered 5 hours ago
terdonâ¦
124k29234412
124k29234412
add a comment |Â
add a comment |Â
up vote
2
down vote
awk -F, 'print $1, $2, $3,"n"$4'
2
You'd need to setOFS=,
as well in order to preserve the other delimiters
â steeldriver
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
... then change the last part to a simple concatenation$3 "n" $4
â steeldriver
20 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
2
down vote
awk -F, 'print $1, $2, $3,"n"$4'
2
You'd need to setOFS=,
as well in order to preserve the other delimiters
â steeldriver
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
... then change the last part to a simple concatenation$3 "n" $4
â steeldriver
20 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
awk -F, 'print $1, $2, $3,"n"$4'
awk -F, 'print $1, $2, $3,"n"$4'
answered 20 hours ago
TNT
363313
363313
2
You'd need to setOFS=,
as well in order to preserve the other delimiters
â steeldriver
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
... then change the last part to a simple concatenation$3 "n" $4
â steeldriver
20 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
add a comment |Â
2
You'd need to setOFS=,
as well in order to preserve the other delimiters
â steeldriver
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
... then change the last part to a simple concatenation$3 "n" $4
â steeldriver
20 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
2
2
You'd need to set
OFS=,
as well in order to preserve the other delimitersâ steeldriver
20 hours ago
You'd need to set
OFS=,
as well in order to preserve the other delimitersâ steeldriver
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
@steeldriver but it will not remove the third comma as requested?
â TNT
20 hours ago
... then change the last part to a simple concatenation
$3 "n" $4
â steeldriver
20 hours ago
... then change the last part to a simple concatenation
$3 "n" $4
â steeldriver
20 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
This removes all commas, replacing them with spaces and doesn't add the extra newline between records. Please make sure your output is the same as the desired output shown in the question.
â terdonâ¦
5 hours ago
add a comment |Â
up vote
1
down vote
Alternative less succinct sed
code:
sed 's/,([^,]*)/n1n/3' file
...which can be used if the hold buffer were needed for some other purpose, (supposing some additional requirement). If you need portable code (the above is gnu sed
syntax) use literal newlines in the RHS:
sed 's/,([^,]*)/
1
/3' file
add a comment |Â
up vote
1
down vote
Alternative less succinct sed
code:
sed 's/,([^,]*)/n1n/3' file
...which can be used if the hold buffer were needed for some other purpose, (supposing some additional requirement). If you need portable code (the above is gnu sed
syntax) use literal newlines in the RHS:
sed 's/,([^,]*)/
1
/3' file
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Alternative less succinct sed
code:
sed 's/,([^,]*)/n1n/3' file
...which can be used if the hold buffer were needed for some other purpose, (supposing some additional requirement). If you need portable code (the above is gnu sed
syntax) use literal newlines in the RHS:
sed 's/,([^,]*)/
1
/3' file
Alternative less succinct sed
code:
sed 's/,([^,]*)/n1n/3' file
...which can be used if the hold buffer were needed for some other purpose, (supposing some additional requirement). If you need portable code (the above is gnu sed
syntax) use literal newlines in the RHS:
sed 's/,([^,]*)/
1
/3' file
edited 10 mins ago
don_crissti
47.7k15126155
47.7k15126155
answered 2 hours ago
agc
4,3571935
4,3571935
add a comment |Â
add a comment |Â
Simon Q is a new contributor. Be nice, and check out our Code of Conduct.
Simon Q is a new contributor. Be nice, and check out our Code of Conduct.
Simon Q is a new contributor. Be nice, and check out our Code of Conduct.
Simon Q is a new contributor. Be nice, and check out our Code of Conduct.
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%2f474882%2freplace-the-third-comma-on-each-line-with-newline%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
3
Hello Simon Q. What have you tried so far?
â roaima
17 hours ago
1
@roaima I tried sed and awk but failed, I spent four hours on this!
â Simon Q
13 hours ago
@SimonQ, 4 hours, ouch! Anyway, for next time please remember that it helps other learners (among whom are many good people also) to see the failed code. It helps shows them what to avoid.
â agc
2 hours ago