change only part of the substring using sed
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I have a file that contains a numbers copied from somewhere. It looks something like this:
02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03
I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.
I tried using sed like:cat file.txt | sed -r "s/ /, /g"
But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','
If I use cat file.txt | sed -r "s/[0123456789] /, /g"
, I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.
How do I do this?
sed
add a comment |Â
up vote
5
down vote
favorite
I have a file that contains a numbers copied from somewhere. It looks something like this:
02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03
I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.
I tried using sed like:cat file.txt | sed -r "s/ /, /g"
But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','
If I use cat file.txt | sed -r "s/[0123456789] /, /g"
, I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.
How do I do this?
sed
1
@Hello scipsycho. Please see below. is this what you want?
â Goro
Sep 27 at 16:14
Just a heads-up: It won't compile even with commas.08
and09
are not valid integer literals.
â pipe
Sep 28 at 1:58
@pipe you are right! but now that you guys have answered this question, I will docat file.txt | sed -r 's/([{, ])0+([0-9])+/ 1 2/g'
which will remove any zeros occurring in the units place
â scipsycho
Sep 28 at 2:48
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have a file that contains a numbers copied from somewhere. It looks something like this:
02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03
I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.
I tried using sed like:cat file.txt | sed -r "s/ /, /g"
But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','
If I use cat file.txt | sed -r "s/[0123456789] /, /g"
, I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.
How do I do this?
sed
I have a file that contains a numbers copied from somewhere. It looks something like this:
02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03
I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.
I tried using sed like:cat file.txt | sed -r "s/ /, /g"
But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','
If I use cat file.txt | sed -r "s/[0123456789] /, /g"
, I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.
How do I do this?
sed
sed
edited Sep 27 at 18:58
GAD3R
23.2k164896
23.2k164896
asked Sep 27 at 16:10
scipsycho
282
282
1
@Hello scipsycho. Please see below. is this what you want?
â Goro
Sep 27 at 16:14
Just a heads-up: It won't compile even with commas.08
and09
are not valid integer literals.
â pipe
Sep 28 at 1:58
@pipe you are right! but now that you guys have answered this question, I will docat file.txt | sed -r 's/([{, ])0+([0-9])+/ 1 2/g'
which will remove any zeros occurring in the units place
â scipsycho
Sep 28 at 2:48
add a comment |Â
1
@Hello scipsycho. Please see below. is this what you want?
â Goro
Sep 27 at 16:14
Just a heads-up: It won't compile even with commas.08
and09
are not valid integer literals.
â pipe
Sep 28 at 1:58
@pipe you are right! but now that you guys have answered this question, I will docat file.txt | sed -r 's/([{, ])0+([0-9])+/ 1 2/g'
which will remove any zeros occurring in the units place
â scipsycho
Sep 28 at 2:48
1
1
@Hello scipsycho. Please see below. is this what you want?
â Goro
Sep 27 at 16:14
@Hello scipsycho. Please see below. is this what you want?
â Goro
Sep 27 at 16:14
Just a heads-up: It won't compile even with commas.
08
and 09
are not valid integer literals.â pipe
Sep 28 at 1:58
Just a heads-up: It won't compile even with commas.
08
and 09
are not valid integer literals.â pipe
Sep 28 at 1:58
@pipe you are right! but now that you guys have answered this question, I will do
cat file.txt | sed -r 's/([{, ])0+([0-9])+/ 1 2/g'
which will remove any zeros occurring in the units placeâ scipsycho
Sep 28 at 2:48
@pipe you are right! but now that you guys have answered this question, I will do
cat file.txt | sed -r 's/([{, ])0+([0-9])+/ 1 2/g'
which will remove any zeros occurring in the units placeâ scipsycho
Sep 28 at 2:48
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
11
down vote
accepted
cat file.txt | sed -r 's/([0-9]+)/1,/g'
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,
Explanation:
First capturing group ([0-9]+)
Match a single character (i.e. number) present in the table [0-9]+
+ Quantifier â Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.
1
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
1
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
2
The1
is called a "back reference" in thesed
documentation.
â RobertL
Sep 27 at 16:50
2
sed
can read files.
â RudiC
Sep 27 at 21:24
 |Â
show 2 more comments
up vote
2
down vote
You can just replace a space followed by any number of spaces by a comma:
sed 's/ */,/g' file
(if the spaces at the start of some lines are just a copy paste error)
add a comment |Â
up vote
2
down vote
How about
sed 's/ +/, /g' file
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03
add a comment |Â
up vote
1
down vote
This perl command will add a comma in between a digit and a space
perl -pe 's/(?<=d)(?=s)/,/g' file
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
cat file.txt | sed -r 's/([0-9]+)/1,/g'
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,
Explanation:
First capturing group ([0-9]+)
Match a single character (i.e. number) present in the table [0-9]+
+ Quantifier â Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.
1
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
1
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
2
The1
is called a "back reference" in thesed
documentation.
â RobertL
Sep 27 at 16:50
2
sed
can read files.
â RudiC
Sep 27 at 21:24
 |Â
