Find, Grep and then Sed


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Find, Grep and then Sed
# 1  
Old 10-17-2009
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  
Old 10-18-2009
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 02:40 AM..
# 3  
Old 10-18-2009
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  
Old 10-19-2009
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 09:05 AM..
# 5  
Old 10-19-2009
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  
Old 10-19-2009
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 09:09 AM..
# 7  
Old 10-19-2009
Quote:
Originally Posted by nxvir
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

Code:
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

Although the preferred method would be to use "while read"
Code:
while read x ; do
  sed -i 's/\(blue\|red\|yellow\)/black/g' "$x"
done < temp.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find=grep or maybe sed/awk for multiple lines of text?

Hi, I am running the following: PASS="username/password" sqlplus -s << EOF | grep -v "^$" $PASS set feedback off set heading off set termout off select name from v\$database ; exit EOF Which gives ERROR: ORA-28002: the password will expire within 5 days PSMP1 (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Help with Passing the Output of grep to sed command - to find and replace a string in a file.

I have a file example.txt as follows :SomeTextGoesHere $$TODAY_DT=20140818 $$TODAY_DT=20140818 $$TODAY_DT=20140818I need to automatically update the date (20140818) in the above file, by getting the new date as argument, using a shell script. (It would even be better if I could pass... (5 Replies)
Discussion started by: SriRamKrish
5 Replies

3. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

4. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

5. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

6. Homework & Coursework Questions

find grep sed commands homework

Use and complete the template provided. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to make as home work several commands with gerp find and sed 2. Relevant commands, code, scripts, algorithms: FIND command -use command find... (8 Replies)
Discussion started by: ViruS89
8 Replies

7. Shell Programming and Scripting

Find and Rename files using (find mv and sed)

In response to a closed thread for degraff63 at https://www.unix.com/shell-programming-scripting/108882-using-mv-find-exec.html the following command might do it as some shells spit it without the "exec bash -c " part: Find . -name "*.model" -exec bash -c "mv {} \`echo {} | sed -e 's//_/g'\`"... (0 Replies)
Discussion started by: rupert160
0 Replies

8. UNIX for Dummies Questions & Answers

using sed or grep to find exact match of text

Hi, Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only) 1 Hello world fantasy. 2 Hello worldfuntastic. 3 Hello world wonderful. I would like to get all those lines of text that... (5 Replies)
Discussion started by: risk_sly
5 Replies

9. Shell Programming and Scripting

Complex find grep or sed command

Haven't worked in bash for ages. did a good bit of shell scripting in regular sh, but have forgotten most of it. I have several thousand php files that now include the following line at the end of the file. There is no LF or CR/LF before it begins, it is just concatenated to the final line of... (3 Replies)
Discussion started by: sjburden
3 Replies

10. Shell Programming and Scripting

grep and sed to find a pattern and add newline

Hello All, I have log file the result from a multithreaded process. So when a process finishes it will write to this log file as 123 rows merged. The issue is sometimes the processess finish at the same time or write to the file at the same time as 123 rows merged.145 rows merged. At... (5 Replies)
Discussion started by: ssikhar
5 Replies
Login or Register to Ask a Question