| Finding patterns | |
| Begin with census data: p=/homes/ddailey/public_html/data/places.txt | |
| finding patterns with grep |
$ grep "Slippery Rock" $p 42 71184 Slippery Rock |
| finding patterns with sed |
$ sed -n /Slippery\ Rock/p $p 42 71184 Slippery Rock |
| and ... with awk |
$ awk '/Slippery Rock/ { print $0 }' $p
42 71184 Slippery Rock |
| moving fields around | |
| with sed |
$ echo one two three|sed 's/\(\w*\)\ \(\w*\)\ \(\w*\)/\2\ \3\ \1/' two three one |
| with awk |
$ echo one two three|awk '{ print $2,$3,$1 }'
two three one |
| separating fields | |
$ cat awk/addy name:address:town George Harrison:123 Primrose Lane:Paddington Station John Lennon:345 Duckworth Place:Barleyforth Paul McCartney:987 Chatsworth Hollow:Durnstorry Blither Richard Starkey:456 Ambrosia Folding Perth:Parvleston Rebound | |
| with cut | $ cut -d: -f2 awk/addy address 123 Primrose Lane 345 Duckworth Place 987 Chatsworth Hollow 456 Ambrosia Folding Perth |
| with sed | sed 's/[^:]*://;s/:.*//' awk/addy address 123 Primrose Lane 345 Duckworth Place 987 Chatsworth Hollow 456 Ambrosia Folding Perth |
| with awk | $ awk '{FS=":"} {print $2}' awk/addy 123 Primrose Lane 345 Duckworth Place 987 Chatsworth Hollow 456 Ambrosia Folding Perth |
| with awk (refmed to include first line) | $ awk 'BEGIN {FS=":"} {print $2}' awk/addyaddress 123 Primrose Lane 345 Duckworth Place 987 Chatsworth Hollow 456 Ambrosia Folding Perth |