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 |
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 Dave |
thanks.
do you know if its possible to add a prompt? so that i can so yes or no before it will be changed? |
You'll probably want to do something like
Code:
read ANSWER; if [ "x$ANSWER" == "xyes" ]; then sed .......; fi |
Quote:
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!! |
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." Dave |
what exactly makes it so nasty?
is it the " or ; ? Cheerz! |
The mix of ''', '"', ';' and '$'.
|
Quote:
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? |
What's the full command you're running?
|
Quote:
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 |
Your 'then' should be before the 'read', not the 'sed'
|
Quote:
aha, ok! and is the forward slash correct, in order to escape things in SED? |
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.
|
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. |