Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl inplace editing

Reply
Thread Tools

Perl inplace editing

 
 
Sundaram Ramasamy
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-25-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
>
> 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
 
Reply With Quote
 
Sundaram Ramasamy
Guest
Posts: n/a
 
      05-25-2004
"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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      05-26-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inplace editing the elegant way jrpfinch Perl Misc 4 10-12-2008 06:06 PM
gcc needs copy consturctor for inplace constructed object passed to func as reference, others not nutty C++ 6 06-15-2006 02:02 PM
asp 1.1 datagrid inplace editing, utf-8 code saved as "??" ? jason ASP .Net 0 11-09-2005 06:57 AM
DataGrid InPlace Editing msnews.microsoft.com ASP .Net 0 01-29-2005 10:11 AM
inplace edit error message news.verizon.net Perl 4 10-08-2003 09:43 PM



Advertisments
 



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