Remove occurrence of a pattern from string everywhere [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
How can I replace a string in a file(s)?
7 answers
I want to remove occurrence of a string from a given string. Like /hello:
from a string like /yo:/sup:/hello:/yo:/hello:/yup
.
And one thing for sure if string contains a colon in end then it must be removed.
There can be anything between /hello
and colon (:
) so I want to strip all from /hello
to first colon (:
).
text-processing sed
marked as duplicate by Jesse_b, DopeGhoti, Mr Shunz, Jeff Schaller, Christopher Feb 15 at 18:19
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 |
This question already has an answer here:
How can I replace a string in a file(s)?
7 answers
I want to remove occurrence of a string from a given string. Like /hello:
from a string like /yo:/sup:/hello:/yo:/hello:/yup
.
And one thing for sure if string contains a colon in end then it must be removed.
There can be anything between /hello
and colon (:
) so I want to strip all from /hello
to first colon (:
).
text-processing sed
marked as duplicate by Jesse_b, DopeGhoti, Mr Shunz, Jeff Schaller, Christopher Feb 15 at 18:19
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.
actually there can be anything between /hello and : colon so I want to strip string from /hello to first match of colon
– XeXkek
Feb 15 at 15:32
1
Please show more examples of input that would demonstrate the various cases.
– glenn jackman
Feb 15 at 15:59
1
@XeXkek Welcome on U&L! Please, add all your requirements to your question (instead of adding them as comment to answers). It will make easier for other users to help you.
– fra-san
Feb 15 at 16:07
Is this i string embedded along with other text in a file, or do you have the string in a shell variable? Where is this string?
– Kusalananda
Feb 15 at 17:35
Yes in a shell variable called PATH
– XeXkek
Feb 15 at 18:27
add a comment |
This question already has an answer here:
How can I replace a string in a file(s)?
7 answers
I want to remove occurrence of a string from a given string. Like /hello:
from a string like /yo:/sup:/hello:/yo:/hello:/yup
.
And one thing for sure if string contains a colon in end then it must be removed.
There can be anything between /hello
and colon (:
) so I want to strip all from /hello
to first colon (:
).
text-processing sed
This question already has an answer here:
How can I replace a string in a file(s)?
7 answers
I want to remove occurrence of a string from a given string. Like /hello:
from a string like /yo:/sup:/hello:/yo:/hello:/yup
.
And one thing for sure if string contains a colon in end then it must be removed.
There can be anything between /hello
and colon (:
) so I want to strip all from /hello
to first colon (:
).
This question already has an answer here:
How can I replace a string in a file(s)?
7 answers
text-processing sed
text-processing sed
edited Feb 15 at 16:03
fra-san
1,8531519
1,8531519
asked Feb 15 at 15:28
XeXkekXeXkek
11
11
marked as duplicate by Jesse_b, DopeGhoti, Mr Shunz, Jeff Schaller, Christopher Feb 15 at 18:19
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 Jesse_b, DopeGhoti, Mr Shunz, Jeff Schaller, Christopher Feb 15 at 18:19
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.
actually there can be anything between /hello and : colon so I want to strip string from /hello to first match of colon
– XeXkek
Feb 15 at 15:32
1
Please show more examples of input that would demonstrate the various cases.
– glenn jackman
Feb 15 at 15:59
1
@XeXkek Welcome on U&L! Please, add all your requirements to your question (instead of adding them as comment to answers). It will make easier for other users to help you.
– fra-san
Feb 15 at 16:07
Is this i string embedded along with other text in a file, or do you have the string in a shell variable? Where is this string?
– Kusalananda
Feb 15 at 17:35
Yes in a shell variable called PATH
– XeXkek
Feb 15 at 18:27
add a comment |
actually there can be anything between /hello and : colon so I want to strip string from /hello to first match of colon
– XeXkek
Feb 15 at 15:32
1
Please show more examples of input that would demonstrate the various cases.
– glenn jackman
Feb 15 at 15:59
1
@XeXkek Welcome on U&L! Please, add all your requirements to your question (instead of adding them as comment to answers). It will make easier for other users to help you.
– fra-san
Feb 15 at 16:07
Is this i string embedded along with other text in a file, or do you have the string in a shell variable? Where is this string?
– Kusalananda
Feb 15 at 17:35
Yes in a shell variable called PATH
– XeXkek
Feb 15 at 18:27
actually there can be anything between /hello and : colon so I want to strip string from /hello to first match of colon
– XeXkek
Feb 15 at 15:32
actually there can be anything between /hello and : colon so I want to strip string from /hello to first match of colon
– XeXkek
Feb 15 at 15:32
1
1
Please show more examples of input that would demonstrate the various cases.
– glenn jackman
Feb 15 at 15:59
Please show more examples of input that would demonstrate the various cases.
– glenn jackman
Feb 15 at 15:59
1
1
@XeXkek Welcome on U&L! Please, add all your requirements to your question (instead of adding them as comment to answers). It will make easier for other users to help you.
– fra-san
Feb 15 at 16:07
@XeXkek Welcome on U&L! Please, add all your requirements to your question (instead of adding them as comment to answers). It will make easier for other users to help you.
– fra-san
Feb 15 at 16:07
Is this i string embedded along with other text in a file, or do you have the string in a shell variable? Where is this string?
– Kusalananda
Feb 15 at 17:35
Is this i string embedded along with other text in a file, or do you have the string in a shell variable? Where is this string?
– Kusalananda
Feb 15 at 17:35
Yes in a shell variable called PATH
– XeXkek
Feb 15 at 18:27
Yes in a shell variable called PATH
– XeXkek
Feb 15 at 18:27
add a comment |
2 Answers
2
active
oldest
votes
With sed
:
sed --in-place 's_/hello[^:]*:__g' inputfile
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Unable to replicate:echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields/yo:/sup:/yo:/yup
.
– DopeGhoti
Feb 15 at 15:37
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
add a comment |
Perl in-line version:
perl -pi -e "s//hello//g" /path/to/file
or
perl -pi -e "s//hello://g" /path/to/file
If you want to remove the trailing : also.
It's not file it's a string
– XeXkek
Feb 15 at 18:04
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With sed
:
sed --in-place 's_/hello[^:]*:__g' inputfile
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Unable to replicate:echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields/yo:/sup:/yo:/yup
.
– DopeGhoti
Feb 15 at 15:37
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
add a comment |
With sed
:
sed --in-place 's_/hello[^:]*:__g' inputfile
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Unable to replicate:echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields/yo:/sup:/yo:/yup
.
– DopeGhoti
Feb 15 at 15:37
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
add a comment |
With sed
:
sed --in-place 's_/hello[^:]*:__g' inputfile
With sed
:
sed --in-place 's_/hello[^:]*:__g' inputfile
answered Feb 15 at 15:31
DopeGhotiDopeGhoti
46.1k56089
46.1k56089
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Unable to replicate:echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields/yo:/sup:/yo:/yup
.
– DopeGhoti
Feb 15 at 15:37
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
add a comment |
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Unable to replicate:echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields/yo:/sup:/yo:/yup
.
– DopeGhoti
Feb 15 at 15:37
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Bro it'll strips down string to last colon not only /helloblabal: I must remove till first match of colon if it is available
– XeXkek
Feb 15 at 15:35
Unable to replicate:
echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields /yo:/sup:/yo:/yup
.– DopeGhoti
Feb 15 at 15:37
Unable to replicate:
echo "/yo:/sup:/hello:/yo:/hello:/yup" | sed 's_/hello[^:]*:__g'
yields /yo:/sup:/yo:/yup
.– DopeGhoti
Feb 15 at 15:37
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Also can colon be optional on first match?
– XeXkek
Feb 15 at 15:42
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
Making the rules for one match different from the rules for the rest of the matches means you're no longer talking about a regular expression, which means it's rather difficult to do as a quick one-off.
– DopeGhoti
Feb 15 at 15:57
add a comment |
Perl in-line version:
perl -pi -e "s//hello//g" /path/to/file
or
perl -pi -e "s//hello://g" /path/to/file
If you want to remove the trailing : also.
It's not file it's a string
– XeXkek
Feb 15 at 18:04
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
add a comment |
Perl in-line version:
perl -pi -e "s//hello//g" /path/to/file
or
perl -pi -e "s//hello://g" /path/to/file
If you want to remove the trailing : also.
It's not file it's a string
– XeXkek
Feb 15 at 18:04
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
add a comment |
Perl in-line version:
perl -pi -e "s//hello//g" /path/to/file
or
perl -pi -e "s//hello://g" /path/to/file
If you want to remove the trailing : also.
Perl in-line version:
perl -pi -e "s//hello//g" /path/to/file
or
perl -pi -e "s//hello://g" /path/to/file
If you want to remove the trailing : also.
answered Feb 15 at 16:00
marc hurleymarc hurley
1
1
It's not file it's a string
– XeXkek
Feb 15 at 18:04
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
add a comment |
It's not file it's a string
– XeXkek
Feb 15 at 18:04
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
It's not file it's a string
– XeXkek
Feb 15 at 18:04
It's not file it's a string
– XeXkek
Feb 15 at 18:04
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
echo /yo:/sup:/hello:/yo:/hello:/yup | perl -p -e 's//hello://g'
– marc hurley
Feb 18 at 9:23
add a comment |
actually there can be anything between /hello and : colon so I want to strip string from /hello to first match of colon
– XeXkek
Feb 15 at 15:32
1
Please show more examples of input that would demonstrate the various cases.
– glenn jackman
Feb 15 at 15:59
1
@XeXkek Welcome on U&L! Please, add all your requirements to your question (instead of adding them as comment to answers). It will make easier for other users to help you.
– fra-san
Feb 15 at 16:07
Is this i string embedded along with other text in a file, or do you have the string in a shell variable? Where is this string?
– Kusalananda
Feb 15 at 17:35
Yes in a shell variable called PATH
– XeXkek
Feb 15 at 18:27