sed across multiple files | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

sed across multiple files

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-17-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
sed across multiple files

I've got a bunch of files (40 or so) and in each there is a substitution I need to perform.

I execuet the following sed command but it just make the changes to the screen without affecting the original file.

sed "s/, LA/,LA/g" *

(All files in the directory need this update).

So I tried redirecting...

for x in *
do
sed "s/, LA/,LA/g" $x > new_$x
done

And that worked. But I'm not sure about renaming the files back to what they were. mv obviously doesnt' like the wildcards and I'm not sure how to convert back.

I'd also like to know if there is a way to do this without piping to a new file...
Sponsored Links
    #2  
Old 01-18-2002
theDirtiest theDirtiest is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 13 September 2002, 9:18 AM EDT
Location: Maryland
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
my try

there is probably a much better way to do this. Please forgive me I have never done any shell programming before (i want to learn) and I just wanted to rack my brain trying to figure out an answer to your problem. here is what I got to work for me. it does however pipe to a new file. sorry i couldn't help you there.



for x in *
do
sed "s/, LA/,LA/g" $x > temp
cat temp > $x
rm temp
done


just make sure you don't already have a file named temp. if yo do then just change the name temp to a file you don't already have.

----------------------
tD

Last edited by theDirtiest; 01-18-2002 at 08:56 AM..
Sponsored Links
    #3  
Old 01-18-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
 
Join Date: Aug 2001
Last Activity: 18 April 2013, 3:03 PM EDT
Location: San Antonio, TX,
Posts: 1,070
Thanks: 0
Thanked 4 Times in 4 Posts
tweak script

I have a minor change to your script. Which will do the job.

Make a file with the listing of the files you want to change. This might be necessary if the directory your 40 files are in has other files that you don't want to change in it.

Make a file with only the filenames of your 40 files in it. Then do the script this way. (with backtics)


for x in `cat filename`
do
sed "s/, LA/,LA/g" $x > temp
cat temp > $x
rm temp
done


By default the 'sed' command will output all lines, even the ones it doesn't modify. If you did only want the lines that you modified to be output use this option 'sed -n ....'. This will suppress all lines that weren't modified.

Now that I think about it, you really don't need the 'rm temp' because your line, "sed "s/, LA/,LA/g" $x > temp", has only the '>' which will overwrite the temp file each time you come thru the 'for loop'.

So this will work as well.

for x in `cat filename`
do
sed "s/, LA/,LA/g" $x > temp
cat temp > $x

done




The Following User Says Thank You to Kelam_Magnus For This Useful Post:
figaro (12-11-2011)
    #4  
Old 01-18-2002
theDirtiest theDirtiest is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 13 September 2002, 9:18 AM EDT
Location: Maryland
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
true you don't need the rm temp, but I just added it so it wasn't left over when the script was finished.
Sponsored Links
    #5  
Old 01-21-2002
peter.herlihy peter.herlihy is offline
Registered User
 
Join Date: Nov 2001
Last Activity: 1 August 2006, 11:51 AM EDT
Location: New Zealand
Posts: 333
Thanks: 0
Thanked 0 Times in 0 Posts
Excellent..... in this instance all the files in the directory are to be modified...and i want all lines including those not modified,.. so a simple redirection to temp and back to $x will work just fine.

rm temp... harmless either way....so that's cool. Thanks guys..

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to find & replace a multiple lines string across multiple php files and subdirectories spfc_dmt Shell Programming and Scripting 12 03-07-2012 09:05 AM
Control multiple program instances - open multiple files problem hakermania Programming 3 02-06-2011 12:06 PM
Using AWK: Extract data from multiple files and output to multiple new files Liverpaul09 UNIX for Dummies Questions & Answers 3 10-12-2010 03:59 AM
best method of replacing multiple strings in multiple files - sed or awk? most simple preferred :) rich@ardz UNIX for Dummies Questions & Answers 19 08-27-2010 10:15 AM
extract multiple cloumns from multiple files; skip rows and include filenames; awk manishabh Shell Programming and Scripting 4 08-18-2009 10:55 PM



All times are GMT -4. The time now is 06:52 PM.