- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- replace word 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
11-25-2007 11:31 PM
11-25-2007 11:31 PM
replace word in a file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2007 11:45 PM
11-25-2007 11:45 PM
Re: replace word in a file
For single file use
cat
This will replace all error word into message.
For all files into folder you can run a for loop with this command inside.
-=ShRi=-
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2007 11:45 PM
11-25-2007 11:45 PM
Re: replace word in a file
sed 's/error/message/g'
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2007 11:50 PM
11-25-2007 11:50 PM
Re: replace word in a file
Create a script named process.sh as follows:
#!/bin/sh
until [ -z "$1" ]; do
if [ -f $1 ]; then
cat $1 | sed s/error/message/g> /tmp/newfile
cp /tmp/newfile $1
fi
#
shift
done
then run the script as follows:
./process.sh
hope this helps!
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2007 11:56 PM
11-25-2007 11:56 PM
Re: replace word in a file
below an example that i used last time:
Server1: home/yogeeraj> ALLFILES=$(ls -1 /home/yogeeraj/mydir/*.htm)
Server1: home/yogeeraj> ./process.sh $ALLFILES
hope this helps too!
kind regards
yogeeraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2007 11:57 PM
11-25-2007 11:57 PM
Re: replace word in a file
if you have several files in one dir the following script will rename them :
for i in `ls
do
k=`echo $i|sed 's/
mv $i $k
done
Regards
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 12:03 AM
11-26-2007 12:03 AM
Re: replace word in a file
You need to be more explicit at several levels. I'll assume that you want to recursively find every *file* in a particular directory and that you want to replace the word "error" (in either upper or lower case?) whenever it appears on a line:
# cd /path && find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \+
THis will perform the operation in your current directory; not cross mountpoints; find only files; and in-place replace every occurance of "error" with "message" _as_long_as_ the string "error has a boundry around it (whitespace, end-of-line, beginning of line, etc.). The update will be done to the file, in place, but a backup copy will be left as the name of the file suffixed with ".old".
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 12:59 AM
11-26-2007 12:59 AM
Re: replace word in a file
I think below script is good , but I can't run it , can advise how should I modify it to suit my requirement ? thx
find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 01:58 AM
11-26-2007 01:58 AM
Re: replace word in a file
If perchance your release level is 11.0 or earlier, substitute a semicolon for the "+" character at the end:
# find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \;
If that doesn't work, post the error you receive.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 02:34 AM
11-26-2007 02:34 AM
Re: replace word in a file
sed(1) takes a filename, no need to use cat(1). AND reading the file at the same time you are overwriting it will get you into trouble. Using /error/ in both the pattern and the substitution is overkill.
>yogeeraj: below an example that i used last time:
You can combine the two:
$ ./process.sh /home/yogeeraj/mydir/*.htm
>Franky: if you have several files in one dir the following script will rename them
ust3 wanted to change the contents not the name.
>Franky: k=`echo $i|sed 's/
There are problems here. You likely are changing all files to the same name. This will make all but the last go poof. You probably want to put $i in for old_file_name but then you need to use "". And you aren't likely to need the "g" at the end.
>JRF: If perchance your release level is 11.0 or earlier, substitute a semicolon for the "+" character at the end:
-exec ... + is supported on 11.00:
http://docs.hp.com/en/B2355-90680/find.1.html
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 12:19 PM
11-26-2007 12:19 PM
Re: replace word in a file
find . -xdev -type f -exec perl -pi.old -e 's/\berror\b/message/ig' {} \;
I think it will be work in unix , but if I want to use it in linux , can advise how can I change the script ? thx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 12:45 PM
11-26-2007 12:45 PM
Re: replace word in a file
> I think it will be work in unix , but if I want to use it in linux , can advise how can I change the script ?
There is nothing to change unless you need to add an absolute path to either the 'find' or the 'perl' executable. The syntax should work equally well in GNU/Linux.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 12:48 PM
11-26-2007 12:48 PM
Re: replace word in a file
By the way, once you find that you are satisfied with the answers that you have received, please give consideration to this:
https://forums1.itrc.hp.com/service/forums/helptips.do?#28
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 03:12 PM
11-26-2007 03:12 PM
Re: replace word in a file
back to the below script ,
for i in `ls
do
k=`echo $i|sed 's/
mv $i $k
done
when I run it , the error is
mv: `file1' and `file2' are the same file
can advise how can I change it ? thx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2007 06:17 PM
11-26-2007 06:17 PM
Re: replace word in a file
Did you see what I said about the script?
This will make all but the last go poof.
>can advise how can I change it?
Why would you want to use this script after I mentioned all it its problems? If you want to RENAME mass quantities of files, it would still need some changes.