Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Replacing characters in a line

by Becky (Beadle)
on Mar 04, 2005 at 11:44 UTC ( #436536=perlquestion: print w/replies, xml ) Need Help??

Becky has asked for the wisdom of the Perl Monks concerning the following question:

I have lines in a file that look like this:

ATOM   3316  N   TYR D  24      97.479  18.805   2.990  1.00 36.32      D    N 
ATOM   3322  CD1 TYR D  24      96.225  15.974   2.812  1.00 17.96      D    C

I need to replace the 2 lone D characters in each line with an X. I DON'T want to replace thd D in CD1. Whatever regex I do I seem to replace the D in CD1 or muck up the formatting of the line, which must stay the same. Can you help? Thanks!

Replies are listed 'Best First'.
Re: Replacing characters in a line
by pelagic (Priest) on Mar 04, 2005 at 11:49 UTC
    Just use "word-boundary anchors" in your regex:
    /\bD\b/

    pelagic
Re: Replacing characters in a line
by holli (Monsignor) on Mar 04, 2005 at 11:50 UTC
    looks like a fixed length line, so iīd simply go for substr():
    $_="ATOM 3316 N TYR D 24 97.479 18.805 2.990 1.00 36.32 + D N"; substr($_,21,1)="X"; print;
    You can use a regex too.


    holli, /regexed monk/
Re: Replacing characters in a line
by monkey_boy (Priest) on Mar 04, 2005 at 13:52 UTC
    These are PDB files
    They are fixed position fields, so use holli's method.


    Im so sick of my Signature...
Re: Replacing characters in a line
by sh1tn (Priest) on Mar 04, 2005 at 15:52 UTC
    Depending on which 'D' exactly must be replaced:
    $_ =<<SQ; ATOM 3316 N TYR D 24 97.479 18.805 2.990 1.00 36.32 + D N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C SQ s/(.+ CD1 .+) | (\b D \b)/$1 or X/gex; __END__ STDOUT: ATOM 3316 N TYR X 24 97.479 18.805 2.990 1.00 36.32 + X N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C


    Or in the other case:
    $_ =<<SQ; ATOM 3316 N TYR D 24 97.479 18.805 2.990 1.00 36.32 + D N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C SQ s/(?<!CD1 \s TYR \s)(\b D \b)/X/gx; __END__ STDOUT: ATOM 3316 N TYR X 24 97.479 18.805 2.990 1.00 36.32 + X N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + X C


Re: Replacing characters in a line
by perlsen (Chaplain) on Mar 04, 2005 at 12:39 UTC

    Hi, just try this,

    $string='ATOM 3316 N TYR D 24 97.479 18.805 2.990 1.00 +36.32 D N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C'; $string=~s#((\w+) \d{4} \w TYR )D#$1X#gsi; print $string; output: ******* ATOM 3316 N TYR X 24 97.479 18.805 2.990 1.00 36.32 + D N ATOM 3322 CD1 TYR D 24 96.225 15.974 2.812 1.00 17.96 + D C
      Why not just s/(?<=TYR )D/X/;?

      Update:
      Or, to keep it fixed-length:
      s/^(.{21})D/$1X/;

      Sidenote:
      Has anyone an idea, why this:
      s/^(?<=.{21})D/X/;
      does not work, while this
      s/(?<=.{21})D/X/;
      does (but donīt use this, itīs unsafe)?


      holli, /regexed monk/
        Are we sure that D will always be preceeded by "TYR " ??
        Manav
Re: Replacing characters in a line
by Taulmarill (Deacon) on Mar 04, 2005 at 14:36 UTC
    the following simple regex should work:
    s/ D / X /g;
      *UPDATE*

      Ignore me. I didnt read the message properly

      but that would also replace the other lonley "D"'s
      e.g.

      ATOM   3316  N   TYR D  24      97.479  18.805   2.990  1.00 36.32      D    N 
      



      Im so sick of my Signature...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2020-02-19 03:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (80 votes). Check out past polls.

    Notices?