Liam Delahunty: Home Tips Web Contact
Recommended laptop
under £500
.

Think I deserve a present? See my Amazon Wish List

Piping Commands - Find, Grep, Sed

I have a client that will update the images on some pages on their site fairly frequently, these can often have new filenames, so I use the following commands to check that all the images from the pages updated today have linking to the correct fresh images.

find . -iname '*php' -mtime -1|xargs grep gif | sed 's/\(.*src="\)\(.*\.gif\)\(.*\)/\2/' | xargs ls -lA find . -iname '*php' -mtime -1

Find all the PHP files that are less than a day old

Then we "pipe" the results into another command, using the | symbol.

xargs grep gif

xargs ( build and execute command lines from standard input) - take the output from the previous command and grep the files (search within the file) for the word "gif"

Without the xargs the grep would search the document filename as provided by the find function, with the xargs we are searching in the document itself.

I then take those results into sed, a stream editor, it allows you to manipulate the "stream", that is the data you are getting from another command, and output it (display it) in another way.

I didn't want the whole line that had a match to the gif search as it had HTML there, such as alt tags and widths etc, so the sed element uses a regular expression to isolate the image path.

Each \(\) is a portion of the string, sometimes known as an atom. We can refer to each portion separately in the output, so these can be useful to isolate and work on a specific section of a string.

.* means everything, so .*src= is everything upto and including the 'src="' phrase.

The next phrase .*\.gif grabs the filename from opening quote, to the gif. the fullstop is escaped so the regular expression doesn't think it means "any character"

then the .* just gets the rest of the line

So, the three elements in my search are:

  1. \(.*src="\) =
  2. \(.*\.gif\)
  3. \(.*\)

Finally using the \2 on the right side of the sed expression will now just output the second element.

sed 's/\(.*src="\)\(.*\.gif\)\(.*\)/\2/'

From the list of gifs we now have, I want to see the information about the file, so we pipe our data into a ls -lA command to display each file in a listing.

find . -iname '*php' -mtime -1|xargs grep gif | sed 's/\(.*src="\)\(.*\.gif\)\(.*\)/\2/' | xargs ls -lA

Share this!  



Pine Delete On Date | Piping Commands Find Grep Sed | Procmail Extraneous Locallockfile Ignored


 

  Tips | Home | Search | Contact

Link here: