Skip to main content
Added last paragraph
Source Link

I am new to regex and sed, and am trying to create what I thought would be a straightforward regex: I want to remove word-final letter if it's an 'o'.

  • Input string: Hello Hello
  • Expected output: Hell Hell

The good news: I can remove the 'o' when it is at the end of the string:

$ echo 'Hello Hello' |sed 's/\(.*\)o/\1/g'
Hello Hell
$ echo 'Hello Hello' |sed 's/\(.*\)o$/\1/g'
Hello Hell

The bad news: I cannot remove it from words earlier in the string. I have tried this with all the anchor symbols I can think of. The result is that none of the word-final 'o's is removed:

$ echo 'Hello Hello' |sed 's/\(.*\)o\b/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\>/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\W/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\s/\1/g'
Hello Hello

Can you please help me regain my sanity by telling me what I'm doing wrong?

Update: I get the dictinct impression that my machine produces different results than some other people's. I am using the terminal window on my Macbook. If anyone can shed some light on this, please tell me.

I am new to regex and sed, and am trying to create what I thought would be a straightforward regex: I want to remove word-final letter if it's an 'o'.

  • Input string: Hello Hello
  • Expected output: Hell Hell

The good news: I can remove the 'o' when it is at the end of the string:

$ echo 'Hello Hello' |sed 's/\(.*\)o/\1/g'
Hello Hell
$ echo 'Hello Hello' |sed 's/\(.*\)o$/\1/g'
Hello Hell

The bad news: I cannot remove it from words earlier in the string. I have tried this with all the anchor symbols I can think of. The result is that none of the word-final 'o's is removed:

$ echo 'Hello Hello' |sed 's/\(.*\)o\b/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\>/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\W/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\s/\1/g'
Hello Hello

Can you please help me regain my sanity by telling me what I'm doing wrong?

I am new to regex and sed, and am trying to create what I thought would be a straightforward regex: I want to remove word-final letter if it's an 'o'.

  • Input string: Hello Hello
  • Expected output: Hell Hell

The good news: I can remove the 'o' when it is at the end of the string:

$ echo 'Hello Hello' |sed 's/\(.*\)o/\1/g'
Hello Hell
$ echo 'Hello Hello' |sed 's/\(.*\)o$/\1/g'
Hello Hell

The bad news: I cannot remove it from words earlier in the string. I have tried this with all the anchor symbols I can think of. The result is that none of the word-final 'o's is removed:

$ echo 'Hello Hello' |sed 's/\(.*\)o\b/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\>/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\W/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\s/\1/g'
Hello Hello

Can you please help me regain my sanity by telling me what I'm doing wrong?

Update: I get the dictinct impression that my machine produces different results than some other people's. I am using the terminal window on my Macbook. If anyone can shed some light on this, please tell me.

Source Link

Trying to delete a word-final characters with sed regex

I am new to regex and sed, and am trying to create what I thought would be a straightforward regex: I want to remove word-final letter if it's an 'o'.

  • Input string: Hello Hello
  • Expected output: Hell Hell

The good news: I can remove the 'o' when it is at the end of the string:

$ echo 'Hello Hello' |sed 's/\(.*\)o/\1/g'
Hello Hell
$ echo 'Hello Hello' |sed 's/\(.*\)o$/\1/g'
Hello Hell

The bad news: I cannot remove it from words earlier in the string. I have tried this with all the anchor symbols I can think of. The result is that none of the word-final 'o's is removed:

$ echo 'Hello Hello' |sed 's/\(.*\)o\b/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\>/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\W/\1/g'
Hello Hello
$ echo 'Hello Hello' |sed 's/\(.*\)o\s/\1/g'
Hello Hello

Can you please help me regain my sanity by telling me what I'm doing wrong?