The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding Multiple Lines to Multiple Files dayinthelife Shell Programming and Scripting 2 06-04-2008 08:50 AM
Splitting input files into multiple files through AWK command arund_01 Shell Programming and Scripting 3 05-13-2008 06:17 AM
Multiple search in multiple files maxvirrozeito Shell Programming and Scripting 2 12-13-2007 10:32 AM
when I try to run rm on multiple files I have problem to delete files with space umen UNIX for Dummies Questions & Answers 1 09-20-2005 12:20 AM
Searching multiple files with multiple expressions Anahka Shell Programming and Scripting 6 01-07-2004 03:24 PM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-17-2002
Registered User
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
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...
__________________
Pete
Forum Sponsor
  #2  
Old 01-18-2002
Registered User
 

Join Date: Nov 2001
Location: Maryland
Posts: 20
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 06:56 AM.
  #3  
Old 01-18-2002
Kelam_Magnus's Avatar
Registered User
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
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




__________________
My brain is your brain
  #4  
Old 01-18-2002
Registered User
 

Join Date: Nov 2001
Location: Maryland
Posts: 20
true you don't need the rm temp, but I just added it so it wasn't left over when the script was finished.
  #5  
Old 01-21-2002
Registered User
 

Join Date: Nov 2001
Location: New Zealand
Posts: 333
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..

__________________
Pete
Google The UNIX and Linux Forums
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0