The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using Find w/ Grep? beefeater267 Shell Programming and Scripting 2 01-24-2009 07:05 AM
Help using find/grep Rally_Point UNIX for Dummies Questions & Answers 4 12-18-2008 03:34 AM
find , grep james94538 UNIX for Dummies Questions & Answers 3 10-09-2008 09:03 PM
grep, find or awk? netrom UNIX for Dummies Questions & Answers 4 04-09-2008 05:03 PM
grep and find MEllis5 UNIX for Dummies Questions & Answers 1 04-07-2008 08:16 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-17-2009
nxvir nxvir is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
Lightbulb Find, Grep and then Sed

Example:
I have folders

456
abc
xyz
123
a1b

I dont want to find in 123 and a1b. From rest folder i need to find in html and php files.

find ./ -path "123" -prune and a1b

find ./ -iname "*.htm*" -o -iname "*.shtm*" -o -iname "*.php"

Now while finding i need to grep multiple words and then replace it one word.

example: grep (blue\|red\|yellow\|orange)

then put the result to text file.

After that i will pass a sed command and replace all the colorblack


My thought is something like this:

find ./ -path "123" -prune -o -path "a1b" -o -iname "*.htm*" -o -iname "*.shtm*" -o -iname "*.php" | xargs grep (blue\|red\|yellow) > temp.txt

for x in cat temp.txt do; sed -i 's/blue/black/g' $x; done


i have arround 10k files to replace, Can any one make it work?
  #2 (permalink)  
Old 10-18-2009
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 601
Hi nxvir,

you can prune them like so, but then find will skip the content of those directories yet still list the directories themselves which will then still be passed as a parameter to xargs.

Code:
find ./ -path "*123" -prune -o -path "*a1b" -prune -o -iname "*.shtm" -o -iname "*.htm" -o -iname "*.php"
So I think it is better to not include the directories in find in the first place:
Code:
find ./!(a1b|123) -iname "*.shtm" -o -iname "*.htm" -o -iname "*.php"
For the grep statement:
Code:
egrep -l "blue|red|yellow"
should work.

Regarding the for statement:
Code:
for x in $(cat temp.txt) do

Last edited by Scrutinizer; 10-18-2009 at 01:40 AM..
  #3 (permalink)  
Old 10-18-2009
steadyonabix steadyonabix is offline
Registered User
  
 

Join Date: Oct 2009
Location: UK
Posts: 155
I think this might work without creating the tmp file: -

Code:
 
for x in $(find ./!(a1b|123) -iname "*.shtm" -o -iname "*.htm" -o -iname "*.php" | xargs egrep -l "blue|red|yellow" )
do
 
etc
  #4 (permalink)  
Old 10-19-2009
nxvir nxvir is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
i need to put the find result to text file is this right

Code:
 xargs egrep -l "blue|red|yellow" > temp.txt

then after that

for x in `cat temp.txt` do; 
sed -i 's/blue/black/g' $x; 
sed -i 's/yellow/black/g' $x; 
sed -i 's/red/black/g' $x; 
done

Last edited by nxvir; 10-19-2009 at 08:05 AM..
  #5 (permalink)  
Old 10-19-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,262
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
  #6 (permalink)  
Old 10-19-2009
nxvir nxvir is offline
Registered User
  
 

Join Date: Oct 2009
Posts: 3
while run both given find command i get error
Code:
 find ./!(a1b|123) -iname "*.shtm" -o -iname "*.htm" -o -iname "*.php"
for x in $(find ./!(a1b|123) -iname "*.shtm" -o -iname "*.htm" -o -iname "*.php" 
| xargs egrep -l "blue|red|yellow" )
bash: !: event not found

wat may have caused this?

This command worked but as told by "Scrutinizer" it is true, still lists the directories which we ignored.

Code:
 find ./ -path "*123" -prune -o -path "*a1b" -prune -o -iname "*.shtm" -
o -iname "*.htm" -o -iname "*.php"
i tried by including -print

Code:
 find ./ -path "*123" -prune -o -path "*a1b" -prune -o -print 
-o -iname "*.shtm" -o -iname "*.htm" -o -iname "*.php"
this worked any taught?

Last edited by nxvir; 10-19-2009 at 08:09 AM..
  #7 (permalink)  
Old 10-19-2009
binlib binlib is offline
Registered User
  
 

Join Date: Aug 2009
Location: New Jersey
Posts: 56
Quote:
bash: !: event not found

wat may have caused this?

I hate the history expansion, so I put this in my profile:
Code:
set +H
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0