Operating System - Linux
1753814 Members
7615 Online
108805 Solutions
New Discussion юеВ

PERL one-liner help needed (text search and replace)

 
SOLVED
Go to solution
Mel Burslan
Honored Contributor

PERL one-liner help needed (text search and replace)

Since I have never got to master perl, I am lost here. So I will not even make an attempt at this one liner

All I want from this one liner is to replace a portion of a line in a given file. To be more specific, I need to disable the prm daemon on 50+ servers. To do this, I need to change the the line

PRM_RMTCONF=1

to

PRM_RMTCONF=0

So, what I am expecting from this one-liner is to look for the line, containing PRM_RMTCONF string and replce the value after the "=" to 0 (zero) regardless if it is 1 or 0 in the file /etc/rc/config.d/prm

It needs to be a one-liner because I want to launch it from a trusted management server via ssh and run it in a command line using a non-interactive session.

I know perl is created situations like this but I do not know how to attack this problem.

Help is greatly appreciated.
________________________________
UNIX because I majored in cryptology...
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: PERL one-liner help needed (text search and replace)

Hi Mel:

# perl -pi.old e 's/PRM_RMTCONF=\d\b/PRM_RMTCONF=0/' file

...This will substitute PRM_RMTCONF= with PRM_RMTCONF=0. The old version of the input file will be retained as "file.old" and the new editted version remain as "file". This is an inplace update.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: PERL one-liner help needed (text search and replace)

It's very easy using -i and -p.

perl -p -i -e 's/PRM_RMTCONF=1/PRM_RMTCONF=0/' myfile

This will modify "myfile" in place.

If you like you can add an argment to the -i switch:

perl -p -i.old -e 's/PRM_RMTCONF=1/PRM_RMTCONF=0/' myfile

and this has the effect of making a backup file myfile.old while the modified file, myfile, is rewritten.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: PERL one-liner help needed (text search and replace)

Hi (again() Mel:

Ooops! I dropped the "-" in fron of the "-p" switch:

# perl -pi.old -e 's/PRM_RMTCONF=\d\b/PRM_RMTCONF=0/' file

Regards!

...JRF...
Sandman!
Honored Contributor

Re: PERL one-liner help needed (text search and replace)

imho...here's an ex solution in case you're interested:

# ex -s +"/^PRM_RMTCONF=/s/[0-9]*$/0/ | wq" /etc/rc.config.d/prm
Mel Burslan
Honored Contributor

Re: PERL one-liner help needed (text search and replace)

Thanks for all of the responses. PERL one-liners worked like charm.
________________________________
UNIX because I majored in cryptology...