从 Linux 中的多个文件中删除文本行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1182756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:33:41  来源:igfitidea点击:

Remove line of text from multiple files in Linux

linuxunixcommand-line

提问by Robert

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

有没有一种简单的方法可以从命令行中充满文本文档的文件夹中删除同一行文本?

采纳答案by ennuikiller

If your version of sed allows the -i.bakflag (edit in place):

如果您的 sed 版本允许该-i.bak标志(就地编辑):

sed -i.bak '/line of text/d' * 

If not, simply put it in a bash loop:

如果没有,只需将其放入 bash 循环中:

for file in *.txt
do
    sed '/line of text/d' "$file" > "$file".new_file.txt
done

回答by chaos

I wrote a Perl script for this:

我为此编写了一个 Perl 脚本:

#!/usr/bin/perl

use IO::Handle;

my $pat = shift(@ARGV) or
    die("Usage: 
foreach $line (@file) {
    chomp $line;
    if($line eq $pat) {
        $line = '';
        $found = 1;
        last;
    }
}
pattern files\n"); die("Usage
for thefile in *.txt ; do
   grep -v "text to remove" $thefile > $thefile.$$.tmp
   mv $thefile.$$.tmp $thefile
done
pattern files\n") unless @ARGV; foreach my $file (@ARGV) { my $io = new IO::Handle; open($io, $file) or die("Cannot read $file: $!\n"); my @file = <$io>; close($io); foreach my $line (@file) { if($line =~ /$pat/o) { $line = ''; $found = 1; last; } } if($found) { open($io, ">$file") or die("Cannot write $file: $!\n"); print $io @file; close($io); } }

Note that it removes lines based on a regex. If you wanted to do exact match, the inner foreachwould look like:

请注意,它会根据正则表达式删除行。如果你想做精确匹配,内部foreach看起来像:

perl -ni -e 'print if not /mystring/' *

回答by Ry4an Brase

Consider grep -v:

考虑 grep -v:

perl -pi -e 's/something/other/' *

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.

Grep -v 显示除匹配行之外的所有行,它们进入临时文件,然后将 tmpfile 移回旧文件名。

回答by Mark Harrison

find . -name "*" -type f | xargs sed -i -e '/<PATTERN>/d'

This tells perl to loop over your file (-n), edit in place (-i), and print the line if it does not match your regular expression.

这告诉 perl 遍历您的文件 (-n),就地编辑 (-i),如果它与您的正则表达式不匹配,则打印该行。

Somewhat related, here's a handy way to perform a substitution over several files.

有点相关,这是对多个文件执行替换的便捷方法。

find . -name "*.xml" -type f | xargs sed -i -e '/sleep/d'

回答by Sitesh

To find a patternand remove the line containing the patternbelow command can be used

可以使用以下命令查找a pattern和删除the line containing the pattern

##代码##

Example : if you want to remove the line containing word sleepin all the xmlfiles

示例:如果要删除sleep所有xml文件中包含单词的行

##代码##

NOTE : Be careful before choosing a pattern as it will delete the line recursively in all the files in the current directory hierarchy :)

注意:在选择模式之前要小心,因为它会递归地删除当前目录层次结构中所有文件中的行:)