|Liam Delahunty: Home||Tips||Web||Contact|
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 all the PHP files that are less than a day old
Then we "pipe" the results into another command, using the | symbol.
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:
Finally using the
From the list of gifs we now have, I want to see the information about the file, so we pipe our data into a