LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Changing multiple files with SED (https://www.linuxquestions.org/questions/linux-software-2/changing-multiple-files-with-sed-553739/)

timmy01 05-14-2007 08:05 AM

Changing multiple files with SED
 
hi,
I need to change multiple files located in different directories
and i can by using SED:

for f in `find . -type f`; do sed -i 's/old/new/g' $f; done

However, ALL dates of the files will be changed
even if nothing has been modified inside the file

so i want only the dates of the files renewed, when the file has been modified!

how can i adhieve this?
Please provide me with example if possible...


Cheerz
Jolan

ilikejam 05-14-2007 09:11 AM

Hi.

Add a grep for "old"
Code:

for f in `find . -type f`; do if grep "old" "$f"; then sed -i 's/old/new/g' "$f"; fi; done
Then you'll only be running sed on files whch contain "old".

Dave

timmy01 05-14-2007 09:51 AM

thanks.

do you know if its possible
to add a prompt? so that i can so yes or no
before it will be changed?

ilikejam 05-14-2007 02:44 PM

You'll probably want to do something like
Code:

read ANSWER; if [ "x$ANSWER" == "xyes" ]; then  sed .......; fi

timmy01 05-14-2007 03:03 PM

Quote:

Originally Posted by ilikejam
You'll probably want to do something like
Code:

read ANSWER; if [ "x$ANSWER" == "xyes" ]; then  sed .......; fi

thanks...

but i ran into another prob.
i need to grep a part of an url, before i can SED it...
but with this doesnt work:

grep " onMouseOver=\"window\.status='abc, def in \$var en ghi\.'; return true\"" * -R

the complet line where the url in a php file look like this:

echo " | <A HREF=\"http://www.domain$var.com/dir/file_nl.php?v1=$var&v2=$var2&file=abc\" onMouseOver=\"window.status='abc, abc in $var en abc.'; return true\">abc in $var</A>";

how to do this?
Cheerz!!

ilikejam 05-14-2007 03:26 PM

That's a pretty nasty string to be grepping for. Try using fgrep (or grep -F) instead of grep. I predict you'll still have to play around with escaping a bit to get it to match.

That, or you could be extremely lazy, and replace anything which you would have to escape with a '.', e.g.
Code:

grep " onMouseOver=.window.status=.abc, def in .var en ghi... return true."
It's possible it will match things you don't want it to, but under the circumstances, I don't think it'll be a problem. I wouldn't use this for mission critical code, though.

Dave

timmy01 05-14-2007 03:59 PM

what exactly makes it so nasty?

is it the " or ; ?

Cheerz!

ilikejam 05-14-2007 04:18 PM

The mix of ''', '"', ';' and '$'.

timmy01 05-14-2007 04:49 PM

Quote:

Originally Posted by ilikejam
You'll probably want to do something like
Code:

read ANSWER; if [ "x$ANSWER" == "xyes" ]; then  sed .......; fi


with this i get an error:
-sh: syntax error near unexpected token `done'

and to escape a string in SED do i use a . as well?

ilikejam 05-14-2007 04:52 PM

What's the full command you're running?

timmy01 05-14-2007 04:58 PM

Quote:

Originally Posted by ilikejam
What's the full command you're running?

for f in `find . -type f`;
do if grep "http://www.$domain.com/dirs/file_nl.php?loc=$Var&var=$var&file=file" "$f";
read ANSWER; if [ "x$ANSWER" == "xyes" ];
then sed -i
's/http:\/\/www.$domain.com\/dirs\/file_nl.php?loc=$Var&var=$var&file=file
/http:\/\/www.$domain.com\/nl\/dirs\/$var\//g'
"$f";
fi;
done

ilikejam 05-14-2007 05:24 PM

Your 'then' should be before the 'read', not the 'sed'

timmy01 05-14-2007 06:11 PM

Quote:

Originally Posted by ilikejam
Your 'then' should be before the 'read', not the 'sed'


aha, ok!

and is the forward slash correct,
in order to escape things in SED?

ilikejam 05-14-2007 06:25 PM

Your sed command looks OK in your last post - backslashes (\) to escape characters. To be completely accurate, you should escape all the '.'s in the first part of the sed expression, but it shouldn't be a problem here.

timmy01 05-15-2007 04:47 AM

for f in `find . -type f`;
do if grep "http://www.$domain.com/dirs/file_nl.php?loc=$Var&var=$var&file=file" "$f";
then
read ANSWER; if [ "x$ANSWER" == "xyes" ];
sed -i
's/http:\/\/www\.$domain\.com\/dirs\/file_nl\.php?loc=$Var&var=$var&file=file
/http:\/\/www\.$domain\.com\/nl\/dirs\/$var\//g'
"$f"; fi; done

with this i get another error:
-bash: syntax error near unexpected token `fi'


All times are GMT -5. The time now is 07:39 PM.