Let us turn our thoughts from looping and look upon another type of tool, one which lets us extract certain parts of Lines from Files, while ignoring other parts which do not meet our fancy. This fine tool is called ‘cut’, named for the cutlery with which we may similarly slice our breakfast mutton or supper eel.

There are two principal ways to use ‘cut’ to carve out the tasty morsels from our Files. The first Option, and simpler, is ‘-c’, ‘dash see’. With this option you may specify the character-columns to cut from each line that passeth under the sharp blades of ‘cut’. Numbers given with this Option may be listed singly, with commas, or as a range. Note that column counting begins at One, not Zero. Id est:

ℜ> cat weasel.txt 111111111122222222223 123456789012345678901234567890 Half a pound of tupenny rice, Half a pound of treacle. That’s the way the money goes, Pop! goes the weasel. ℜ> cut -c1 weasel.txt 1 H H T P ℜ> cut -c1,3,5 weasel.txt 135 Hl Hl Ta’ Pp ℜ> cut -c8-18 weasel.txt 111111111 89012345678 pound of tu pound of tr the way the es the weas ℜ> _
Samson Wrestling with the Lion

Cutting out columns from thy File is an estimable ability, and fruitful. Yet not all Files have their Contente arrayed so straight and true, like the white trunks of the birch-forest. This is why ‘cut’ hath been endowed with another Option, ‘-f’, dash eff. Use this to tell ‘cut’ that not the column, but rather, the Field ye seek. These Fields are presumed divided by Tabbes, unless ‘cut’ be instructed to use a different de-limiter, and thus a third Option arises, ‘-d’, dash dee. Id est:

ℜ> cat byzantine.csv emperor,reign_end,dynasty Theodore I Laskaris,1205,Laskarid John III Doukas Vatatzes,1254,Laskarid Theodore II Laskaris,1258,Laskarid John IV Laskaris,1261,Laskarid ℜ> cut -d" " -f1 byzantine.csv emperor,reign_end,dynasty Theodore John Theodore John ℜ> cut -d"," -f2 byzantine.csv reign_end 1205 1254 1258 1261 ℜ> cut -d"," -f1,2 byzantine.csv emperor,reign_end Theodore I Laskaris,1205 John III Doukas Vatatzes,1254 Theodore II Laskaris,1258 John IV Laskaris,1261 ℜ> _
A Water Fowl

There is one more trick which you may needs avail yourself of, with either the ‘-c’ or the ‘-f’ Options. If thy wish is to circumscribe a particular character-column or a particular field, and from thence also to the end of each line, then simply abandon the final number of the range and it will be so.

ℜ> cut -c17- weasel.txt 11122222222223 78901234567890 tupenny rice, treacle. he money goes, asel. ℜ> cut -d"," -f2- byzantine.csv reign_end,dynasty 1205,Laskarid 1254,Laskarid 1258,Laskarid 1261,Laskarid ℜ> _

Ctrl-D, Amen.

Samson Wrestling with the Lion, about 1270, Franco-Flemish
A Water Fowl, about 1270, Franco-Flemish
Digital images courtesy of the Getty’s Open Content Program

~ this page pub. 09 February 2014 ~
« previouse page: Wyld Kingdom: bonnacons ~ Table of Contentes ~ next page: Sponsors »