hankknight
asked on
Place files that contain key phrase in sh script
Hello,
This creates a custom script called runme.sh that processes files.
find /home/mydir/ |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" > runme.sh
Now ALL files are placed in "runme.sh" however I only want files to be placed in "runme.sh" that contain the word "Hello World"
This creates a custom script called runme.sh that processes files.
find /home/mydir/ |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" > runme.sh
Now ALL files are placed in "runme.sh" however I only want files to be placed in "runme.sh" that contain the word "Hello World"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
grep has the -l option to only print out the file name and not matching lines. please check your grep's man page to see if you have such options...
ASKER
Thanks, this works:
find /home/mydir -exec grep -il "Hello World" {} \; |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" > runme.sh
find /home/mydir -exec grep -il "Hello World" {} \; |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" > runme.sh
Here's another way of doing it:
grep -rli 'hello world' /home/mydir/*|xargs -i echo "./rmcom {} 'Hello World' >tempfile && mv tempfile {}" >runme.sh
grep -rli 'hello world' /home/mydir/*|xargs -i echo "./rmcom {} 'Hello World' >tempfile && mv tempfile {}" >runme.sh
ASKER
I tested it and it does NOT work. The problem is that your idea inserts unneeded junk. Instead of just adding the filename it adds context-- lines from the file where "Hello World" is contained.