Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Replace a pattern in each file

by Eily (Hermit)
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?


Comment on Re: Replace a pattern in each file
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1060754]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2014-09-19 06:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (132 votes), past polls