Operating System - Linux
1755055 Members
2839 Online
108829 Solutions
New Discussion юеВ

Need help with scripting mass file edits..

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Need help with scripting mass file edits..

Hello,

I am wanting to know a way to shell (ksh)script-edit a file by having a script that searches for a specific string, and then inputs lines after that specific string. Please help, as I will be up all night if I can't figure this out.
15 REPLIES 15
James R. Ferguson
Acclaimed Contributor

Re: Need help with scripting mass file edits..

Hi Patrick:

I am going to assume that you want to preserve the entire contents of the line on which your match occurs, and simply add some number of lines thereafter.

This script will do that for any number of files *and* update them "in-place":

By example, using 'localhost' as a string to be matched (e.g. as with '/etc/hosts'):

# perl -pi.old -e 's/(localhost.*)/$1\nnew\nlines\nhere/' file1 file2 file3

The original file will be preserved with an ".old" suffix and the file updated in-place.

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Need help with scripting mass file edits..

Perhaps I should have stated that I wanted to input lines of text after the string that is being searched out.
James R. Ferguson
Acclaimed Contributor

Re: Need help with scripting mass file edits..

Hi (again):

> Perhaps I should have stated that I wanted to input lines of text after the string that is being searched out.

OK, and do what with any text that follows the match on the line?

This discards any text on the line on which the pattern matches and inserts new test lines thereafter:

# perl -pi.old -e 's/(localhost)/$1\nnew\nlines\nhere/' file1 file2 file3 ...

Change the pattern in parentheses to anything you want.

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Need help with scripting mass file edits..

I would want to keep the text on that line, but I guess a solution would be to let that text be discarded, and re-add it in the replacement text.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need help with scripting mass file edits..

Hi:

> I would want to keep the text on that line, but I guess a solution would be to let that text be discarded, and re-add it in the replacement text.

OK, solution-1 retains it. Solution-2 discards it and this one (solution-3) pushes text that trails the matched pattern to its own line and then inserts the line(s) you specify:

# perl -pi.old -e 's/(localhost)(.*)/$1\n$2\nnew\nlines\nhere/' file1 file2 file3 ...

Regards!

...JRF...
Patrick Ware_1
Super Advisor

Re: Need help with scripting mass file edits..

Nice! I just tested this out, and it works pretty dang good!! Thanks so much for your responses. Solution 3 was the one.
Patrick Ware_1
Super Advisor

Re: Need help with scripting mass file edits..

Wait, what if I wanted to enter the followng after the string:

Cmnd_Alias EPROV_ADMIN_CMDS = /usr/sbin/groupadd, /usr/sbin/groupdel, /usr/sbin/groupmod, \
/usr/bin/last, /usr/bin/listusers, /usr/sbin/logins, \
/usr/sbin/usermod, /usr/sbin/useradd, /usr/sbin/userdel, \
/usr/bin/passwd, /usr/bin/ypmatch, /usr/bin/yppasswd, \
/usr/sbin/ypcat, /usr/bin/login


Is there a way to do this?
Patrick Ware_1
Super Advisor

Re: Need help with scripting mass file edits..

I will be up all night if I can't get this figured out. I just got tasked (last minute) to change the sudoers file on 398 hosts by by tomorrow morning.
Steven Schweda
Honored Contributor

Re: Need help with scripting mass file edits..

I have an aversion to doing someone else's
job for him when he's getting paid for it
and I'm not, but if you expect someone else
to do it for you, you might consider
precisely and accurately specifying the work
you want done. In this case, one way to do
that might be to show an example of what the
file would look like before and after the
change(s) you'd like to make.

When a reply begins, "I am going to assume",
then _you_ can assume that you didn't specify
the problem clearly enough.