0

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

3
  • Instead of the range operator 1,4d in the example you posted just do 's/<your text>//' ... Commented Mar 17, 2014 at 20:25
  • So I can just do find . -type f -iname \*.txt -exec sed -i~ 's/file_name//' '{}' +? Commented Mar 17, 2014 at 20:30
  • No, not filename. The text you want to delete Commented Mar 17, 2014 at 20:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thank U, it worked for me … just a note: it just and .orig suffix to original files …

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.