0

I tried using sed '/\bpassword\b/d' and sed "/\<password\>/d" to find only lines that contain "password" and delete them but it doesn't work, the command also finds words like "password!" or "password@".

Let's say I have these words:

password
password1
password2
password!
password@
password%
password*

I only have to delete the line with "password" and not the others. Currently those two commands delete the "password@" , "password%" , "password!" and "password*" too.

  • 1
    ... what shouldn't come after password, then? Add example input and expected output, please. – muru Nov 9 '17 at 11:22
  • @muru Let's say I have these words "password" , "password1" , "password2" , "password!" , "password@" , "password%" , "password*" . I only have to delete the line with "password" and not the others,currently those two commands delete the "password@" , "password%" , "password!" and "password*" too . – Aintsmartenough Nov 9 '17 at 11:29
  • Add example input and output to the question by editing it. – muru Nov 9 '17 at 11:31
  • @muru done,the topic is edited – Aintsmartenough Nov 9 '17 at 11:32
  • 1
    You are telling about lines or words? Because even if your line contain password your commands deleting whole line! – αғsнιη Nov 9 '17 at 11:48
5

If they appear on one line per word, use the ^ and $ to match start of line and end of line. Consider the following sample file foo:

$ cat foo
password
password!1
password2
2password

Using sed matching beginning and end of line replaces only the line consisting of solely password

$ sed '/^password$/d' foo
password!1
password2
2password
2

It's currently unclear whether the lines you want to delete may contain anything but the string password. I'm to assume that they do because vidarlo’s answer already covers the opposite.

The (extended) regular expression

(^|\s)password(\s|$)

matches all strings that start at the start-of-line or with whites-space, followed by the infix password, followed by either white-space or end-of-line.

The respective sed command is:

sed -re '/(^|\s)password(\s|$)/d'

Alternatively you can use grep in inverse mode:

grep -vEe '(^|\s)password(\s|$)'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.