Remove line of text from multiple files in Linux

前端 未结 5 1478
时光取名叫无心
时光取名叫无心 2020-12-08 04:07

Is there a simple way to remove the same line of text from a folder full of text documents at the command line?

5条回答
  •  粉色の甜心
    2020-12-08 04:30

    Consider grep -v:

    for thefile in *.txt ; do
       grep -v "text to remove" $thefile > $thefile.$$.tmp
       mv $thefile.$$.tmp $thefile
    done
    

    Grep -v shows all lines except those that match, they go into a temp file, and then the tmpfile is moved back to the old file name.

提交回复
热议问题