show 2 more comments
up vote
11
down vote
accepted
cat file.txt | sed -r 's/([0-9]+)/1,/g'
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,
Explanation:
First capturing group ([0-9]+)
Match a single character (i.e. number) present in the table [0-9]+
+ Quantifier â Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.
1
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
1
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
2
The1
is called a "back reference" in thesed
documentation.
â RobertL
Sep 27 at 16:50
2
sed
can read files.
â RudiC
Sep 27 at 21:24
 |Â
show 2 more comments
up vote
11
down vote
accepted
up vote
11
down vote
accepted
cat file.txt | sed -r 's/([0-9]+)/1,/g'
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,
Explanation:
First capturing group ([0-9]+)
Match a single character (i.e. number) present in the table [0-9]+
+ Quantifier â Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.
cat file.txt | sed -r 's/([0-9]+)/1,/g'
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,
Explanation:
First capturing group ([0-9]+)
Match a single character (i.e. number) present in the table [0-9]+
+ Quantifier â Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.
edited Sep 27 at 16:51
answered Sep 27 at 16:14
Goro
6,67752865
6,67752865
1
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
1
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
2
The1
is called a "back reference" in thesed
documentation.
â RobertL
Sep 27 at 16:50
2
sed
can read files.
â RudiC
Sep 27 at 21:24
 |Â
show 2 more comments
1
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
1
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
2
The1
is called a "back reference" in thesed
documentation.
â RobertL
Sep 27 at 16:50
2
sed
can read files.
â RudiC
Sep 27 at 21:24
1
1
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Thanks! it worked! What does this '1' mean?
â scipsycho
Sep 27 at 16:27
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
â Goro
Sep 27 at 16:34
1
1
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
Hi @Goro, thanks a lot for this explanation.
â scipsycho
Sep 27 at 16:49
2
2
The
1
is called a "back reference" in the sed
documentation.â RobertL
Sep 27 at 16:50
The
1
is called a "back reference" in the sed
documentation.â RobertL
Sep 27 at 16:50
2
2
sed
can read files.â RudiC
Sep 27 at 21:24
sed
can read files.â RudiC
Sep 27 at 21:24
 |Â
show 2 more comments
up vote
2
down vote
You can just replace a space followed by any number of spaces by a comma:
sed 's/ */,/g' file
(if the spaces at the start of some lines are just a copy paste error)
add a comment |Â
up vote
2
down vote
You can just replace a space followed by any number of spaces by a comma:
sed 's/ */,/g' file
(if the spaces at the start of some lines are just a copy paste error)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can just replace a space followed by any number of spaces by a comma:
sed 's/ */,/g' file
(if the spaces at the start of some lines are just a copy paste error)
You can just replace a space followed by any number of spaces by a comma:
sed 's/ */,/g' file
(if the spaces at the start of some lines are just a copy paste error)
answered Sep 27 at 16:18
choroba
25k34168
25k34168
add a comment |Â
add a comment |Â
up vote
2
down vote
How about
sed 's/ +/, /g' file
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03
add a comment |Â
up vote
2
down vote
How about
sed 's/ +/, /g' file
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03
add a comment |Â
up vote
2
down vote
up vote
2
down vote
How about
sed 's/ +/, /g' file
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03
How about
sed 's/ +/, /g' file
02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03
answered Sep 27 at 16:24
RudiC
1,8329
1,8329
add a comment |Â
add a comment |Â
up vote
1
down vote
This perl command will add a comma in between a digit and a space
perl -pe 's/(?<=d)(?=s)/,/g' file
add a comment |Â
up vote
1
down vote
This perl command will add a comma in between a digit and a space
perl -pe 's/(?<=d)(?=s)/,/g' file
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This perl command will add a comma in between a digit and a space
perl -pe 's/(?<=d)(?=s)/,/g' file
This perl command will add a comma in between a digit and a space
perl -pe 's/(?<=d)(?=s)/,/g' file
answered Sep 27 at 17:14
glenn jackman
48.3k365105
48.3k365105
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%2f471875%2fchange-only-part-of-the-substring-using-sed%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
1
@Hello scipsycho. Please see below. is this what you want?
â Goro
Sep 27 at 16:14
Just a heads-up: It won't compile even with commas.
08
and09
are not valid integer literals.â pipe
Sep 28 at 1:58
@pipe you are right! but now that you guys have answered this question, I will do
cat file.txt | sed -r 's/([{, ])0+([0-9])+/ 1 2/g'
which will remove any zeros occurring in the units placeâ scipsycho
Sep 28 at 2:48