...
$ grep Washing areacodes.txt
202	DC	Washington
227	MD	Silver Spring, Washington suburbs, Frederick
240	MD	Silver Spring, Washington suburbs, Frederick
⋮

Count the area codes with wc:

$ wc -l areacodes.txt
375 areacodes.txt

Find the state with the most area codes (the winner is California with 38):

$ cut -f2 areacodes.txt | sort | uniq -c | sort -nr | head -n1
     38 CA

Convert the file to CSV format to import into a spreadsheet application. Print the third field enclosed in double quotes to prevent its commas from being interpreted as CSV separator characters:

$ awk -F'\t' '{printf "%s,%s,\"%s\"\n", $1, $2, $3}' areacodes.txt \
  > areacodes.csv
$ head -n3 areacodes.csv
201,NJ,"Hackensack, Jersey City"
202,DC,"Washington"
203,CT,"New Haven, Stamford"

Collate all area codes for a given state onto a single line:

$ awk '$2~/^NJ$/{ac=ac FS $1} END {print "NJ:" ac}' areacodes.txt
NJ: 201 551 609 732 848 856 862 908 973

or collate for each state, using arrays and for loops as in “Improving the duplicate file detector”:

$ awk '{arr[$2]=arr[$2] " " $1} \
         END {for (i in arr) print i ":" arr[i]}' areacodes.txt \
  | sort
AB: 403 780
AK: 907
AL: 205 251 256 334 659
⋮
WY: 307

Turn any of the preceding commands into aliases, functions, or scripts, whatever is convenient. A simple example is the areacode script in Example 9-4.

Example 9-4. The areacode script
#!/bin/bash
if [ -n "$1" ]; then
  grep -iw "$1" areacodes.txt
fi

The areacode script searches for any whole word in the areacodes.txt file, such ...

Get Efficient Linux at the Command Line now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.