Cutting based on a delimiter
| command/output | comments |
| $ cat temp 12:19865:medical:(adj) 2:19866:medicament:(n) 1:19867:medicaster:(n) 2:19868:medicate:(v) 0:19869:medication:(n) 1:19870:medicinal:(adj) 7:19871:medicine:(n) 1:19872:mediety:(n) |
A file in which fields are delimitted by the character ':' |
| $ cut -d: -f3 temp medical medicament medicaster medicate medication medicinal medicine mediety |
Outputting the 3rd field from that file using cut. |
$ cat drivel 123456 7 8 9 10 11 |
Some semi-structured data in a file.
(a subset of ~ddailey/public_html/full.txt)
crude numbering of "fields" following a blank. |
$ cut -d' ' -f11 drivel |
cutting the file into fields based on a blank character as the delimiter. outputting the 11th field. |
| $ cat eos echo ego embargo embryo ergo espresso expo |
A file |
| $ cut -c1,2,3 eos ech ego emb emb erg esp exp |
Outputting the first, second and third columns of each line of the file. |
Cutting columns (practical examples)
| Census data (on places) | |
| command/output | comments |
| $c=/home/SRUNET/david.dailey/data/places.txt $wc $c 23791 269051 3401831 /home/SRUNET/david.dailey/data/places.txt | A fairly big file, from the US Census bureau, containing information about names of places in the US. |
| $head -5 $c 01 00124 Abbeville city AL 3173 1320 0000040308 0000000121 +31567949 -085253681 01 00460 Adamsville city AL 4161 1554 0000007948 0000000000 +33595542 -086957993 01 00484 Addison town AL 626 286 0000007688 0000000000 +34199940 -087177921 01 00676 Akron town AL 468 220 0000001437 0000000000 +32876118 -087738779 01 00820 Alabaster city AL 14732 5144 0000048812 0000000106 +33226547 -086824620 $tail -5 $c 78 39700 Frederiksted Southeast CDP VI 3002 992 0000003263 0000001019 +17707358 -064880139 78 46000 Grove Place CDP VI 2655 811 0000004322 0000000000 +17726297 -064827477 78 46000 Grove Place CDP VI 2655 811 0000004322 0000000000 +17726297 -064827477 | The first 5 and last 5 lines of the file. |
| $head -5 $c|cut -c72-76 $head -5 $c|cut -c72-77 A A A A A $head -5 $c|cut -c72-78 AL AL AL AL AL $head -5 $c|cut -c77-78 AL AL AL AL AL | Finding
the actual place names or state names using cut. (class work) This
demonstrates a bit of trial and error to hone in on the particular
columns that are relevant to finding the two letter state code. Note: 'head' rather than 'cat' is used so that we can experiment on a subset of the full file (which is large). |
| $cut -c77-78 $c|sort|uniq -c|head -5 2 317 AK 473 AL 501 AR 73 AS ~$cut -c77-78 $c|sort|uniq -c|tail -5 69 VT 426 WA 618 WI 277 WV 109 WY | Results from above can then be sorted and counted to give a breakdown of number of municipalities per state. |
| $cat <(head -5 $c) <(tail -5 $c) 01 00124 Abbeville city AL 3173 1320 0000040308 0000000121 +31567949 -085253681 01 00460 Adamsville city AL 4161 1554 0000007948 0000000000 +33595542 -086957993 01 00484 Addison town AL 626 286 0000007688 0000000000 +34199940 -087177921 01 00676 Akron town AL 468 220 0000001437 0000000000 +32876118 -087738779 01 00820 Alabaster city AL 14732 5144 0000048812 0000000106 +33226547 -086824620 78 39700 Frederiksted Southeast CDP VI 3002 992 0000003263 0000001019 +17707358 -064880139 78 46000 Grove Place CDP VI 2655 811 0000004322 0000000000 +17726297 -064827477 78 46000 Grove Place CDP VI 2655 811 0000004322 0000000000 +17726297 -064827477 | How to find the actual names of places (given that some places, e.g., 'Slippery Rock') have more than one word in their name? Here I've concatenated the beginning and end of the file, fcutor examination. |
| $cat <(head -5 $c) <(tail -5 $c)|cut -c10-76 Abbeville city Adamsville city Addison town Akron town Alabaster city Frederiksted Southeast CDP Grove Place CDP Grove Place CDP | Based on above analysis of columns, and a bit of hand-counting, we see that the place names begin at column 10 and extend up until column 76. |