Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Replace a pattern in each file

by Eily (Vicar)
on Nov 01, 2013 at 12:25 UTC ( #1060754=note: print w/replies, xml ) Need Help??

in reply to Replace a pattern in each file

Adding \ in front of the spaces is not necessary. This is useful in a shell command to show that the space isn't here to separate different arguments if your string isn't enclosed in quotes. But since your file name is in a string of its own, there's no ambiguity. Besides with s#\s+#\\ /sg; you would have replaced multiple spaces by only one.

What you are searching for is Inplace Editing, you either have to add the -i option in your shebang (first line) or set the $^I variable. This means that all files opened with the <> construct will be edited inplace. while (<>) will go through each lines of each files in @ARGV (which you can set manually), and with inplace editing activated, anything you will print will be written to that file (so to let a line unchanged, you actually have to print it again).

chomp(@ARGV = <FILE>); # put each line of FILE into @ARGV and remove t +he trailing \n while(<>) # for each filename in @ARGV { print s! <--/--a8300--> .*? <--/a8300-->. !!igr; }

Maybe what you actually meant is actually s! <--/--a8300--> (.*?) <--/a8300-->. !$1!igr which translate to "get the content of <--/--a8300--> <--/a8300-->and keep only that", when what you wrote will delete the whole thing, tag and content. You could read perlretut for useful information on Perl regexen.

Are you sure sed wouldn't have been enough though?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060754]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-02-22 01:46 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (322 votes). Check out past polls.