0

I have a String

/abc/gef        \*(cse,fff)

to delete from a file,have to match the full string pattern,tried with

sed -i '//abc/gef        \*(cse,fff)/d' filename

but this ends in an error:

sed: -e expression #1, char xx: expected newer version of sed

I also tried the following options but didn't work:

sed -i '/\/abc\/gef        \*(cse,fff)/d' filename
sed -i 's|/abc/gef        \*(cse,fff)||g' filename

What is the right command for this?

2
  • I am able to execute upto below expreseeion sed -i '/\/abc\/gef \*/d' filename BUT when i include "(" like --- sed -i '/\/var\/nfs \*(cse,fff)/d' filename it's not deleting
    – Mike
    Commented Dec 3, 2020 at 10:59
  • how to escape the "(" and ")" ? DO i need to escape "," also
    – Mike
    Commented Dec 3, 2020 at 11:07

3 Answers 3

2

The following works with GNU sed versions 4.5 and 4.7 and will delete any lines that contain the string:

sed '/\/abc\/gef        \\\*(cse,fff)/d' file

You have to use \ to escape the two instances of / in the string so that they aren't taken as the delimiter and * which otherwise expands to the character before it which is f. The latter will cause it not to match the string.

You can also use the s option which allows other characters as a delimiter if you only want to delete the string itself throughout the file and not the entire line:

sed 's|/abc/gef        \\\*(cse,fff)||g' file

That uses | as a delimiter so that you don't have to escape /.

To edit the file in place after you're sure that it does what you want, you can use -i like you have above:

sed -i '/\/abc\/gef        \\\*(cse,fff)/d' file

sed -i 's|/abc/gef        \\\*(cse,fff)||g' file

EDIT: I have updated the answer as the string in the question is different from what it was when originally posted.

7
  • You may also use almost any other character as a delimiter in the s command, getting rid of the need for escaping the slashes.
    – Kusalananda
    Commented Dec 3, 2020 at 8:58
  • IT's not giving the error but not deleting from the file the exact string to delete --- /abc/gef *(cse,fff) after gef 4,5 blankspaces are present but it's not showing here
    – Mike
    Commented Dec 3, 2020 at 9:13
  • The /.../d version deletes the entire line that contains the string, though, not just the string.
    – DonHolgo
    Commented Dec 3, 2020 at 9:16
  • yes I want to delete the entire line
    – Mike
    Commented Dec 3, 2020 at 9:22
  • Tried with below options but didn't work--- sed -i '/\/abc\/gef *(cse,fff)/d' filename sed -i 's|/var/nfs *(cse,fff)||g' filename
    – Mike
    Commented Dec 3, 2020 at 9:28
1

The problem is that both \ (escape character) and * (0 or more) have a special meaning in regular expressions, so you need to escape them in order to use them as literal characters. Now, the way to escape something is to add a \ before it. So, in order for * to match a literal asterisk and not mean "0 or more", you would write \*. Similarly, to escape the \ you would write \\. Putting this together, we have:

$ cat file 
foo
bar
/abc/gef        \*(cse,fff)
baz

And:

$ sed '/\/abc\/gef        \\\*(cse,fff)/d' file 
foo
bar
baz

Note how I have also escaped the / since // is the match operator in sed. This is why we have /\/abc\/gef and not //abc/gef. The d at the end means "delete any line matching this pattern".

0
1

This sounds like a job for grep:

$ cat file
line 1
line 2
/abc/gef        *(cse,fff)
line 4

$ grep -Fxv '/abc/gef        *(cse,fff)' file
line 1
line 2
line 4
1
  • Thanks a lot Glenn
    – Mike
    Commented Dec 3, 2020 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .