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

后端 未结 18 2141
生来不讨喜
生来不讨喜 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:29

    SED:

    • '/James\|John/d'
    • -n '/James\|John/!p'

    AWK:

    • '!/James|John/'
    • /James|John/ {next;} {print}

    GREP:

    • -v 'James\|John'
    0 讨论(0)
  • 2020-11-22 02:29

    Curiously enough, the accepted answer does not actually answer the question directly. The question asks about using sed to replace a string, but the answer seems to presuppose knowledge of how to convert an arbitrary string into a regex.

    Many programming language libraries have a function to perform such a transformation, e.g.

    python: re.escape(STRING)
    ruby: Regexp.escape(STRING)
    java:  Pattern.quote(STRING)
    

    But how to do it on the command line?

    Since this is a sed-oriented question, one approach would be to use sed itself:

    sed 's/\([\[/({.*+^$?]\)/\\\1/g'
    

    So given an arbitrary string $STRING we could write something like:

    re=$(sed 's/\([\[({.*+^$?]\)/\\\1/g' <<< "$STRING")
    sed "/$re/d" FILE
    

    or as a one-liner:

     sed "/$(sed 's/\([\[/({.*+^$?]\)/\\\1/g' <<< "$STRING")/d" 
    

    with variations as described elsewhere on this page.

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

    You can use good old ed to edit a file in a similar fashion to the answer that uses ex. The big difference in this case is that ed takes its commands via standard input, not as command line arguments like ex can. When using it in a script, the usual way to accomodate this is to use printf to pipe commands to it:

    printf "%s\n" "g/pattern/d" w | ed -s filename
    

    or with a heredoc:

    ed -s filename <<EOF
    g/pattern/d
    w
    EOF
    
    0 讨论(0)
  • 2020-11-22 02:31

    The easy way to do it, with GNU sed:

    sed --in-place '/some string here/d' yourfile
    
    0 讨论(0)
  • 2020-11-22 02:34

    To remove the line and print the output to standard out:

    sed '/pattern to match/d' ./infile
    

    To directly modify the file – does not work with BSD sed:

    sed -i '/pattern to match/d' ./infile
    

    Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

    sed -i '' '/pattern to match/d' ./infile
    

    To directly modify the file (and create a backup) – works with BSD and GNU sed:

    sed -i.bak '/pattern to match/d' ./infile
    
    0 讨论(0)
  • 2020-11-22 02:34

    Just in case someone wants to do it for exact matches of strings, you can use the -w flag in grep - w for whole. That is, for example if you want to delete the lines that have number 11, but keep the lines with number 111:

    -bash-4.1$ head file
    1
    11
    111
    
    -bash-4.1$ grep -v "11" file
    1
    
    -bash-4.1$ grep -w -v "11" file
    1
    111
    

    It also works with the -f flag if you want to exclude several exact patterns at once. If "blacklist" is a file with several patterns on each line that you want to delete from "file":

    grep -w -v -f blacklist file
    
    0 讨论(0)
提交回复
热议问题