从linux终端中的多个文件中删除保存在文件中的文本

时间:2022-11-08 02:52:27

I’d like to remove some text that I have saved into a file (because it is quite long) of which I know it is part of many files in a directory (and its subdirectories). Now, I want to remove that sample text from all those files.

我想删除一些我保存到文件中的文本(因为它很长)我知道它是目录中许多文件(及其子目录)的一部分。现在,我想从所有这些文件中删除该示例文本。

I have read this one, but it does not uses and input file—I cannot manage this little tweak :)

我已经读过这个,但它没有使用和输入文件 - 我无法管理这个小调整:)

Thank you in advance.

先谢谢你。

Info: I am using Ubuntu 13.10 i386

信息:我正在使用Ubuntu 13.10 i386

1 个解决方案

#1


1  

Let the common text be in a file named "common.txt"

让公共文本位于名为“common.txt”的文件中

find . -type f -not -name common.txt -print0 |
xargs -0  perl -i.orig -0777 -pe '
    BEGIN {open my $fh, "<", "common.txt"; $common = <$fh>; close $fh} 
    while (($i=index($_, $common)) != -1) {
        substr($_, $i, length($common)) = ""
    }
'

That perl program reads each file as a single string, then uses plain string matching to delete the common parts.

perl程序将每个文件作为单个字符串读取,然后使用纯字符串匹配来删除公共部分。

#1


1  

Let the common text be in a file named "common.txt"

让公共文本位于名为“common.txt”的文件中

find . -type f -not -name common.txt -print0 |
xargs -0  perl -i.orig -0777 -pe '
    BEGIN {open my $fh, "<", "common.txt"; $common = <$fh>; close $fh} 
    while (($i=index($_, $common)) != -1) {
        substr($_, $i, length($common)) = ""
    }
'

That perl program reads each file as a single string, then uses plain string matching to delete the common parts.

perl程序将每个文件作为单个字符串读取,然后使用纯字符串匹配来删除公共部分。