LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-14-2007, 08:05 AM   #1
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Rep: Reputation: 15
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
 
Old 05-14-2007, 09:11 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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
 
Old 05-14-2007, 09:51 AM   #3
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
thanks.

do you know if its possible
to add a prompt? so that i can so yes or no
before it will be changed?
 
Old 05-14-2007, 02:44 PM   #4
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
You'll probably want to do something like
Code:
read ANSWER; if [ "x$ANSWER" == "xyes" ]; then  sed .......; fi
 
Old 05-14-2007, 03:03 PM   #5
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
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!!
 
Old 05-14-2007, 03:26 PM   #6
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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

Last edited by ilikejam; 05-14-2007 at 03:27 PM.
 
Old 05-14-2007, 03:59 PM   #7
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
what exactly makes it so nasty?

is it the " or ; ?

Cheerz!
 
Old 05-14-2007, 04:18 PM   #8
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
The mix of ''', '"', ';' and '$'.
 
Old 05-14-2007, 04:49 PM   #9
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
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?
 
Old 05-14-2007, 04:52 PM   #10
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
What's the full command you're running?
 
Old 05-14-2007, 04:58 PM   #11
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
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
 
Old 05-14-2007, 05:24 PM   #12
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Your 'then' should be before the 'read', not the 'sed'
 
Old 05-14-2007, 06:11 PM   #13
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
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?
 
Old 05-14-2007, 06:25 PM   #14
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
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.
 
Old 05-15-2007, 04:47 AM   #15
timmy01
LQ Newbie
 
Registered: Sep 2003
Posts: 22

Original Poster
Rep: Reputation: 15
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'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
Changing extension of multiple files harisund Programming 17 06-01-2006 10:59 AM
changing case in sed phoenix7 Linux - Software 8 08-30-2005 07:18 AM
changing ownership and group of multiple files.. utanja Debian 3 02-21-2005 06:10 PM
changing extension of multiple files shaggystyle Linux - General 5 01-29-2004 12:35 PM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration