0

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 (:).

fra-san
  • 10,205
  • 2
  • 22
  • 43
XeXkek
  • 1
  • 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 '19 at 15:32
  • 1
    Please show more examples of input that would demonstrate the various cases. – glenn jackman Feb 15 '19 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 '19 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 '19 at 17:35
  • Yes in a shell variable called PATH – XeXkek Feb 15 '19 at 18:27

2 Answers2

0

With sed:

sed --in-place 's_/hello[^:]*:__g' inputfile
DopeGhoti
  • 76,081
  • 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 '19 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 '19 at 15:37
  • Also can colon be optional on first match? – XeXkek Feb 15 '19 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 '19 at 15:57
0

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.