delete lines with sed match a special regex - regex

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?

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

Related

Extract text between two markers and replace a character

I want to change
>lcl|ORF183:9482:8118 unnamed protein product
into
>ORF183:9482-8118
Keep everything after | and before 'white space', plus replacing second : to -
So far I'm doing it with the following code:
sed -e '/^>/s/ .*//' -e '/^>/s/|/ /' -e '/^>/s/lcl //' -e '/^>/s/\(.*\):/\1-/'
but wish to do it in a simpler one-line code.
This could work:
sed -e 's/\(^.*|\)\(.*\):\(.*\):\(.*\)[[:space:]]\(unnamed.*$\)/>\2:\3-\4/'
Here's some improvements based on code you've tried
$ sed -e '/^>/s/ .*//' -e '/^>/s/lcl|//' -e '/^>/s/:/-/2' ip.txt
>ORF183:9482-8118
-e '/^>/s/|/ /' -e '/^>/s/lcl //' can be simplified to -e '/^>/s/lcl|//'
use s/>[^|]*|/>/ if you wish to match any text between > and |
sed allows to specify which occurrence of the match you want to replace, s/:/-/2 means replace the 2nd : to -
If your sed implementation allows grouping, you can group all the commands (separated by ;) inside {} for a particular address
$ sed '/^>/{s/ .*//; s/lcl|//; s/:/-/2}' ip.txt
>ORF183:9482-8118
Please visit https://stackoverflow.com/tags/sed/info for learning resources and other goodies

unterminated `s' command, can't find my mistake

sudo wbinfo --group-info GROUPNAME| sed -r -e 's/(?:DOMAIN\\(\w+),?)|(?:[^]+:)/$1/g'
This command results in an
sed: -e expression #1, char 36: unterminated `s' command
The output of
sudo wbinfo --group-info GROUPNAME
is like
GROUPNAME:x:0123456789:DOMAIN\user1,DOMAIN\user2,DOMAIN\user3,...,DOMAIN\userN
I tried escaping all instances of ( with \(, \ with \\ (also \\ with \\\\)
sudo wbinfo --group-info GROUPNAME| sed -r -e s/'(?:DOMAIN\\(\w+),?)|(?:[^]+:)'/$1/g
(changed quoted area)
sudo wbinfo --group-info GROUPNAME| sed -r -e s/'(?:DOMAIN\\(\w+),?)|(?:[^]+:)/\1/g'
(\1 instead of $1)
I still don't know how to get what I need:
user1 user2 user3 ... userN
TL;TR
Your attempt is too complicated, you can simply use this:
sed -r 's/[^\]+DOMAIN\\([[:alnum:]]+)/\1 /g'
About the syntax error:
You are using sed -r which enables extended posix regular expressions. Note that in extended posix regular expressions the ? is used as a quantifier for optional repetition. You you need to escape it:
sed -r -e 's/(\?:DOMAIN\\(\w+),\?)|(\?:[^]+:)/$1/g'
However, there is still a problem left with the regex: you are using [^]. Note that the ^ when used in a character class, negates the match of that class. You are using the ^ but missed to say which characters should not matched. You need to put in something like:
sed -r -e 's/(\?:DOMAIN\\(\w+),\?)|(\?:[^abc]+:)/$1/g'
awk to the rescue!
$ ... | awk -F'\\\\' -v RS=, '{print $2}'
will give the result one user per line, if you want them to appear on a single line add ... | xargs
Here's another approach with sed:
sed -r -e 's/^.*://' -e 's/[^,]+\\//g' -e 's/,/ /g'
First remove all the stuff before the last colon in the line,
then remove all the domain parts (non-commas followed by a backslash),
then change commas to spaces.

sed: delete characters between two strings

I'd like to use sed to remove all characters between "foo=1&" and "bar=2&" for all occurrences in an xml file.
<url>http://example.com?addr=123&foo=1&s=alkjasldkffjskdk$bar=2&f=jkdng</url>
<url>http://example.com?addr=124&foo=1&k=d93ndkdisnskiisndjdjdj$bar=2&p=dnsks</url>
Here is my sed command:
sed -e '/foo=1&/,/bar=2&/d' sample.xml
When I run this, the file is unchanged.
The above is based on this example: Find "string1" and delete between that and "string2"
Use the substitution command instead of the delete command:
sed -e 's/\(foo=1&\).*\(bar=2&\)/\1\2/'
You should use
sed -i -e 's/\(foo=1&\).*\(bar=2&\)/\1\2/' your_html.xml

Delete matching line with sed

I currently have this sed command that replaces foo.us.param=value with foo.param=value:
sed -i -e 's/\.us\./\./' file.txt
I also need it to delete any lines that contain .eu. anywhere but leave all other lines untouched. Any help would save me a long time trying to figure this out alone and would be greatly appreciated.
sed -i -e 's/\.us\./\./' -e '/\.eu\./d' file.txt
instead of sed you could also use grep
grep -v '\.eu\.'

sed: mix explicit and regex phrases

I'm trying to write a sed command to remove a specific string followed by two digits. So far I have:
sed -e 's/bizzbuzz\([0-9][0-9]\)//' file.txt
but I cant seem to get the syntax right. Any suggestions?
sed -re 's/bizzbuzz[0-9]{2}//' file.txt
and
sed -re 's/\bbizzbuzz[0-9]{2}\b//' file.txt
if the searched string have word boundary
sed -e 's/bizzbuzz[0-9]\{2\}//' file.txt
if you don't have GNU sed
Your current approach seems like it should work fine:
$ echo 'FOO bizzbuzz56 BAR' | sed -e 's/bizzbuzz\([0-9][0-9]\)//'
FOO BAR
As said in other answer, the syntax seems to be fine (with unnecesary parenthesis).
But may be you want to replace all the strings found in each line ? In that case, you should add a 'g' at the end of the 's' command:
sed -e 's/bizzbuzz\([0-9][0-9]\)//g' file.txt