Tim wrote:
On Mon, 2005-11-21 at 13:33 +0000, Paul Howarth wrote:
Something like this?
$ ls --full-time | awk 'NR == 1 { next } { printf "<a href=\"/images/%s\" title=%s><img src=\"/thumbnails/%s\"></a>\n", $9, $6, $9 }' >> index.html
Pretty much (just changed the title=%s bit to be title=\"%s\", to be on
the safe side), thanks.
But just briefly...
I presume "NR == 1 { next }" means to iterate through the results until
the end of them. But it's not too clear (from quickly going through the
awk man file, look up "NR" and "next").
It actually means to skip the first line, which you'll see in the output
of "ls --full-time" isn't a line you're interested in.
Looking at the output from "ls --full-time" and the $9, $6 & $9, bits, I
presume that they mean to use the ninth, sixth, and ninth again, strings
returned from the ls command as the strings to be inserted where the
three %s variables are placed in the printf statements.
That's right.
Linux is full of tools for doing things like this. awk just happens
to be my preference but lots of people would do it in perl.
Hmm, I was trying to figure out where to start. There's a number of
programming languages around, and it did seem a bit like jumping in the
deep end to learn perl, or something similar to do something as simple
as this. Looks like I have the fun task of reading up on how awk
works.
awk is a great little text-processing language. An awk program basically
consists of a list of patterns and actions. Each line of input is
iterated through one after another, and for every line that matches a
pattern, the associated action is done. The default pattern matches
every line, and the default action is to print the line.
So, looking at the example above, we have two elements to the program:
NR == 1 { next }
NR is a built-in awk variable that returns the record (line) number of
the input. This is "1" for the first line, so what happens is that for
the first line and only the first line, the associated action is
executed. The action in this case is "next", which means go straight to
the next line of input without trying to match any further patterns in
the program. So effectively the first line of input is skipped.
For all other lines of input after the first, this action is not
executed and we pass on to the next program element:
{ printf "<a href=\"/images/%s\" title=%s><img
src=\"/thumbnails/%s\"></a>\n", $9, $6, $9 }
Here there is no pattern specified, so the default one of matching every
line of input is applied. The action is then applied to the rest of the
input data, producing some boilerplate text with the 6th and 9th fields
of the input data inserted appropriately.
Paul.