I’m working on the Web site for Creating Cool Web Sites and just realized that the little Unix trick I used while editing a file is actually a beautiful example of why so many people love Unix so much, so I thought I’d share it. The problem: a file where lines have 1-10 words + a last field value (in this case, a page number) that I don’t want. The challenge is to figure out how to easily remove that last field, when there are a variable number of fields on the line.
My first instinct was to turn to awk or perl, but in fact there’s a much easier way using basic Unix utilities: rev and cut:
$ rev inputfile | cut -f2- | rev > outputfile
How does this work? The rev command reverses each line of input, so that means that the first field of each now-reversed line is the field that we want to remove. That’s easily done with cut with the -f2- flag (that means output field two through the end of the line). Then, finally, we re-reverse (that is, fix) each line and save the output in a new file.
Quickly and easily done.