changing a line in multiple .profiles (530 Views)
Reply
Super Advisor
John Jimenez
Posts: 610
Registered: ‎02-17-2003
Message 1 of 8 (530 Views)
Accepted Solution

changing a line in multiple .profiles

Hello I want to make multiple changes of one line in hundreds of .profiles.      I do not script well and came up with this. 

 

I want change change a line in .profile that says

 

A=/1/2/3

 

to

 

A=1/4/5/6/3

 

 Will this work?

cd /home

find ./.profile -type f -exec perl -pi'.bak' -e 's/A=\/1\/2\/3/A=\/1\/4\/5\/6\/3' {} \;

Hustle Makes things happen
Acclaimed Contributor
Dennis Handly
Posts: 25,274
Registered: ‎03-06-2006
Message 2 of 8 (527 Views)

Re: changing a line in multiple .profiles

>I want to make multiple changes of one line in hundreds of .profiles.

 

If only 100s, there is no need to use find(1).


Assuming your perl syntax is correct:

cd /home

for profile in */.profile; do

   perl -pi'.bak' -e 's/A=\/1\/2\/3/A=\/1\/4\/5\/6\/3' $profile

done

 

I'm not sure if you will have ownership problems after you modify each .profile?  I.e. the ownership and permissions may be changed, so you may have to script cloning those.

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 8 (523 Views)

Re: changing a line in multiple .profiles

[ Edited ]

Hi:

 

Yes, you Perl command will work.  You can make it easier to read by writing:

 

# perl -pi.bak -e 's{A=/1/2/3}{A=1/4/5/6/3}' MYFILE

The idea is to use a different delimiter than a slash since the slash is one of the characters you want to match.  The {} ones are nice.

 

While the permissions of the modified file will be the same as the original, the ownership will be the default for the account running the command.

 

You can also specify *multiple* files without having to create separate instantiations of a Perl process.  Hence, you could do:

 

# perl -pi.bak -e 's{A=/1/2/3}{A=1/4/5/6/3}' /home/*/.profile

...to do all of them!

 

Regards!

 

...JRF...

Super Advisor
John Jimenez
Posts: 610
Registered: ‎02-17-2003
Message 4 of 8 (514 Views)

Re: changing a line in multiple .profiles

Thank you James.  That worked.      I am looking to assign everyone points, but have not figured out how this new forum works yet.

Hustle Makes things happen
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 5 of 8 (511 Views)

Re: changing a line in multiple .profiles


John Jimenez wrote:

Thank you James.  That worked.      I am looking to assign everyone points, but have not figured out how this new forum works yet.


There are now kudos in lieu of points.  Also, mark the solution that solved the problem.  Be aware, though, that marking a particular reply as the one that solved your problem does *not* generate a kudo.  Hence, please do both.  See:

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/kudos#kudos

 

http://h30499.www3.hp.com/t5/help/faqpage/faq-category-id/solutions#solutions

 

Regards!

 

...JRF...

Super Advisor
John Jimenez
Posts: 610
Registered: ‎02-17-2003
Message 6 of 8 (509 Views)

Re: changing a line in multiple .profiles

hmmmm I wanted to assign more then one kudos, but it only changed the first time I clicked it.

Hustle Makes things happen
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 7 of 8 (507 Views)

Re: changing a line in multiple .profiles


John Jimenez wrote:

hmmmm I wanted to assign more then one kudos, but it only changed the first time I clicked it.


Hi (again):

 

I'm sorry, you can't do that :-)  A kudo is all anyone can give.  It has been said that at some point some contributors will be able to give 1+ kudos based on their "rank" in the community.  I have no idea of the criteria that might define a higly "ranked" member, either.

 

I'm happy to have been of help.

 

...JRF...

Honored Contributor
H.Merijn Brand (procura
Posts: 6,189
Registered: ‎10-13-1997
Message 8 of 8 (501 Views)

Re: changing a line in multiple .profiles

[ Edited ]

Update: part of below info was already posted by JRF, but I only noticed after posting, as by some incident, JSF's post didn't show when I replied. Sorry for that. I also wanted to reply to Dennis' post, as his snippet has a syntax error.

 

Did you know that that s/// allows other delimiters? using / is really awful when changing /'s

cd /home
for profile in */.profile; do
    perl -pi.bak -e's{A=/1/2/3}{A=/1/4/5/6/3}' $profile
    done

Reads much better, right?  Besides that, it also prevents causing syntax errors like Dennis' substitute is missing a trailing /

Another nice thing about using {} pairs, is that you can split over lines. Useful for readability in scripts:

$var =~ s{A=/1/2/3}
         {A=/1/4/5/6/3};

 I hope your code shows in a fixed-width font, otherwise this example is useless.

 

Enjoy, Have FUN! H.Merijn
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.