Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete lines with sed match a special regex

Tags:

regex

sed

match

I try to delete all lines that begin with some optional special chars followed by blubb:

That's the lines I want to match:

#blubb
*blubb
-blubb
blubb

That should do it, but doesn't work :(

sed "/^.?blubb$/d" -i special.conf   sed "/^[#*-]?blubb$/d" -i special.conf   

Has somebody the right solution?

like image 852
Thomas Avatar asked May 30 '12 19:05

Thomas


People also ask

How do I delete a sed matching line?

To begin with, if you want to delete a line containing the keyword, you would run sed as shown below. Similarly, you could run the sed command with option -n and negated p , (! p) command. To delete lines containing multiple keywords, for example to delete lines with the keyword green or lines with keyword violet.

How do I remove a specific pattern from a Unix file?

N command reads the next line in the pattern space. d deletes the entire pattern space which contains the current and the next line. Using the substitution command s, we delete from the newline character till the end, which effective deletes the next line after the line containing the pattern Unix.

Does sed work with regex?

Although the simple searching and sorting can be performed using sed command, using regex with sed enables advanced level matching in text files. The regex works on the directions of characters used; these characters guide the sed command to perform the directed tasks.


1 Answers

Use this sed command:

sed -i.old '/^[#*-]\{0,1\}blubb/d' special.conf 

OR

sed -i.old -E '/^[#*-]?blubb/d' special.conf 

OR

sed -i.old -r '/^[#*-]?blubb/d' special.conf 
like image 164
anubhava Avatar answered Sep 24 '22 23:09

anubhava