• Uncategorized

About regex : Sed-replace-asterisk-symbols

Question Detail

I’m am trying to replace a series of asterix symbols in a text file with a -999.9 using sed. However I can’t figure out how to properly escape the wildcard symbol.

e.g.

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/************/-999.9/g’
sed: 1: “s/************/-999.9/g”: RE error: repetition-operator operand invalid

Doesn’t work. And

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/[************]/-999.9/g’
2006.0,1.0,-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9-999.9,-5.0

puts a -999.9 for every * which isn’t what I intended either.

Thanks!

Question Answer

Use this:

echo “2006.0,1.0,************,-5.0” | sed ‘s/[*]\+/-999.9/g’

Test:

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/[*]\+/-999.9/g’
2006.0,1.0,-999.9,-5.0

……………………………………………………
Any of these (and more) is a regexp that will modify that line as you want:

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/\*\**/999.9/g’
2006.0,1.0,999.9,-5.0

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/\*\+/999.9/g’
2006.0,1.0,999.9,-5.0

$ echo “2006.0,1.0,************,-5.0” | sed -r ‘s/\*+/999.9/g’
2006.0,1.0,999.9,-5.0

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/\*\{12\}/999.9/g’
2006.0,1.0,999.9,-5.0

$ echo “2006.0,1.0,************,-5.0” | sed -r ‘s/\*{12}/999.9/g’
2006.0,1.0,999.9,-5.0

$ echo “2006.0,1.0,************,-5.0” | sed ‘s/\*\{1,\}/999.9/g’
2006.0,1.0,999.9,-5.0

$ echo “2006.0,1.0,************,-5.0” | sed -r ‘s/\*{1,}/999.9/g’
2006.0,1.0,999.9,-5.0

sed operates on regular expressions, not strings, so you need to learn regular expression syntax if you’re going to use sed and in particular the difference between BREs (which sed uses by default) and EREs (which some seds can be told to use instead) and PCREs (which sed never uses but some other tools and “regexp checkers” do). Only the first solution above is a BRE that will work on all seds on all platforms. Google is your friend.
……………………………………………………
* is a regex symbol that needs to be escaped.

You can even use BASH string replacement:

s=”2006.0,1.0,************,-5.0″
echo “${s/\**,/-999.9,}”
2006.0,1.0,-999.9,-5.0

Using sed:

sed ‘s/\*\+/999.9/g’ <<< "$s" 2006.0,1.0,999.9,-5.0 ............................................................ Ya, * are special meta character which repeats the previous token zero or more times. Escape * in-order to match literal * characters. sed 's/\*\*\*\*\*\*\*\*\*\*\*\*/-999.9/g' ............................................................ When this possibility was introduced into gawk I have no idea! gawk -F, '{sub(/************/,"-999.9",$3)}1' OFS=, file 2006.0,1.0,-999.9,-5.0

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.