[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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"



0
hankknight
Asked:
hankknight
  • 2
  • 2
1 Solution
 
ghostdog74Commented:
you can try to get those files that have Hello World in your "find" portion
eg (not tested)

  find /home/mydir/  -exec grep -i "hello world" {} \; | sed ........

something like that...
0
 
hankknightAuthor Commented:
Thanks, ghostdog74.

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.
0
 
ghostdog74Commented:
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...
0
 
hankknightAuthor Commented:
Thanks, this works:

find /home/mydir -exec grep -il "Hello World" {} \; |sed "s/.*/\.\/rmcom '&' 'Hello World' > tempfile \&\& mv tempfile &/" > runme.sh
0
 
TintinCommented:
Here's another way of doing it:

grep -rli 'hello world' /home/mydir/*|xargs -i echo "./rmcom {} 'Hello World' >tempfile && mv tempfile {}" >runme.sh
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now