Perl inplace editing

S

Sundaram Ramasamy

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
 
J

Joe Smith

Sundaram said:
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
 
P

Paul Lalli

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
 
P

Paul Lalli

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
 
P

Paul Lalli

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
 
J

John W. Krahn

Sundaram said:
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
 
S

Sundaram Ramasamy

John W. Krahn said:
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
 
J

John W. Krahn

Sundaram said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top