How to delete from a text file, all lines that contain a specific string?

后端 未结 18 2168
生来不讨喜
生来不讨喜 2020-11-22 02:06

How would I use sed to delete all lines in a text file that contain a specific string?

相关标签:
18条回答
  • 2020-11-22 02:46

    You can use sed to replace lines in place in a file. However, it seems to be much slower than using grep for the inverse into a second file and then moving the second file over the original.

    e.g.

    sed -i '/pattern/d' filename      
    

    or

    grep -v "pattern" filename > filename2; mv filename2 filename
    

    The first command takes 3 times longer on my machine anyway.

    0 讨论(0)
  • 2020-11-22 02:46
    cat filename | grep -v "pattern" > filename.1
    mv filename.1 filename
    
    0 讨论(0)
  • 2020-11-22 02:50
    perl -i    -nle'/regexp/||print' file1 file2 file3
    perl -i.bk -nle'/regexp/||print' file1 file2 file3
    

    The first command edits the file(s) inplace (-i).

    The second command does the same thing but keeps a copy or backup of the original file(s) by adding .bk to the file names (.bk can be changed to anything).

    0 讨论(0)
  • 2020-11-22 02:51

    You may consider using ex (which is a standard Unix command-based editor):

    ex +g/match/d -cwq file
    

    where:

    • + executes given Ex command (man ex), same as -c which executes wq (write and quit)
    • g/match/d - Ex command to delete lines with given match, see: Power of g

    The above example is a POSIX-compliant method for in-place editing a file as per this post at Unix.SE and POSIX specifications for ex.


    The difference with sed is that:

    sed is a Stream EDitor, not a file editor.BashFAQ

    Unless you enjoy unportable code, I/O overhead and some other bad side effects. So basically some parameters (such as in-place/-i) are non-standard FreeBSD extensions and may not be available on other operating systems.

    0 讨论(0)
  • 2020-11-22 02:52

    You can also delete a range of lines in a file. For example to delete stored procedures in a SQL file.

    sed '/CREATE PROCEDURE.*/,/END ;/d' sqllines.sql

    This will remove all lines between CREATE PROCEDURE and END ;.

    I have cleaned up many sql files withe this sed command.

    0 讨论(0)
  • 2020-11-22 02:53

    to show the treated text in console

    cat filename | sed '/text to remove/d' 
    

    to save treated text into a file

    cat filename | sed '/text to remove/d' > newfile
    

    to append treated text info an existing file

    cat filename | sed '/text to remove/d' >> newfile
    

    to treat already treated text, in this case remove more lines of what has been removed

    cat filename | sed '/text to remove/d' | sed '/remove this too/d' | more
    

    the | more will show text in chunks of one page at a time.

    0 讨论(0)
提交回复
热议问题