tukusejssirs
tukusejssirs

Reputation: 854

Remove text that is saved in a file, from multiple files in linux terminal

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

Upvotes: 0

Views: 65

Answers (1)

glenn jackman
glenn jackman

Reputation: 246817

Let the common text be in a file named "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.

Upvotes: 1

Related Questions