LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2010, 01:53 AM   #1
pellicle
Member
 
Registered: Jul 2008
Location: Finland
Distribution: RHEL4
Posts: 139

Rep: Reputation: 15
multi file recursive folder search and replace


Hi

I'm wanting to mod some PHP files across a hierarchy and thought I'd drive it with find + grep + xargs

I built up a command line which I was confident would do the job, but now can't save the results.

First I tried this:
Code:
find . -name \*.php | xargs grep serialize | cut -d: -f1| sort -u | xargs sed -i s/serialize/serialise/g
but that didn't work:

Code:
sed: illegal option -- i
so I thought I'd try using
Code:
xargs -I {}
so that I could repeat the file name

but I'm having trouble with this. For instance

Code:
find . -name \*.php | xargs grep serialize | cut -d: -f1| sort -u | xargs -I{} sed s/serialize/serialise/g {} > {}.old
just writes a single file with the name {}.old containing all the other files

while
Code:
find . -name \*.php | xargs grep serialize |cut -d: -f1|sort -u |  xargs -I{} echo {}  {}.alt
produces:

Code:
./includes/authenticate.php ./includes/authenticate.php.alt
./modules/team/modify.php ./modules/team/modify.php.alt
./modules/team/modifyInfo.php ./modules/team/modifyInfo.php.alt
so, I'm clearly missing something ... perhaps something obvious.

can anyone assist with this?
 
Old 08-03-2010, 02:21 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
try:

xargs -n1
 
Old 08-03-2010, 02:27 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Something like this:

find . -type f -name "*.php" -exec grep -l "serialize" {} \; | xargs sed 's/serialize/serialise/g' {}

If this is what you want/need add the -i.bak flag to sed (sed 's/serialize/serialise/g' -> sed -i.bak 's/serialize/serialise/g')

- First find all files that match *.php (-type f -name "*.php")
- List only files that contain serialize (exec grep -l "serialize" {} \;)
- Give the found files to sed (xargs sed 's/serialize/serialise/g' {})

Hope this helps.
 
Old 08-03-2010, 02:52 AM   #4
Mr. Bit
LQ Newbie
 
Registered: Jun 2006
Posts: 6

Rep: Reputation: 3
Try this:

Code:
find . -name \*.php | xargs grep -l serialize | xargs -I{} sed -i.orig 's/serialize/serialise/g' {}
 
Old 08-03-2010, 04:05 AM   #5
pellicle
Member
 
Registered: Jul 2008
Location: Finland
Distribution: RHEL4
Posts: 139

Original Poster
Rep: Reputation: 15
Guys

thanks for the responses. As to sed -i, for some reason that is not supported on my system (I already tried it, it was the first option posted).

I'll try the limit on xargs -n1, but I would be curious to know why my previous attempt at writing a new file with > didn't work while the echo did ...

Last edited by pellicle; 08-03-2010 at 04:07 AM. Reason: spelling
 
Old 08-03-2010, 04:35 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by pellicle View Post
I'll try the limit on xargs -n1, but I would be curious to know why my previous attempt at writing a new file with > didn't work while the echo did ...
Because the > sign at the end of the command line redirects all the standard output from the previous pipe to a file. Instead you want the redirection be part of the sed command itself. Here is a workaround:
Code:
find . -name \*.php | xargs grep serialize | cut -d: -f1| sort -u | xargs -I{} echo bash -c "sed 's/serialize/serialise/g' {} > {}.alt"
You can see that redirection is now embedded in double quotes and it is protected from the parent shell. Please note that in the line above I put an echo to check the resulting commands: remove the echo statement when you want to actually create the new files.
 
Old 08-03-2010, 07:38 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So I am curious why everyone is using grep when sed has a perfectly good search function of its own??
I must have missed something.
 
Old 08-03-2010, 07:43 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by grail View Post
So I am curious why everyone is using grep when sed has a perfectly good search function of its own??
I agree. Furthermore, since the searched pattern is that one to be substituted you don't even need to specify the address in sed:
Code:
find . -name \*.php | xargs -I{} echo bash -c "sed 's/serialize/serialise/g' {} > {}.alt"

Last edited by colucix; 08-03-2010 at 07:45 AM.
 
1 members found this post helpful.
Old 08-03-2010, 07:03 PM   #9
pellicle
Member
 
Registered: Jul 2008
Location: Finland
Distribution: RHEL4
Posts: 139

Original Poster
Rep: Reputation: 15
Hi

Quote:
Originally Posted by grail View Post
So I am curious why everyone is using grep when sed has a perfectly good search function of its own??
I must have missed something.
that is a good point ... my excuse is that as I developed the string (an iterative process) I started with grep to identify the files which needed changing. I didn't then apply the logic that I could remove this with sed

thanks for pointing that out :-)
 
Old 08-03-2010, 07:06 PM   #10
pellicle
Member
 
Registered: Jul 2008
Location: Finland
Distribution: RHEL4
Posts: 139

Original Poster
Rep: Reputation: 15
Hi

Quote:
Originally Posted by colucix View Post
I agree. Furthermore, since the searched pattern is that one to be substituted you don't even need to specify the address in sed:
the only thing I have against this method is that it left me with bucket loads of files .alt which did not need to be edited ... and makes it seem that heaps more needed to be checked back in to SVN which was in fact not needed to be edited.

:-)
 
Old 08-03-2010, 07:12 PM   #11
pellicle
Member
 
Registered: Jul 2008
Location: Finland
Distribution: RHEL4
Posts: 139

Original Poster
Rep: Reputation: 15
Hi

Quote:
Originally Posted by colucix View Post
Because the > sign at the end of the command line redirects all the standard output from the previous pipe to a file. Instead you want the redirection be part of the sed command itself.

thank you for the explanation and the example

 
  


Reply



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
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 10:52 AM
sed multi-line search/replace woes djmm Programming 8 03-17-2009 05:25 AM
search and replace with multi line string kambrish Programming 5 04-28-2008 06:02 AM
Perl Multi-Line Search & Replace... Can I?... donv2 Linux - Newbie 2 01-30-2007 09:59 PM
Multi-file search and replace? miknight Linux - General 4 10-21-2003 06:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:45 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration