1752805 Members
5622 Online
108789 Solutions
New Discussion юеВ

Scripting Question

 
SOLVED
Go to solution
Michael Williams_6
Trusted Contributor

Scripting Question

Hello all (again, I swear I post more than anyone else on here!),

We're using SuSE here and the installation can be a little manual at time. Now most installs we use a Ghost image - so that's easy, but there are a couple of custom installs where we can't use Ghost and need to make lots of manual modifications to files that I'd like to automate.

Now, consider the /etc/profile file as a good example. My default one has the line:

test -z "$MAIL" && MAIL=/var/spool/mail/$USER

But I'd like that just to be (for backward compatibility in users' profiles):

MAIL=/var/spool/mail/$USER

Now I could achieve this in a script by:

sed -e 's/test -z "$MAIL" && MAIL=\/var\/spool\/mail\/$USER/MAIL=\/var\/spool\/mail\/$USER/' /etc/profile > /tmp/profile2; mv /tmp/profile2 /etc/profile

Which is a bit messy, and if I have multiple lines to modify in the file, then I'm going to a lot of file copying.

Can anyone suggest a simpler method to automate this? It would be nice if the file could be modified directly rather than having to use redirects...

Any ideas chaps?

Mike
2 REPLIES 2
Alexander Chuzhoy
Honored Contributor
Solution

Re: Scripting Question

perl -p -i.bak -w -e 's/change/replaced/g' filename

will replace each "change" string with "replaced" in file named "filename" and it will create a backup "filename.bak" in case something went wrong.
-p option tells perl to write a program
-i.bak stands for extension of a backup
-w turns on warnings
-e says that executable code follows.


Michael Williams_6
Trusted Contributor

Re: Scripting Question

My good man that's exactly what I'm looking for!

Works a treat!