- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ksh: replacing a line in many files under a dir.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2002 07:45 AM
04-21-2002 07:45 AM
I have a directory and it contains a lot of files. 90% of these files include a particular email address, "abc@abc.com". How do I write a ksh script to replace the address with some thing else, "xyz@xyz.com".
Thanks in advance.
Roger
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2002 07:55 AM
04-21-2002 07:55 AM
Solutionshort example:
#!/bin/ksh
cd /dir/
for i in *;do
cp $i $i.$$
sed -e 's/abc@abc.com/xyz@xyz.com/' $i.$$ >$i
rm $i.$$
done
this is just a quick hack, there are surely other and maybe better ways
Heiner
P.S. make a copy of your files before you test it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2002 05:55 PM
04-21-2002 05:55 PM
Re: ksh: replacing a line in many files under a dir.
And if a single file can have "abc@abc.com" more than once, you may want to replace globally:
#!/bin/ksh
for i in *
do
sed "s/abc\@abc.com/xyz\@xyz.com/g" $i > $i.out
mv $i.out $i
done

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2002 06:26 PM
04-21-2002 06:26 PM
Re: ksh: replacing a line in many files under a dir.
You could use perl command line for this:
cd /toyourdirectory
perl -p -i -e 's/abc\@abc.com/xyz\@xyz.com/g' *
Will replace all occurrences in the file itself, no moving of files around ...
Thanks,
Shabu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2002 11:34 AM
05-31-2002 11:34 AM
Re: ksh: replacing a line in many files under a dir.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2002 11:36 AM
05-31-2002 11:36 AM
Re: ksh: replacing a line in many files under a dir.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2002 11:52 AM
05-31-2002 11:52 AM
Re: ksh: replacing a line in many files under a dir.
Use a -p on the cp command line - EX
cp -p file1 file2
the -p preserves perms & ownership.
Rgds,
Jeff
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2002 12:07 PM
05-31-2002 12:07 PM
Re: ksh: replacing a line in many files under a dir.
#!/bin/ksh
for i in *
do
sed "s/abc\@abc.com/xyz\@xyz.com/g" $i > $i.out
mv $i.out $i
done
Wouldn't the redirection make the new files have as owner whoever runs the script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-31-2002 12:16 PM
05-31-2002 12:16 PM
Re: ksh: replacing a line in many files under a dir.
Add a cp -p command just before the sed command:
#!/bin/ksh
for i in *
do
cp -p $i $i.out
sed "s/abc\@abc.com/xyz\@xyz.com/g" $i > $i.out
mv $i.out $i
done
Darrell