When you try to match strings with sed you need to distinguish between characters and metacharacters. Characters only match themselves, i.e.
will print every (line containing an) "a" because /a/ matches only an "a" and nothing else.
But there are characters that do not match these same characters directly but influence the way other characters match something. These are called metacharacters and a "regular expression" usually consists of a mixture of characters and metacharacters. Here:
the expression searched for to print the line is /aa*z/ and the asterisk makes the "a" in front of it optional: it makes that any number (including zero) of the character before is matched. Here are some strings which would be matched by this expression:
The one string NOT matched by the expression is this: aa*z, because the asterisk - unlike the "a" - doesn't match itself, only the way other characters (in this case the "a") match something.
In general, if you want to use a metacharacter as normal character you need to [i]escape[/icode] it. To escape it means you put a backslash ("\") in front of it:
This changes the "*" to be a normal character and the only string this expression will match now is:
and nothing else. The slash ("/") is also a metacharacter because it is used to mark beginning and end of a regular expression. In the above /aa*z/ it is not encluded in the string we searched for but acts only as a separator, similar to quotes like "this", which don't belong to the word they surround either. And therefore it needs to be escaped the same way as the asterisk:
You need to do this with all metacharacters in your search expression. The most used ones are:
Btw., you should NOT use more for this purpose and you do not need to use separate sed calls for this:
write it like this instead:
or even like this - ";" is used as separator between consecutive commands:
I hope this helps.
I have a list of names and email addresses, like this. The <tab> markers are actually tabs.
joe.blow <tab> joe.blow@wherever.com
tom.t.hall <tab> tom.t.hall@wherever.com
john.r.smith <tab> john.r.smith@wherever.com
sally.jones <tab> sally.jones@state.or.us
I want to parse the data so that... (3 Replies)
How to use sed to remove html tags including text between them?
Example: User <b> rolvak </b> is stupid. It does not using <b>OOP</b>!
and should output: User is stupid. It does not using !
Thank you.. (2 Replies)
I want to instert Category:XXXXX into the 2. line
something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong:
sed "2i\\${n}Category:$cat\n"
Sample:
Titel Blahh Blahh abllk sdhsd sjdhf
Blahh Blah Blahh
Blahh
Should look like... (2 Replies)
How do I filter only comments and still keep Line breaks at the end of the line!?
This is one of the common tasks we all do,, How can we do this in a right way..!?
I try to ignore empty lines and commented lines using following approach.
test.sh
# \040 --> SPACE character octal... (17 Replies)
I have the following line:
4/23/2010 0:00:38.000: Copying $$3MSYDDC02$I would like to use sed (or similiar) to remove everthing between and including $ that appears in the line so it ends up like this.
4/23/2010 0:00:38.000: Copying 3MSYDDC02I have been trying these but i'm really just... (5 Replies)
Hi,
I am using the following command(sed) to get the key/value pair from the string
String="{ "test":"test message", "testmessage":"subscription is active, charge successfully} " }"
status=$( echo $String | sed -e 's/^.*\("testmessage":*\).*$/\1/')
echo $status
i am getting this... (2 Replies)
hello.
How to remove all characters in a line from first character ( a $ ) until and including the third occurrence of that character ( $ ).
Any help is welcome. (10 Replies)
Hello,
I could not remove brackets with text contents
myfile:
Please remove the bracket with text
I wish to remove:
I tried:
sed 's/\//' myfile
It gives:
Please remove the bracket with text A1
I expect:
Please remove the bracket with text
Many thanks
Boris (2 Replies)
Hello All,
I have created a script that searches for different things and "sanitizes" the findings from files. Currently the user is required to put in a hostname (server.serverfarm.abc) one at a time to replace. I would like the user be able to use *.*.abc in grep and then pipe into sed to... (1 Reply)