HPUX find string in directory and filetype and replace string

Hi,

Here's my dilemma.

I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories

Is this possible?

Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below

I have scoured the forum and have found the following commands but cannot get any to do what I:mad::wall: am trying to do :frowning:

	  grep -rl 'Sept_2012' ./ | xargs sed -i 's/Sept_2012/Oct_2012/g'
	  
	  sed '/Sept_2012/ {n; s/Sept_2012/Oct_2012/;}'

	  awk '/Sept_2012/{x++}x{sub("Sept_2012","Oct_2012")&&x++&&x=(x==2)?0:x}1' infile
	  
	  cat sample.txt | sed -e "s/$SEARCH/$REPLACE/" >> result.txt
	  
	  perl -p -i -e 's/original text string/replacementstring/g' file

Try This

#!/bin/sh
for fl in *.py ; do
mv $fl $fl.old
sed 's/Sept_2012/Oct_2012/g' $fl.old > $fl
rm -f $fl.old
done

Actually I figured out the find command just need to know how to replace now?

find . -name "*.py" -exec grep "Oct_2012" {} \;

---------- Post updated at 01:24 PM ---------- Previous update was at 01:14 PM ----------

Thanks for the reply however the code below didn't work

#!/bin/sh
for fl in *.py ; do
mv $fl $fl.old
sed 's/Sept_2012/Oct_2012/g' $fl.old > $fl
rm -f $fl.old
done
find . -type f -name ".*py" -exec sed -i 's/sep_2012/Oct_2012/g' {} \;

Unfortunately this didn't work either

find . -type f -name ".*py" -exec sed -i 's/sep_2012/Oct_2012/g' {} \;

Oh Sorry it should be

"*.py"
find . -type f -name "*.py" -exec sed -i 's/Sept_2012/Oct_2012/g' {} \;

Produced the following error: -

Usage: sed [-n] [-e script] [-f source_file] [file...]
sed: illegal option -- i

Strange. Both of the solution work for me . Iam working on Linux red hat. What error you got for that

for loop 

solution.

Ah that could be why I am on HPUX

can you try

sed -e

rather than

-i

.However it will not update the file directly. Can you try this.

Only GNU sed has -i option.
The previous code should have worked; the for loop scans the current directory.
"find" scans the current directory *tree* (recursively).
You can replace the for loop by a while loop with read, and connect with a pipe:

#!/bin/sh
find . -type f -name "*.py" -print |
while read fp
do
 cp -p "$fp" "$fp".old &&
 sed 's/Sept_2012/Oct_2012/g' "$fp".old > "$fp" &&
 rm -f "$fp".old
done

Note the safety enhancements:
Always quote "$variables" with unknown contents!
cp to a backup - mv breaks links!
An && at the end of a command only continues with the next command if successful.

Excellent the code works when I put it into a shell script only a couple of problems.

1) I didn't know how to get a log of the files changed unless I ran the command below
2) All the .py files it searched got touched so I get today's time stamp on the directories it looks in

#!/bin/sh
find . -type f -name "*.py" -print |
while read fp
do
 cp -p "$fp" "$fp".old &&
 sed 's/Sept_2012/Oct_2012/g' "$fp".old > "$fp" &&
 rm -f "$fp".old
done
find . -name "*.py"  -exec grep "Oct_2012" {} \;

Sorry I thought you want all files changed.
Replaced -print by -exec grep -l ...
that prints only the matching file names:

#!/bin/sh
search="Sept_2012"
replace="Oct_2012"
find . -type f -name "*.py" -exec grep -l "$search" {} \; |
while read fp
do
 echo "$fp"
 cp -p "$fp" "$fp".old &&
 sed "s/$search/$replace/g" "$fp".old > "$fp" && 
 rm -f "$fp".old
done

Hi MadeInGermany,

Yes you are right we should always use the quotes for the variable

" "

.I just forget that in my reply. Also thanks for giving some tips on using

mv

and

cp

.:slight_smile: