- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Searching & Replacing string in a File
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
09-25-2003 04:07 PM
09-25-2003 04:07 PM
I've hundreds of file in one directory and want to create a script which searches for a specific string "AB-00" and replace with "AB-11". Any suggestions? -Sanjay
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 04:39 PM
09-25-2003 04:39 PM
Re: Searching & Replacing string in a File
do
rm ./sed_file 2>/dev/null
sed 's/AB-11/AB-11/' $i > ./sed_file
mv ./sed_file $i
done
this script will do... sed_file is a temp file. When you run this script ensure that it does not exist in your current dir
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 04:49 PM
09-25-2003 04:49 PM
SolutionThe script must be like as shown in this..
for i in `ls dirname`
do
rm ./sed_file 2>/dev/null
sed 's/AB-00/AB-11/' $i > ./sed_file
mv ./sed_file $i
done
and make sure that the script itself is not in the ls output (save it a different location)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 04:51 PM
09-25-2003 04:51 PM
Re: Searching & Replacing string in a File
sed "s/$var1/$var2/" file1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 04:54 PM
09-25-2003 04:54 PM
Re: Searching & Replacing string in a File
Thanks for your reply. Some concerns are:
a) Why not put the script in the same directory?
b) What 'dirname' refers to?
c) Why it's required to create the temp file?? - Sanjay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 05:22 PM
09-25-2003 05:22 PM
Re: Searching & Replacing string in a File
- unable to run from other directory
- when run from the same directory it's changing the permission in the script too.
Can you suggest the way to correctly run this from other dir??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 05:28 PM
09-25-2003 05:28 PM
Re: Searching & Replacing string in a File
Run this below script in your direcory where all your files need to be modified.
#!/usr/bin/ksh
for file in *
do
cp $file $file.bak
sed 's/AB-00/AB-11/g' $file.bak >$file
rm $file.bak
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 05:33 PM
09-25-2003 05:33 PM
Re: Searching & Replacing string in a File
Make sure you r not copying the script to the directory as it will change the scripts itself.
You just run those commands don't copy as a file.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 05:35 PM
09-25-2003 05:35 PM
Re: Searching & Replacing string in a File
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 05:49 PM
09-25-2003 05:49 PM
Re: Searching & Replacing string in a File
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 06:14 PM
09-25-2003 06:14 PM
Re: Searching & Replacing string in a File
Let me start from the beginning.. Assume that the files you want to change are in dir /temp/mydir
Copy the script that I had sent to you to /temp
Change the script, I mean the first line, ls dirname to ls /temp/mydir and execute the script.
Why it shud not be in the same dir is because, even your script will also get changed if it is in the same dir. hope this helps...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 06:18 PM
09-25-2003 06:18 PM
Re: Searching & Replacing string in a File
You need to modify the scripts file search path with absolute path.
Suppose your files need to be modified are in a directory called /home/data. Then the script should be
#!/usr/bin/ksh
for file in /home/data/*
do
cp $file $file.bak
sed 's/AB-00/AB-11/g' $file.bak >$file
rm $file.bak
done
I hope this addressed all your concerns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-25-2003 06:27 PM
09-25-2003 06:27 PM
Re: Searching & Replacing string in a File
It all worked perfectly fine. What I did also is changed the script to .name and all ok now. Thanks & appreciate for you support & quick response.