I want to search for and remove a block text from several files.

The block of text to be matched against is in file, say /home/user/myblock.txt I want to parse the directory /home/user/rep and remove the content of mybloc.txt from all the files in the directory.

  • 1
    By "block of text" do you mean a block of (potentially) many lines of text, or a string of characters that always fits within a single line of text? – Riccardo Murri May 30 '11 at 16:20

If the block of text to remove spans multiple lines, and if each individual file fits comfortably in RAM, the easiest approach is to load each file entirely into RAM and perform a global string replacement. Perl makes this easy, with -i -p to modify files in place record by record and -0777 to not set any record separator (the default record separator is a newline).

perl -0777 -i -pe '
    BEGIN {
        open BLOCK, "<", "/home/user/myblock.txt";
        $block = <BLOCK>;
        die unless length($block);
        close BLOCK;
' /home/user/rep/*
  • Another perl pearl.. \Q (how to to de-regex a regex) ... I'm starting to see why you use perl so much.. – Peter.O May 31 '11 at 1:55
  • Your answer looks interesting, but it does'nt work here. And I've got no error. – nicosomb May 31 '11 at 6:47
  • @nico_somb: It works for me™. Do the files have the same line endings, and no extra whitespace? If you can't get it to work, post some sample files. – Gilles 'SO- stop being evil' May 31 '11 at 10:01
  • @nico_somb: I initially thought that it wasn't working, because I expected output on stdout, but the -i updates the input files with the changes, so nothing is displayed... It definitely works for me.. – Peter.O May 31 '11 at 10:03

You can try this:

    var=`cat /home/user/myblock.txt`;
    sed -e s/"$var"//g /home/user/rep/somefile;

Of course, this will just print the somefile without your block of text, but you can easily save it with output redirection.

If you want to edit the file "in place", use the -i argument to sed, but be careful. You may lose all your data. It's a better idea to save to some temporary file and rename it.

This is not working if myblock.txt has multiple lines.

  • I tried your code in myfile.sh, but I've got an error : line 1: /home/user/myblock.txt: Permission denied. I tried with sudo, same error. – nicosomb May 30 '11 at 13:37
  • use backticks on the first line, I dont know to format it well. Admin, please help – Boban P. May 30 '11 at 14:09
  • it's the same thing. It doesn't work. – nicosomb May 31 '11 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.