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

Re: Newbie: uses/limits of perl in editing files

by cdarke (Prior)
on Nov 23, 2007 at 14:42 UTC ( [id://652580]=note: print w/replies, xml ) Need Help??


in reply to Newbie: uses/limits of perl in editing files

Exetending your requirements a little, there is a neat feature that is useful when replacing tokens, like your HEADER and REMARK. You can execute code from within a substitute statement, for example:
$line =~ s/(HEADER|REMARK)/mysub($1)/ge;
That will call user-written subroutine mysub every time HEADER and REMARK are found in the text. The argument passed is the text matched inside (). Whatever is returned by mysub will replace the token. It probably would not be worth it for the simple substitution you mentioned, but for more complex combinations it can be very powerful.

Replies are listed 'Best First'.
Re^2: Newbie: uses/limits of perl in editing files
by wherethewild (Novice) on Nov 23, 2007 at 15:02 UTC
    I was looking at those s/// but I wasn't sure I how I could get it to do some of the things I need as the text which has to be substitued is different from file to file and it also appears elswhere in the file, where it's not to be adjusted.

    Was that clear?
    This is a theoretical line is my file:
    BOBBY X66666 A 345 674 A 123 488

    The X66666 has to be changed to B22222. But the next file might have U33333 there instead of X66666, or worse still 666D3P, or even worse absolutely nothing at all. And I don't know what it might be unless I open up each of the text files and look what the previous program did to it (something I'm trying to avoid by learning this!). It SHOULD be that the spacing is constant across that line, but that's not guaranteed.

    Anyway, that's some of what I'm trying to do. Thanks everyone for the speed and friendliness in helping me out!

      BOBBY X66666 A 345 674 A 123 488

      Assuming the piece you want to replace is the 2nd token in the line then you can do something like:

      s/^(\S+)(\s+)(\S+)(.*)/${1}${2}B22222${4}/;

      This reads like:
      (NOT WHITESPACE)(WHITESPACE)(NOT WHITESPACE)(EVERYTHING)

      That collects the first 3 pieces into variables $1-$3 then the remainder of the line into $4, then reassemblies the line with the pieces and the replacement.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found