Professional Web Applications Themes

Line replace - PERL Beginners

Hello I should comment out lines in a text file. I have a lot of troubles to realize this. The lines Alpha(a, b, c) should be changed to # Alpha(a, b, c) What I would like to write is a small perl script to make this convetion. Thanks Urs...

  1. #1

    Default Line replace

    Hello

    I should comment out lines in a text file. I have a lot of troubles to
    realize this.

    The lines
    Alpha(a, b, c)
    should be changed to
    # Alpha(a, b, c)

    What I would like to write is a small perl script to make this convetion.

    Thanks

    Urs
    Urs Guest

  2. #2

    Default Re: Line replace

    On Mon, 2004-10-11 at 16:23, Urs Wagner wrote: 


    perl -pli.BAK -e 's/(?=.*Alpha\(a, b ,c\))/#/' FILENAME

    HTH
    Ram


    Ramprasad Guest

  3. #3

    Default RE: Line replace


    Good one liner ....I appreciate it ..

    Arjun


    -----Original Message-----
    From: Ramprasad A Padmanabhan [mailto:com]
    Sent: Monday, October 11, 2004 4:41 PM
    To: Urs Wagner
    Cc: perl beginners
    Subject: Re: Line replace


    On Mon, 2004-10-11 at 16:23, Urs Wagner wrote: 


    perl -pli.BAK -e 's/(?=.*Alpha\(a, b ,c\))/#/' FILENAME

    HTH
    Ram



    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>




    Confidentiality Notice

    The information contained in this electronic message and any attachments tothis message are intended
    for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
    you are not the intended recipient, please notify the sender at Wipro com immediately
    and destroy all copies of this message and any attachments.
    Arjun Guest

  4. #4

    Default RE: Line replace


    Hai !
    Open the file with any file handler and use regular expression.. This
    way it will work.



    Open (File handler name ,">>file name");

    While ( $string=<filehandler>)
    {
    if ( $string = ~ /^Alpha\(a,b,c\)/ )
    {
    $string = "#" . $string ;
    print <file handler> $string ;
    }
    }


    Arjun

    -----Original Message-----
    From: Urs Wagner [mailto:phys.ethz.ch]
    Sent: Monday, October 11, 2004 4:24 PM
    To: org
    Subject: Line replace


    Hello

    I should comment out lines in a text file. I have a lot of troubles to
    realize this.

    The lines
    Alpha(a, b, c)
    should be changed to
    # Alpha(a, b, c)

    What I would like to write is a small perl script to make this
    convetion.

    Thanks

    Urs

    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>





    Confidentiality Notice

    The information contained in this electronic message and any attachments tothis message are intended
    for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
    you are not the intended recipient, please notify the sender at Wipro com immediately
    and destroy all copies of this message and any attachments.
    Arjun Guest

  5. #5

    Default Re: Line replace

    On Mon, 11 Oct 2004, Ramprasad A Padmanabhan wrote:
     
    >
    > perl -pli.BAK -e 's/(?=.*Alpha\(a, b ,c\))/#/' FILENAME[/ref]

    Knowing how to do this in Perl is, of course, a very useful thing.

    In this case though, it would also be useful to know how to do this in
    your text editor. In vi/Vim, for example, you can do a --

    :%s/\(Alpha(a, b, c)\)/# \1/gc

    -- and this will find every instance of the string [1], show you each
    instance as it is found, and ask for you to confirm the change. If it
    looks right when the command finishes, save the file as normal.

    I know Emacs can do this about as efficiently, but I'm not an Emacs user
    so I'm not sure how to go about it.

    Editors like BBEdit, UltraEdit, and even Microsoft Write / WordPad have
    ways to get similar results, though it may be by way of simple replace
    rather than a full regular expression match & substitution. In this
    case, that's okay -- you can work with literal strings here -- but using
    an editor that can do full regex search & replace is very useful. It's
    worth using such an editor and getting comfortable with how it works.


    [1] "Every instance" includes multiple instances on the same line,
    which may or may not be what you want; take out the 'g' at the
    end of the command if you only want to match the first one.


    --
    Chris Devers
    Chris Guest

Similar Threads

  1. Regex - assign to new variable and replace in one line?
    By Brad Walton in forum PERL Miscellaneous
    Replies: 12
    Last Post: June 22nd, 12:17 PM
  2. replace leading whitespaces in every new line
    By Tim Van Wassenhove in forum PHP Development
    Replies: 6
    Last Post: February 3rd, 04:27 AM
  3. command line search and replace
    By Randy Brown in forum PERL Beginners
    Replies: 2
    Last Post: December 31st, 08:56 PM
  4. recursive replace command line
    By George Georgalis in forum PERL Beginners
    Replies: 7
    Last Post: December 11th, 11:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139