Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2016-10-21 18:32 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (289 votes). Check out past polls.