LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-05-2004, 11:29 AM   #1
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Rep: Reputation: 32
Using sed on several files at once


I have looked for this but no joy.

I know
sed s/x/y/g file1 file1
changes all the 'x's to 'y' in file1.
However I want to do this to multiple files at once.
What is the best way to do this.
(My shell scripting is rusty).
 
Old 05-05-2004, 03:04 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Code:
for i in file1 file2 file3
do
  sed ....... $i $i
done
 
Old 05-05-2004, 03:48 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by acid_kewpie
Code:
for i in file1 file2 file3
do
  sed ....... $i $i
done
If sed -V >= 4
Code:
  sed -i ....... $i
If sed < 4
Code:
  sed ....... $i > ./tmp
  mv ./tmp $i
 
Old 05-05-2004, 03:49 PM   #4
Blinker_Fluid
Member
 
Registered: Jul 2003
Location: Clinging to my guns and religion.
Posts: 683

Rep: Reputation: 63
or
cat file1 file2 file3 | sed 's/x/y/g'
 
Old 05-06-2004, 04:13 AM   #5
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Original Poster
Rep: Reputation: 32
I have tired this but I just can not get them to work.
Surely sed can has to go a new file not the old one.
 
Old 05-06-2004, 08:21 AM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Here is an example where the changes are made to all of the .txt files.

for i in *.txt; do j=`echo $i | sed -e 's/x/y/g'`; mv "$i" "$j"; done
 
Old 05-06-2004, 02:21 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by homey
Here is an example where the changes are made to all of the .txt files.

for i in *.txt; do j=`echo $i | sed -e 's/x/y/g'`; mv "$i" "$j"; done
That's just renaming the files, replacing a x
in the files name with a y, rather than
changing the files contents?



Cheers,
Tink
 
Old 05-06-2004, 02:25 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally posted by davholla
I have tired this but I just can not get them to work.
Surely sed can has to go a new file not the old one.
Which version of sed are you using?

Tricky's script with my version dependent variations
should work (works here, with sed 4.0.9 in the -i
variant (in-place replacement).


Cheers,
Tink
 
Old 05-06-2004, 03:17 PM   #9
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Linux Mint 13 Maya
Posts: 729

Original Poster
Rep: Reputation: 32
I don't know what version of sed.
It was HP unix at work I wanted to do this but using Suse 8.2 at home I get the same it only works if you makes the changes to a new version of the file
 
Old 05-06-2004, 03:34 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
sed -V
 
Old 05-06-2004, 04:00 PM   #11
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Guess I should have used cat instead of echo.

Something like this....

Code:
#!/bin/bash

for i in *.txt; do
cat $i | sed -e 's/x/y/g' > $i
done
 
Old 05-12-2004, 06:39 AM   #12
ruiseixas
LQ Newbie
 
Registered: Jul 2003
Location: Portugal
Distribution: redhat 9
Posts: 17

Rep: Reputation: 0
Hi.

I have the same problem, but I solve it, with out the need of any scrypt and without the side effect of changing the permissions or the owner of the file, because the mv command.

You say that

Quote:
sed s/x/y/g file1 file1
changes all the 'x's to 'y' in file1.
but is not truth, I try and do not work, even for a single file :-( ...
that because the sed is a little limited, they can't change a file while reading him.

But i find a simple way of do what you want, a simple command, nothing more nothing less.

The command is the follow.

Code:
find . -name "*.html" -exec sh -c 'sed "s/x/y/g" $0 > $0.tmp; cat $0.tmp > $0; rm $0.tmp' {} \;
This command it's simple to change to your needs, and define what files do you wnat to change or not.
And if you dont want to go through the subfolders you can just add
Code:
-maxdepth 0
to the find command.
You can type
Code:
man find
to know more about it.

And remember, if you want to find the solution, it's because find is the solution :-)
Just kidding...
 
Old 04-03-2005, 07:47 AM   #13
basher400
Member
 
Registered: Mar 2005
Posts: 54

Rep: Reputation: 15
I have bit more complicated problem I can't figure out how to solve.

I need to search and replace a pattern in an unknow number of files in an unknown number of folders.

i'm using this to list them:
ls jaras/200*/dan/*.html

in every file like this I need to replace 'romer' with 'romaz'.
all I know is that this is the sed format:
sed -e 's/romer/romaz/g' *.html

anyone?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] working on files with sed and pipe angel115 Linux - Newbie 4 10-23-2005 06:15 PM
bash: cp files and sed nachtfalter Programming 3 10-04-2004 07:06 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
cannot delete certain files with sed ganninu Linux - General 6 12-10-2003 03:31 PM
saving files after sed processing raypen Linux - General 2 07-23-2002 06:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration