Operating System - HP-UX
1751695 Members
4803 Online
108781 Solutions
New Discussion

Re: changing a line in multiple .profiles

 
SOLVED
Go to solution
John Jimenez
Super Advisor

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
7 REPLIES 7
Dennis Handly
Acclaimed Contributor

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.

James R. Ferguson
Acclaimed Contributor
Solution

Re: changing a line in multiple .profiles

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...

John Jimenez
Super Advisor

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
James R. Ferguson
Acclaimed Contributor

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...

John Jimenez
Super Advisor

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
James R. Ferguson
Acclaimed Contributor

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...

H.Merijn Brand (procura
Honored Contributor

Re: changing a line in multiple .profiles

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