find and remove a line in multiple files

前端 未结 1 1146
刺人心
刺人心 2021-01-29 10:24

I have many files, and I have to find a single line in these files and delete, and should be in terminal in linux. Anyone know how to do?

Example

FileSystem

1条回答
  •  迷失自我
    2021-01-29 11:03

    This would delete that line in each file.

    for f in myFiles/*; do
      sed -i 'd/pattern that matches line that you want to delete/' $f
    done
    

    Alternatively you could use awk as well.

    tmp=$(mktemp)
    for f in myFiles/*; do
      awk '!/pattern that matches the line that you want to delete/' $f > $tmp
      cp $tmp $f 
    done
    rm $tmp
    

    The pattern here would be a regular expression. You can specify different variants of regular expressions, e.g. POSIX or extended by passing different flags to sed or awk. Let me know if this adequately answers your question.

    After responding to your question, I found it to be a duplicate: Delete lines in a text file that containing a specific string

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