Need help with scripting mass file edits..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with scripting mass file edits..
# 8  
Old 08-14-2007
I don't know enough about SED to know if there is a problem with the SED script.

One example of what I am trying to do is as follows:


Let say I wanted to add a user alias line to the sudoers file such as:

User_Alias SOME_ADMINS = user1

At the start of that section of the file, is the heading that tells what section it is:

# User alias specification

I would want to search for this headding, and add the user alias line after that, while not deleting anything else in the file.
# 9  
Old 08-14-2007
Someone gave me this perl solution:

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

What this example does is search for the string 'localhost', moves the existing text on the line after the string down a line, and inserts the new lines. It does this for 3 files( file1, file2, and file3). I tested this out like so:

Code:
perl -pi.old -e 's/(#Section 2)(.*)/$1\n$2\It Worked!!!/' file1

This was the result I found when I looked at file1:

Before:

#Section 1
This is some stuff I am typing in this file for space.

#Section 2


After (I did it several times):

#Section 1
This is some stuff I am typing in this file for space.

#Section 2
It Worked!!!
It Worked!!!
It Worked!!!
It Worked!!!
It Worked!!!



The problem comes when I try to enter this:

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

What would be the best way to enter this? In other words, I need to be able to enter absolute paths to files.

Last edited by LinuxRacr; 08-14-2007 at 09:37 PM..
# 10  
Old 08-14-2007
Quote:
Originally Posted by akdwivedi
You can also try sed

place all the replace instances in a file ( sed_file) with entries
s/[search string1]/[replace string1]/g
s/[search string2]/[replace string2]/g

Then to make the replacements do as :

$ sed -f sed_file [input file]
As I stated above, it may not be the best approach. 'sed' searches for [search string] and replaces it with [replace string]

As per your requirement, you want the search string as well as the replaced string on the next line.

Note: if you have many replacements in a file make a temp file [sed_file] and input in it the following entries --
s/[search string1]/"[search string1] \n [replace string1]"/g
s/[search string2]/"[search string2] \n [replace string2]"/g

save this file and to make the global replacement in any file (input_file) run the following command -

sed -f sed_file input_file.

The sed command here will look for replacements from sed_file and make proper changes in the input_file.
Let me know if this helps.

Abhi.
This User Gave Thanks to akdwivedi For This Post:
# 11  
Old 08-14-2007
Code:
change="User_Alias SOME_ADMINS = user1"
awk -v c="$change" '{print}
/User alias specification/{print c}
' "file" > temp
mv temp file

This User Gave Thanks to ghostdog74 For This Post:
# 12  
Old 08-14-2007
But will this work with the list of absolute path names above? Please help!!
# 13  
Old 08-14-2007
Quote:
Originally Posted by ghostdog74
Code:
change="User_Alias SOME_ADMINS = user1"
awk -v c="$change" '{print}
/User alias specification/{print c}
' "file" > temp
mv temp file

Thanks, I'll give this a try.
# 14  
Old 08-15-2007
This has been working great. I found out that the awk argument can't take more than 399 bytes. About to find out how scalable it is.

Last edited by LinuxRacr; 08-15-2007 at 11:00 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[solved]removing characters from a mass of file names

I found a closed thread that helped quite a bit. I tried adding the URL, but I can't because I don't have enough points... ? Modifying the syntax to remove ! ~ find . -type f -name '*~\!]*' | while IFS= read -r; do mv -- "$REPLY" "${REPLY//~\!]}"; done These messages are... (2 Replies)
Discussion started by: rabidphilbrick
2 Replies

2. UNIX for Dummies Questions & Answers

multiple text edits inone pass

File_1 looks like: bunch of text Untitled Placemark bunch of text bunch of text Untitled Placemark bunch of text bunch of text Untitled Placemark bunch of text File_2 looks like: Title_001 Title_002 Title_003 First: I need to replace the 1st occurence of "Untitled Placemark"... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies

3. Homework & Coursework Questions

K&R C code edits

1. The problem statement, all variables and given/known data: 2. Relevant commands, code, scripts, algorithms: We have to do this using 'unix tools' and not use the script as if it were C. Meaning, he wants more uses of grep, sed, awk, cut, etc... than he does while, for, do's and done's.... (23 Replies)
Discussion started by: theexitwound
23 Replies

4. UNIX for Dummies Questions & Answers

OpenLDAP DB_CONFIG edits, changes live? or do I need run something

So I am probably missing something , but when I made edits to my DB_CONFIG file to fix form db_lock issues, the changes are not propagating after a service restart. Anyone know if I need to run anything else, or are the changes live? (0 Replies)
Discussion started by: jcejka
0 Replies

5. Shell Programming and Scripting

Multiple edits to a bunch of html files

I'm trying to upgrade a whole bunch of pages on my site to a new design. I thought one way of doing it would be to enclose the content in special comment tags and then use some form of script to wrap the new html around it. Like this: <!-- content start --> <h1>Blah blah blah</h1> yada yada... (9 Replies)
Discussion started by: dheian
9 Replies

6. Shell Programming and Scripting

mass file mv

I have 100k+ files in a directory. I wanna create new directories and move each 2500 files into a new directory. Thank you very much. (2 Replies)
Discussion started by: Sean2008
2 Replies

7. UNIX for Dummies Questions & Answers

Mass file renaming

Hi :) Is there any command I could use to rename a bunch of files resident of the same location to their original name plus a fixed text string of my own? Example: File1 File2 File3 Output: File1.txt File2.txt File3.txt This is easy using a "for" loop but what I want is a one-line... (5 Replies)
Discussion started by: Indalecio
5 Replies

8. AIX

VI questions : mass changes, mass delete and external insert

Is it possible in VI to do a global change but take the search patterns and the replacement patterns from an external file ? I have cases where I can have 100,200 or 300+ global changes to do. All the new records are inside a file and I must VI a work file to change all of them. Also, can... (1 Reply)
Discussion started by: Browser_ice
1 Replies

9. UNIX for Dummies Questions & Answers

mass delete a certain string in a .log file

Hey all. I have a file that has roughly 115,000 lines in it. There are a few lines of information that I don't want in it, but I don't want to search through all of the lines to find the ones that I don't want. Is there a way to do a mass delete of the lines that I don't want? Thanks for the... (4 Replies)
Discussion started by: jalge2
4 Replies
Login or Register to Ask a Question