Hello Damian,
I will be very obliged if you could help me with this query.I have used the code you have shared on this forum to replace one single word in all my files but I get an error. I am not sure why I am getting this error. Whether this is a ubuntu specific problem or a problem with my script.
Here is what I run:
Code:
#!/bin/bash
for fname in $(find . -name "*" -print)
do
sed '/word/s/rs#/rs/g' ${fname} > ${fname}.new &&
mv ${fname}.new ${fname}
done
Here is the error I get:
replace_word.sh: 3: Syntax error: word unexpected (expecting "do")
My environment is:
Linux version 2.6.32-24-server (buildd@yellow) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) )
Thanks
biobee
Quote:
Originally Posted by Damian Ibbotson
I would do something like this for multiple files...
for fname in $(find . -name "*" -print)
do
sed '/word/s/word/new_word/g' ${fname} > ${fname}.new &&
mv ${fname}.new ${fname}
done
The 'find' command will output every filename that matches '*' (i.e. EVERY file) starting from the current directory ('.')
The 'for' loop will obviously cause the nested statements to occur for the list of substituted filenames.
The 'sed' command will substitute the word you require. Note: the initial occurrence of the word 'word' is not necessary but it makes the command more efficient as sed will only look to perform the substitution on lines that contain the match (as opposed to every line by default).
The use of '&&' is important because it means that the following command (i.e. 'mv') will only execute if the 'sed' command executed successfully.
|