- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- find/replace in any text file in a whole directory...
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-2003 08:08 AM
11-25-2003 08:08 AM
find/replace in any text file in a whole directory structure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:12 AM
11-25-2003 08:12 AM
Re: find/replace in any text file in a whole directory structure
find . -type f -exec <script replacing texts>
If you want to make sure it is an ASCII file, the script needs to check the filetype with the file command.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:20 AM
11-25-2003 08:20 AM
Re: find/replace in any text file in a whole directory structure
find /test -type f -depth | while read FILE
do
sed "s/txt/text/g" < ${FILE} > ${FILE}.shiju
mv ${FILE}.shiju $FILE
done
Replace your directory name and text appropriately.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:29 AM
11-25-2003 08:29 AM
Re: find/replace in any text file in a whole directory structure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:36 AM
11-25-2003 08:36 AM
Re: find/replace in any text file in a whole directory structure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:47 AM
11-25-2003 08:47 AM
Re: find/replace in any text file in a whole directory structure
old="text to replace"
new="text to substitute"
find /test -type f -depth |
while read File
do
if [[ ! -r $File ]] ;then
print "$File isn't readable"
continue
fi
file $File | grep 'ascii text' >/dev/null 2>/dev/null
if [[ $? != 0 ]] ;then
print "$File isn't ascii text"
continue
fi
if [[ ! -r $File ]] ;then
print "$File isn't writable"
continue
fi
ex -s +"%s/$old/$new/g | wq" $File
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:49 AM
11-25-2003 08:49 AM
Re: find/replace in any text file in a whole directory structure
that last
if [[ ! -r $File ]] ;then
print "$File isn't writable"
continue
fi
should have been
#use -w to test for writable
#
if [[ ! -w $File ]] ;then
print "$File isn't writable"
continue
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 08:54 AM
11-25-2003 08:54 AM
Re: find/replace in any text file in a whole directory structure
grep "$old" $File >/dev/null 2>/dev/null
if [[ $? = 0 ]] ;then
print "modifing $File"
ex -s +"%s/$old/$new/g | wq" $File
else
print "no changes to $File"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 09:06 AM
11-25-2003 09:06 AM
Re: find/replace in any text file in a whole directory structure
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 05:17 PM
11-25-2003 05:17 PM
Re: find/replace in any text file in a whole directory structure
find . -type f -exec <script> {} ';'
So that the files are presented one-by-one to the script.
As for finding ascii files, that completely depends on what you think ascii files are. The file command will report them, but sometimes as 'commands text', sometimes as 'ascii file'. It depends on a lot of things what it will report. For instance a shell script is reported as commands text while the .profile is reported as ascii file, even though it is in fact a shell script too... It also depends on your magic file...
So what you need to do is come up with criteria for the names/contents of the files you want to alter, write those criteria in your script, have the script make backups so files wrongly touched can be put back, and then test it...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2003 11:47 PM
11-25-2003 11:47 PM
Re: find/replace in any text file in a whole directory structure
FROM -> text to replace
TO -> replace text to
find ${DIR} -type f -exec file {} \;|
grep text|cut -f1 -d':'|
while read fname
do
ed -s ${fname}<
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2003 04:26 AM
11-26-2003 04:26 AM
Re: find/replace in any text file in a whole directory structure
similar to heins solutions, I prefer to create an "ed"-inputfile to have the chance to review and test the editor-commands:
find DIR -type f .... | while read fname
do
echo e $fname
echo s/FROM/TO/g
echo w
done >/tmp/cmd.ed
After review:
ed -s
If you really want to do it immediately,
change the last line to
...
done | ed -s
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2003 02:06 AM
12-03-2003 02:06 AM