Sed Match Exact

sed replace exact match

I believe \< and \> work with gnu sed, you just need to quote the sed command:

sed -i.bak 's/\<sample_name\>/sample_01/g' file

Sed match exact

I'm not sure what the regex you are using is, but if you want an exact match of '192.168.0.11' you can use:
s/\<192\.168\.0\.11\>//g

The \<\> force an exact match.

Unable to use sed on Mac for exact match

What you've asked sed to match is <he> - so of course it doesn't update (that's because OSX sed isn't GNU sed).

If you want to replace he on it's own or "he" you'll need to match quite a few different cases (to make this work generally and not grab a he in a word).

  • \she\s*$ ie. Line ends with he
  • ^\s*he\s ie. Line starts with he
  • \she\s ie. A he as a separate word
  • "he" ie. "he"

Someone else suggested use [[:<:]] character class instead of \< - that works nicely on OSX ...

# ( echo "Cd    "; echo "       Cd"; echo "ABCdFg CdFg Cd \"Cd\""; echo "Ab Cd EfG") | \
sed -e 's/[[:<:]]Cd[[:>:]]/FOO/g'
FOO
FOO
ABCdFg CdFg FOO "FOO"
AB'FOO
Ab FOO EfG

replace an exact match for a double underscore with a string using sed

If you have no overlapping matches (and your provided input has none), a sed like this will do:

sed -E  's/([^_]|^)__([^_]|$)/\1.abc.def__\2/g' file > newfile

Here, ([^_]|^)__([^_]|$) matches and captures into Group 1 (\1) any char other than _ or start of string (([^_]|^)), then matches __, and then captures into Group 2 (\2) any char other than _ or end of string (([^_]|$)).

If there can be overlapping matches, sed becomes rather difficult to use here. A perfect alternative would be using

perl -pe 's/(?<!_)__(?!_)/.abc.def__/g' file > newfile
perl -i -pe 's/(?<!_)__(?!_)/.abc.def__/g' file

The (?<!_)__(?!_) regex contains two lookarounds, (?<!_) negative lookbehind that makes sure there is no _ char immediately to the left of the current location, and (?!_) negative lookahead makes sure there is no _ char immediately to the right of the current location.

See the online demo:

#!/bin/bash
s='AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012__386___Day_'

sed -E 's/([^_]|^)__([^_]|$)/\1.abc.def__\2/g' <<< "$s"
# => AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012.abc.def__386___Day_
perl -i -pe 's/(?<!_)__(?!_)/.abc.def__/g' <<< "$s"
# => AFM_7499_190512_110136_001_p_EQ4H_1_s60_0012.abc.def__386___Day_

sed regex to match exact pattern and ignore some pattern

You may use this sed:

sed -E 's/([: ]+)([0-9]+) *$/\1"\2"/' file.yml

- name: HEALTH_ELASTIC_HOST
value: 40c07d4283d245.elastic.test.com
- name: HEALTH_ELASTIC_PORT
value: "9243"
- name: HEALTH_ELASTIC_USERNAME
value: elastic

Explanation:

  • ([: ]+): Match 1+ of : or space and capture in group #2
  • ([0-9]+): Match 1+ digits an capture in group #2
  • *: Match 0 or more spaces
  • $: match end position
  • \1"\2": In substitution we wrap back-reference #2 with double quotes

“sed” command to remove a line that matches an exact string on first word

The . is metacharacter in regex which means "Match any one character". So you accidentally created a regex that will also catch cnnPcom or cnn com or cnn\com. While it probably works for your needs, it would be better to be more explicit:

  sed -r '/^cnn\.com\b/d' raw.txt 

The difference here is the \ backslash before the . period. That escapes the period metacharacter so it's treated as a literal period.


As for your lines that start with a space, you can catch those in a single regex (Again escaping the period metacharacter):

  sed -r '/(^[ ]*|^)127\.0\.0\.1\b/d' raw.txt

This (^[ ]*|^) says a line that starts with any number of repeating spaces ^[ ]* OR | starts with ^ which is then followed by your match for 127.0.0.1.


And then for stringing these together you can use the | OR operator inside of parantheses to catch all of your matches:

  sed -r '/(^[ ]*|^)(127\.0\.0\.1|cnn\.com|0\.0\.0\.0)\b/d' raw.txt

Alternatively you can use a ; semicolon to separate out the different regexes:

  sed -r '/(^[ ]*|^)127\.0\.0\.1\b/d; /(^[ ]*|^)cnn\.com\b/d; /(^[ ]*|^)0\.0\.0\.0\b/d;' raw.txt


Related Topics



Leave a reply



Submit