- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: replace text string and rewrite file script
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
07-12-2001 04:27 AM
07-12-2001 04:27 AM
I'm looking for a script that'll recursively search files in a directory structure and replace all occurances of a string to the current hostname.
for example I've got around 30 files with a text string enter_hostname and I want a script that when I run the script with for example
./script -old enter_hostname -new hostname -recurse thisdir
I use find . * |xargs grep enter_hostname to search for the hostname but it finds strings in binary files that I need to avoid rewriting.
I just want to rewrite text files.
any ideas/suggestions?
Later,
Bill
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 04:59 AM
07-12-2001 04:59 AM
Re: replace text string and rewrite file script
do a file * in the directory file and grep those that are described as ascii text only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 05:02 AM
07-12-2001 05:02 AM
Re: replace text string and rewrite file script
A quick suggestion is to use 'file' to filter out non-ascii candidates from your process stream. I know how much you hate 'awk', so:
For example:
# file /etc/hosts|awk '{print $2}'
...returns ascii
# file /usr/bin/vi|awk '{print $2}'
...returns s800
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 05:44 AM
07-12-2001 05:44 AM
Re: replace text string and rewrite file script
you can use command "/usr/bin/file" to check for ascii or binary.
Sachin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 06:53 AM
07-12-2001 06:53 AM
Re: replace text string and rewrite file script
I've got to do some sort of file io script that
seds the file perhaps and replaces all occurances of string with newstring... and then rewrites the file.
At the moment, I list all files, then vi them all and use :1:$/s/oldstring/newstring/g
in vi.
I want a script to do it...
Later,
Bill
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 06:59 AM
07-12-2001 06:59 AM
Re: replace text string and rewrite file script
Once youve identified the ascii text filenames then simply do;
cat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 07:05 AM
07-12-2001 07:05 AM
Re: replace text string and rewrite file script
> do
> ex ${f}
where exscript contains your vi command
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 07:19 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-12-2001 07:51 AM
07-12-2001 07:51 AM
Re: replace text string and rewrite file script
#!/usr/bin/sh
#
typeset OLD=$1
typeset NEW=$2
#
for F in *
do
if [ `file $F|awk '{print $2}'` = ascii ]
then
sed "s/$OLD/$NEW/g" $F > ${F}.new
mv $F.new $F
fi
done
#.end.
...JRF...