Perl inplace editing

Discussion in 'Perl Misc' started by Sundaram Ramasamy, May 24, 2004.

  1. I want to check in file line start with HOSTNAME, then I want to
    replcae HOSTNAME value to linux.com, if line is not there I want add
    new line HOSTNAME=linux.com

    Using inplace editing I was not able to add new line.

    Here is my one liner inplace editing script

    perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {
    s/=.*$/=linux.com/; $nx++; print $_; }else { print $_; } END { if( $nx
    ==0 ){ $_="HOSTNAME=linux.com\n"; print $_; } }' network

    Requirment:

    1) orginal file:
    NMAE=myname
    IP=234.56.43.23
    HOSTNAME=abcde.com

    I need out put:
    NMAE=myname
    IP=234.56.43.23
    HOSTNAME=linux.com

    2) orginal file:
    NMAE=myname
    IP=234.56.43.23

    I need out put:
    NMAE=myname
    IP=234.56.43.23
    HOSTNAME=linux.com

    Any tips for this
     
    Sundaram Ramasamy, May 24, 2004
    #1
    1. Advertising

  2. Sundaram Ramasamy

    Joe Smith Guest

    Sundaram Ramasamy wrote:

    > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {


    You don't want to set $ne to zero everytime through the loop.

    perl -i.old -ne '$ne=0 if not defined $ne; if( /^\s*HOSTNAME\s*=/ ) {

    -Joe
     
    Joe Smith, May 24, 2004
    #2
    1. Advertising

  3. Sundaram Ramasamy

    Paul Lalli Guest

    On Mon, 24 May 2004, Sundaram Ramasamy wrote:

    > I want to check in file line start with HOSTNAME, then I want to
    > replcae HOSTNAME value to linux.com, if line is not there I want add
    > new line HOSTNAME=linux.com
    >
    > Using inplace editing I was not able to add new line.
    >
    > Here is my one liner inplace editing script
    >
    > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {
    > s/=.*$/=linux.com/; $nx++; print $_; }else { print $_; } END { if( $nx
    > ==0 ){ $_="HOSTNAME=linux.com\n"; print $_; } }' network
    >

    Your main problem is that when run via -i, perl selects STDOUT after the
    end of the implicit loop. perldoc perlrun gives the clue on how to do
    this correctly.
    (Other problems in your code involve using $ne one place and $nx another
    and doing more regular expression processing than you need to).

    perl -i.old -ne '$n++ if s/^(\s*HOSTNAME\s*)=.*/$1=linux.com/; print $_;
    if (eof && $n==0 ){ print "HOSTNAME=linux.com\n" }' network


    read perldoc perlrun for -i and perldoc perlfunc for eof

    Paul Lalli
     
    Paul Lalli, May 24, 2004
    #3
  4. Sundaram Ramasamy

    Paul Lalli Guest

    On Mon, 24 May 2004, Joe Smith wrote:

    > Sundaram Ramasamy wrote:
    >
    > > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {

    >
    > You don't want to set $ne to zero everytime through the loop.
    >
    > perl -i.old -ne '$ne=0 if not defined $ne; if( /^\s*HOSTNAME\s*=/ ) {
    >
    > -Joe
    >


    Seeing as the OP wasn't using $ne anywhere else in the one-liner, that
    wasn't one of his problems at all, actually.

    Paul Lalli
     
    Paul Lalli, May 24, 2004
    #4
  5. Sundaram Ramasamy

    Paul Lalli Guest

    On Mon, 24 May 2004, Paul Lalli wrote:

    > On Mon, 24 May 2004, Sundaram Ramasamy wrote:
    >
    > > I want to check in file line start with HOSTNAME, then I want to
    > > replcae HOSTNAME value to linux.com, if line is not there I want add
    > > new line HOSTNAME=linux.com
    > >
    > > Using inplace editing I was not able to add new line.
    > >
    > > Here is my one liner inplace editing script
    > >
    > > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {
    > > s/=.*$/=linux.com/; $nx++; print $_; }else { print $_; } END { if( $nx
    > > ==0 ){ $_="HOSTNAME=linux.com\n"; print $_; } }' network

    >
    > Your main problem is that when run via -i, perl selects STDOUT after the
    > end of the implicit loop. perldoc perlrun gives the clue on how to do
    > this correctly.
    > (Other problems in your code involve using $ne one place and $nx another
    > and doing more regular expression processing than you need to).
    >
    > perl -i.old -ne '$n++ if s/^(\s*HOSTNAME\s*)=.*/$1=linux.com/; print $_;
    > if (eof && $n==0 ){ print "HOSTNAME=linux.com\n" }' network


    If and only if you're willing to let the HOSTNAME line move in the file,
    this might be a slightly less messy way of accomplishing your goal.

    [untested]
    perl -i.old -ne 'print unless /^\s*HOSTNAME\s*=/; print
    "HOSTNAME=linux.com\n" if eof;'

    (The caveat is that the modified file will always have the HOSTNAME line
    at the end of the file, regardless of where it might have been in the
    original)

    Paul Lalli
     
    Paul Lalli, May 24, 2004
    #5
  6. Sundaram Ramasamy wrote:
    >
    > I want to check in file line start with HOSTNAME, then I want to
    > replcae HOSTNAME value to linux.com, if line is not there I want add
    > new line HOSTNAME=linux.com
    >
    > Using inplace editing I was not able to add new line.
    >
    > Here is my one liner inplace editing script
    >
    > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {
    > s/=.*$/=linux.com/; $nx++; print $_; }else { print $_; } END { if( $nx
    > ==0 ){ $_="HOSTNAME=linux.com\n"; print $_; } }' network
    >
    > Requirment:
    >
    > 1) orginal file:
    > NMAE=myname
    > IP=234.56.43.23
    > HOSTNAME=abcde.com
    >
    > I need out put:
    > NMAE=myname
    > IP=234.56.43.23
    > HOSTNAME=linux.com
    >
    > 2) orginal file:
    > NMAE=myname
    > IP=234.56.43.23
    >
    > I need out put:
    > NMAE=myname
    > IP=234.56.43.23
    > HOSTNAME=linux.com
    >
    > Any tips for this



    perl -i.old -0pe'
    s/(?<=HOSTNAME)(\s*=\s*.+)/=linux.com/
    ||
    s/\z/HOSTNAME=linux.com\n/
    ' network



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 25, 2004
    #6
  7. "John W. Krahn" <> wrote in message news:<>...
    > Sundaram Ramasamy wrote:
    > >
    > > I want to check in file line start with HOSTNAME, then I want to
    > > replcae HOSTNAME value to linux.com, if line is not there I want add
    > > new line HOSTNAME=linux.com
    > >
    > > Using inplace editing I was not able to add new line.
    > >
    > > Here is my one liner inplace editing script
    > >
    > > perl -i.old -ne '$ne=0; if( /^\s*HOSTNAME\s*=/ ) {
    > > s/=.*$/=linux.com/; $nx++; print $_; }else { print $_; } END { if( $nx
    > > ==0 ){ $_="HOSTNAME=linux.com\n"; print $_; } }' network
    > >
    > > Requirment:
    > >
    > > 1) orginal file:
    > > NMAE=myname
    > > IP=234.56.43.23
    > > HOSTNAME=abcde.com
    > >
    > > I need out put:
    > > NMAE=myname
    > > IP=234.56.43.23
    > > HOSTNAME=linux.com
    > >
    > > 2) orginal file:
    > > NMAE=myname
    > > IP=234.56.43.23
    > >
    > > I need out put:
    > > NMAE=myname
    > > IP=234.56.43.23
    > > HOSTNAME=linux.com
    > >
    > > Any tips for this

    >
    >
    > perl -i.old -0pe'
    > s/(?<=HOSTNAME)(\s*=\s*.+)/=linux.com/
    > ||
    > s/\z/HOSTNAME=linux.com\n/
    > ' network
    >
    >
    >
    > John


    Thanks for all,

    John can you explain the meaning of your script.

    SR
     
    Sundaram Ramasamy, May 25, 2004
    #7
  8. Sundaram Ramasamy wrote:
    >
    > "John W. Krahn" <> wrote in message news:<>...
    > >
    > > perl -i.old -0pe'
    > > s/(?<=HOSTNAME)(\s*=\s*.+)/=linux.com/
    > > ||
    > > s/\z/HOSTNAME=linux.com\n/
    > > ' network

    >
    > John can you explain the meaning of your script.


    The -0p switches open the files on the command line and read the
    complete contents of each file into the $_ variable in a while loop and
    print the contents of $_ at the end of the loop. The first substitution
    searches for '\s*=\s*.+' which is preceeded by the string 'HOSTNAME'
    using a zero-width look-behind assertion. If it is found it replaces
    '\s*=\s*.+' with '=linux.com' and if it is not found it runs the second
    substitution which appends "HOSTNAME=linux.com\n" to the end of $_.


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 26, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.verizon.net

    inplace edit error message

    news.verizon.net, Sep 29, 2003, in forum: Perl
    Replies:
    4
    Views:
    1,615
    Jim Gibson
    Oct 8, 2003
  2. msnews.microsoft.com

    DataGrid InPlace Editing

    msnews.microsoft.com, Jan 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    353
    msnews.microsoft.com
    Jan 29, 2005
  3. jason
    Replies:
    0
    Views:
    425
    jason
    Nov 9, 2005
  4. nutty
    Replies:
    6
    Views:
    399
    Tom Widmer
    Jun 15, 2006
  5. jrpfinch

    Inplace editing the elegant way

    jrpfinch, Oct 9, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    98
Loading...

Share This Page