78

I need to replace the whole line with sed if it matches a pattern. For example if the line is 'one two six three four' and if 'six' is there, then the whole line should be replaced with 'fault'.

jww
  • 97,681
  • 90
  • 411
  • 885
irek
  • 1,075
  • 2
  • 11
  • 9
  • Have you considered `grep -v`? –  May 08 '13 at 12:15
  • I realize it's been a couple of years, but you should remember to come back and upvote/accept an answer if it was useful to you --and it looks like there're a few that appear to answer the question correctly. – code_dredd Aug 16 '18 at 18:46

4 Answers4

118

You can do it with either of these:

sed 's/.*six.*/fault/' file     # check all lines
sed '/six/s/.*/fault/' file     # matched lines -> then remove

It gets the full line containing six and replaces it with fault.

Example:

$ cat file
six
asdf
one two six
one isix
boo
$ sed 's/.*six.*/fault/'  file
fault
asdf
fault
fault
boo

It is based on this solution to Replace whole line containing a string using Sed

More generally, you can use an expression sed '/match/s/.*/replacement/' file. This will perform the sed 's/match/replacement/' expression in those lines containing match. In your case this would be:

sed '/six/s/.*/fault/' file

What if we have 'one two six eight eleven three four' and we want to include 'eight' and 'eleven' as our "bad" words?

In this case we can use the -e for multiple conditions:

sed -e 's/.*six.*/fault/' -e 's/.*eight.*/fault/' file

and so on.

Or also:

sed '/eight/s/.*/XXXXX/; /eleven/s/.*/XXXX/' file
Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    What if we have 'one two six eight eleven three four' and we want to include 'eight' and 'eleven' as our "bad" words? Will s/.*six|eight|eleven.*/fault/g work? – irek May 08 '13 at 12:37
  • 1
    What if i want to store the changes in the same file? – M.M.H.Masud Apr 22 '15 at 22:10
  • 1
    @M.M.H.Masud just use `sed -i`. You better use also `sed -i.bak` so the original file gets replaced but also backed up to `file.bak`. – fedorqui Apr 23 '15 at 07:35
  • The `/g` flag is useless here, as there can only ever be a single match on one line, and you only care whether there is one or more matches anyway. – tripleee Apr 07 '16 at 10:47
  • @tripleee you are right. Something + `.*` will have a unique match per line, if any. Thanks! – fedorqui Apr 07 '16 at 12:45
  • should also explain when to use double quotes for variables IMO – Mike Q Mar 09 '22 at 23:31
16

Above answers worked fine for me, just mentioning an alternate way

Match single pattern and replace with a new one:

sed -i '/six/c fault' file

Match multiple pattern and replace with a new one(concatenating commands):

sed -i -e '/one/c fault' -e '/six/c fault' file
Hasan Rumman
  • 577
  • 1
  • 6
  • 16
5

To replace whole line containing a specified string with the content of that line

Text file:

Row: 0 last_time_contacted=0, display_name=Mozart, _id=100, phonebook_bucket_alt=2
Row: 1 last_time_contacted=0, display_name=Bach, _id=101, phonebook_bucket_alt=2

Single string:

$ sed 's/.* display_name=\([[:alpha:]]\+\).*/\1/'
output:
100
101

Multiple strings delimited by white-space:

$ sed 's/.* display_name=\([[:alpha:]]\+\).* _id=\([[:digit:]]\+\).*/\1 \2/'
output:
Mozart 100
Bach 101

Adjust regex to meet your needs

[:alpha] and [:digit:] are Character Classes and Bracket Expressions

The Mauler
  • 141
  • 2
  • 5
3

This might work for you (GNU sed):

sed -e '/six/{c\fault' -e ';d}' file

or:

sed '/six/{c\fault'$'\n'';d}' file
potong
  • 55,640
  • 6
  • 51
  • 83