find and remove a line in multiple files

江枫思渺然 提交于 2019-12-31 07:35:13

问题


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

myFiles
   + file1
   + file2
   ...
   + file6500

The file

aaa0
aaa1
....
fff9

回答1:


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



来源:https://stackoverflow.com/questions/29618779/find-and-remove-a-line-in-multiple-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!