How to remove text at the beginning of a file using regular expression?

I have a bunch of files that contain a semi-standard header. That is, its appearance is very similar, but the text changes somewhat.

I want to remove this header from all files.

From looking at the files, I know that what I want to delete is encapsulated between similar words.

So, for example, I have:

Foo bar...some text here...
more text
Foo bar...I want to keep everything after this point

I tried this command in perl:

perl -pi -e "s/\A.*?Foo.bar*?Foo.bar//simxg" 00ws110.txt

But that will not work. I am not a regular expression expert, but I hope someone knows how to basically remove a piece of text from the very beginning of the file based on text match rather than the number of characters ...

+3
source share
4 answers

ARGV (aka <>, -p), .

:

  • Unset $/, Perl .

    perl -pi -e "BEGIN{undef$/}s/\A.*?Foo.bar*?Foo.bar//simxg" 00ws110.txt
    

    BEGIN , , .

  • -0, $/ = "\0".

    perl -pi -0 -e "s/\A.*?Foo.bar*?Foo.bar//simxg" 00ws110.txt
    
  • --.

    perl -ni -e "print unless 1 ... /^Foo.bar/'
    

    , 1 /^Foo.bar/.

+7

, perl . , :

perl -0777pi.orig -e 's/your regex/your replace/s' file1 file2 file3

-0777 perl slurp, $_ . , , . , , . . perldoc perlrun.

, , Project Gutenberg ebook, , , :

perl -ni.orig -e 'print unless 1 .. /^\*END/' 00ws110.txt

Gutenberg

*END*THE SMALL PRINT! FOR PUBLIC DOMAIN ETEXTS*Ver.04.29.93*END*

*END* , .

+3

, , , :

perl -ni -e 'print unless 1..($. > 1 && /^Foo bar/)'
+2

! :


use Tie::File;

tie my @array,"Tie::File","path_to_file" or die("can't tie the file");
$array[0] =~s/text_i_want_to_replace/replacement_text/gi;
untie @array;

You can work with the array and you will see the changes in the array. You can delete elements from an array and erase a string from a file. Applying substitution on elements will replace text from strings.

If you want to delete the first two lines and save something from the third, you can do something like this:


# tie the @array before this
shift @array;
shift @array;
$array[0]=~s/foo bar\.\.\.//gi;
# untie the @array

and that will do exactly what you need!

0
source

Source: https://habr.com/ru/post/1705205/


All Articles