How can I replace / with . in a file using sed
Clash Royale CLAN TAG#URR8PPP
I am trying to figure out how to replace the '/' in a date (20/01/1990) with '.' (20.01.1990). I do understand to do the whole file is 's///./g'
, but there are urls in the file so it replaces those '/' with '.' which is not what I need. Is there any way to make it apply to only certain columns? I say there is a way with perl but I am working in bash
text-processing sed
add a comment |
I am trying to figure out how to replace the '/' in a date (20/01/1990) with '.' (20.01.1990). I do understand to do the whole file is 's///./g'
, but there are urls in the file so it replaces those '/' with '.' which is not what I need. Is there any way to make it apply to only certain columns? I say there is a way with perl but I am working in bash
text-processing sed
add a comment |
I am trying to figure out how to replace the '/' in a date (20/01/1990) with '.' (20.01.1990). I do understand to do the whole file is 's///./g'
, but there are urls in the file so it replaces those '/' with '.' which is not what I need. Is there any way to make it apply to only certain columns? I say there is a way with perl but I am working in bash
text-processing sed
I am trying to figure out how to replace the '/' in a date (20/01/1990) with '.' (20.01.1990). I do understand to do the whole file is 's///./g'
, but there are urls in the file so it replaces those '/' with '.' which is not what I need. Is there any way to make it apply to only certain columns? I say there is a way with perl but I am working in bash
text-processing sed
text-processing sed
edited Feb 7 at 10:52
Stéphane Chazelas
308k57581939
308k57581939
asked Feb 7 at 4:28
AnythingHelpsHereAnythingHelpsHere
61
61
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Just use a different delimiter that's not part of your line.
echo 'http://www.google.com (20/01/1990)' | sed -r 's@([0-9]+)/([0-9]+)/([0-9]+)@1.2.3@g'
http://www.google.com (20.01.1990)
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
1
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Depending on your version ofsed
(and OS) you may needsed -E
instead ofsed -r
.
– nohillside
Feb 7 at 10:55
add a comment |
bash
is a shell, a command line interpreter. It's role is to run commands. sed
and perl
are two commands that bash
or any other shell can run.
Both happen to be interpreters of some programming language, but that's not relevant here. Both are great at text processing as they have been designed for that. sed
is a standard command but there are many different implementations which are incompatible if you stray outside of what POSIX specifies, perl
is not but there is only one implementations (though many different versions with different feature level).
Here, using standard sed
syntax, you can do:
sed 's|([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)|1.2.3|g'
Which should work regardless of the sed
implementation provided it's POSIX compliant within the limits specified by POSIX (in this case, as long as the input is valid text, which in practice excludes very long lines, sequences of bytes not forming valid characters or non-delimited lines, though some implementations like GNU sed
may accept those as an extension).
With any version of perl
and any input, you can also write it:
perl -pe 's|(dd)/(dd)/(d4)|$1.$2.$3|g'
Or with recent versions or perl
:
perl -pe 'sdd/dd/d4$&=~yge'
If you want to avoid replacing 1000/10/99999
with 1000.10.99999
, you can use word boundary operators which are available in perl
, but not in standard sed
(though some implementations support <
/>
or [[:<:]]
/[[:>:]]
for that as an extension):
perl -pe 's|b(dd)/(dd)/(d4)b|$1.$2.$3|g'
Or look-around operators (again, a feature of perl
regexps, but not standard sed regexps).
perl -pe 's|(?<!d)(dd)/(dd)/(d4)(?!d)|$1.$2.$3|g'
You can do something equivalent with standard sed
with a bit of programming (sed
's conditional looping construct):
sed -e :1 -e 's|^(.*[^[:digit:]])0,1([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)([^[:digit:]].*)0,1$|12.3.45|g; t1'
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499217%2fhow-can-i-replace-with-in-a-file-using-sed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just use a different delimiter that's not part of your line.
echo 'http://www.google.com (20/01/1990)' | sed -r 's@([0-9]+)/([0-9]+)/([0-9]+)@1.2.3@g'
http://www.google.com (20.01.1990)
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
1
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Depending on your version ofsed
(and OS) you may needsed -E
instead ofsed -r
.
– nohillside
Feb 7 at 10:55
add a comment |
Just use a different delimiter that's not part of your line.
echo 'http://www.google.com (20/01/1990)' | sed -r 's@([0-9]+)/([0-9]+)/([0-9]+)@1.2.3@g'
http://www.google.com (20.01.1990)
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
1
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Depending on your version ofsed
(and OS) you may needsed -E
instead ofsed -r
.
– nohillside
Feb 7 at 10:55
add a comment |
Just use a different delimiter that's not part of your line.
echo 'http://www.google.com (20/01/1990)' | sed -r 's@([0-9]+)/([0-9]+)/([0-9]+)@1.2.3@g'
http://www.google.com (20.01.1990)
Just use a different delimiter that's not part of your line.
echo 'http://www.google.com (20/01/1990)' | sed -r 's@([0-9]+)/([0-9]+)/([0-9]+)@1.2.3@g'
http://www.google.com (20.01.1990)
edited Feb 7 at 4:35
answered Feb 7 at 4:29
tinktink
4,49711221
4,49711221
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
1
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Depending on your version ofsed
(and OS) you may needsed -E
instead ofsed -r
.
– nohillside
Feb 7 at 10:55
add a comment |
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
1
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Depending on your version ofsed
(and OS) you may needsed -E
instead ofsed -r
.
– nohillside
Feb 7 at 10:55
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
When I try and run this on my .sed file I get the error: invalid reference 3 on `s' command's RHS
– AnythingHelpsHere
Feb 7 at 5:34
1
1
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Check your spelling and typing of the command.
– Kusalananda
Feb 7 at 6:35
@AnythingHelpsHere Depending on your version of
sed
(and OS) you may need sed -E
instead of sed -r
.– nohillside
Feb 7 at 10:55
@AnythingHelpsHere Depending on your version of
sed
(and OS) you may need sed -E
instead of sed -r
.– nohillside
Feb 7 at 10:55
add a comment |
bash
is a shell, a command line interpreter. It's role is to run commands. sed
and perl
are two commands that bash
or any other shell can run.
Both happen to be interpreters of some programming language, but that's not relevant here. Both are great at text processing as they have been designed for that. sed
is a standard command but there are many different implementations which are incompatible if you stray outside of what POSIX specifies, perl
is not but there is only one implementations (though many different versions with different feature level).
Here, using standard sed
syntax, you can do:
sed 's|([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)|1.2.3|g'
Which should work regardless of the sed
implementation provided it's POSIX compliant within the limits specified by POSIX (in this case, as long as the input is valid text, which in practice excludes very long lines, sequences of bytes not forming valid characters or non-delimited lines, though some implementations like GNU sed
may accept those as an extension).
With any version of perl
and any input, you can also write it:
perl -pe 's|(dd)/(dd)/(d4)|$1.$2.$3|g'
Or with recent versions or perl
:
perl -pe 'sdd/dd/d4$&=~yge'
If you want to avoid replacing 1000/10/99999
with 1000.10.99999
, you can use word boundary operators which are available in perl
, but not in standard sed
(though some implementations support <
/>
or [[:<:]]
/[[:>:]]
for that as an extension):
perl -pe 's|b(dd)/(dd)/(d4)b|$1.$2.$3|g'
Or look-around operators (again, a feature of perl
regexps, but not standard sed regexps).
perl -pe 's|(?<!d)(dd)/(dd)/(d4)(?!d)|$1.$2.$3|g'
You can do something equivalent with standard sed
with a bit of programming (sed
's conditional looping construct):
sed -e :1 -e 's|^(.*[^[:digit:]])0,1([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)([^[:digit:]].*)0,1$|12.3.45|g; t1'
add a comment |
bash
is a shell, a command line interpreter. It's role is to run commands. sed
and perl
are two commands that bash
or any other shell can run.
Both happen to be interpreters of some programming language, but that's not relevant here. Both are great at text processing as they have been designed for that. sed
is a standard command but there are many different implementations which are incompatible if you stray outside of what POSIX specifies, perl
is not but there is only one implementations (though many different versions with different feature level).
Here, using standard sed
syntax, you can do:
sed 's|([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)|1.2.3|g'
Which should work regardless of the sed
implementation provided it's POSIX compliant within the limits specified by POSIX (in this case, as long as the input is valid text, which in practice excludes very long lines, sequences of bytes not forming valid characters or non-delimited lines, though some implementations like GNU sed
may accept those as an extension).
With any version of perl
and any input, you can also write it:
perl -pe 's|(dd)/(dd)/(d4)|$1.$2.$3|g'
Or with recent versions or perl
:
perl -pe 'sdd/dd/d4$&=~yge'
If you want to avoid replacing 1000/10/99999
with 1000.10.99999
, you can use word boundary operators which are available in perl
, but not in standard sed
(though some implementations support <
/>
or [[:<:]]
/[[:>:]]
for that as an extension):
perl -pe 's|b(dd)/(dd)/(d4)b|$1.$2.$3|g'
Or look-around operators (again, a feature of perl
regexps, but not standard sed regexps).
perl -pe 's|(?<!d)(dd)/(dd)/(d4)(?!d)|$1.$2.$3|g'
You can do something equivalent with standard sed
with a bit of programming (sed
's conditional looping construct):
sed -e :1 -e 's|^(.*[^[:digit:]])0,1([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)([^[:digit:]].*)0,1$|12.3.45|g; t1'
add a comment |
bash
is a shell, a command line interpreter. It's role is to run commands. sed
and perl
are two commands that bash
or any other shell can run.
Both happen to be interpreters of some programming language, but that's not relevant here. Both are great at text processing as they have been designed for that. sed
is a standard command but there are many different implementations which are incompatible if you stray outside of what POSIX specifies, perl
is not but there is only one implementations (though many different versions with different feature level).
Here, using standard sed
syntax, you can do:
sed 's|([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)|1.2.3|g'
Which should work regardless of the sed
implementation provided it's POSIX compliant within the limits specified by POSIX (in this case, as long as the input is valid text, which in practice excludes very long lines, sequences of bytes not forming valid characters or non-delimited lines, though some implementations like GNU sed
may accept those as an extension).
With any version of perl
and any input, you can also write it:
perl -pe 's|(dd)/(dd)/(d4)|$1.$2.$3|g'
Or with recent versions or perl
:
perl -pe 'sdd/dd/d4$&=~yge'
If you want to avoid replacing 1000/10/99999
with 1000.10.99999
, you can use word boundary operators which are available in perl
, but not in standard sed
(though some implementations support <
/>
or [[:<:]]
/[[:>:]]
for that as an extension):
perl -pe 's|b(dd)/(dd)/(d4)b|$1.$2.$3|g'
Or look-around operators (again, a feature of perl
regexps, but not standard sed regexps).
perl -pe 's|(?<!d)(dd)/(dd)/(d4)(?!d)|$1.$2.$3|g'
You can do something equivalent with standard sed
with a bit of programming (sed
's conditional looping construct):
sed -e :1 -e 's|^(.*[^[:digit:]])0,1([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)([^[:digit:]].*)0,1$|12.3.45|g; t1'
bash
is a shell, a command line interpreter. It's role is to run commands. sed
and perl
are two commands that bash
or any other shell can run.
Both happen to be interpreters of some programming language, but that's not relevant here. Both are great at text processing as they have been designed for that. sed
is a standard command but there are many different implementations which are incompatible if you stray outside of what POSIX specifies, perl
is not but there is only one implementations (though many different versions with different feature level).
Here, using standard sed
syntax, you can do:
sed 's|([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)|1.2.3|g'
Which should work regardless of the sed
implementation provided it's POSIX compliant within the limits specified by POSIX (in this case, as long as the input is valid text, which in practice excludes very long lines, sequences of bytes not forming valid characters or non-delimited lines, though some implementations like GNU sed
may accept those as an extension).
With any version of perl
and any input, you can also write it:
perl -pe 's|(dd)/(dd)/(d4)|$1.$2.$3|g'
Or with recent versions or perl
:
perl -pe 'sdd/dd/d4$&=~yge'
If you want to avoid replacing 1000/10/99999
with 1000.10.99999
, you can use word boundary operators which are available in perl
, but not in standard sed
(though some implementations support <
/>
or [[:<:]]
/[[:>:]]
for that as an extension):
perl -pe 's|b(dd)/(dd)/(d4)b|$1.$2.$3|g'
Or look-around operators (again, a feature of perl
regexps, but not standard sed regexps).
perl -pe 's|(?<!d)(dd)/(dd)/(d4)(?!d)|$1.$2.$3|g'
You can do something equivalent with standard sed
with a bit of programming (sed
's conditional looping construct):
sed -e :1 -e 's|^(.*[^[:digit:]])0,1([[:digit:]]2)/([[:digit:]]2)/([[:digit:]]4)([^[:digit:]].*)0,1$|12.3.45|g; t1'
edited Feb 7 at 11:17
answered Feb 7 at 11:11
Stéphane ChazelasStéphane Chazelas
308k57581939
308k57581939
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f499217%2fhow-can-i-replace-with-in-a-file-using-sed%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown