Results 1 to 3 of 3
Hi,
Can somebody help me with a shell script to do the following.
It should segreagate binary files from text files in a folder and it should replace a word ...
- 08-18-2009 #1Just Joined!
- Join Date
- Jul 2003
- Posts
- 17
Script to replace word in text files
Hi,
Can somebody help me with a shell script to do the following.
It should segreagate binary files from text files in a folder and it should replace a word in all the text files.
Say for eg., it should replace NewYork with NY
tHX
- 08-18-2009 #2Just Joined!
- Join Date
- Aug 2009
- Location
- Mumbai, India
- Posts
- 75
To replace text in file
sed -i 's/old_text/new_text/g file_name
e.g.
sed -i 's/New York/NY/g' test_file
Hopefully this would answer half your question
--Syd
- 08-21-2009 #3Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
To test if a file is a text file, you can use command file, for example,
$ file test.txt
test.txt: ASCII text
Based on that, you can write your script like this:
Code:find . -type f | while read afile; do [ -n "`file $afile|grep text`" ] && sed -i 's/old_text/new_text/g' $afile; done


Reply With Quote