UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 27.16 Faking Case-Insensitive Searches Chapter 27
Searching Through Files
Next: 27.18 Fast Searches and Spelling Checks with  "look"
 

27.17 Finding a Character in a Column

Here's an idea for finding lines that have a given character in a column. Use the following simple awk (33.11) command:

% awk 'substr($0,n,1) == "c"' filename

where c is the character you're searching for, and n is the column you care about.

Where would you do this? If you're processing a file with very strict formatting, this might be useful; for example, you might have a telephone list with a # in column 2 for "audio" telephone numbers, $ for dial-up modems, and % for fax machines. A script for looking up phone numbers might use an awk command like this to prevent you from mistakenly talking to a fax machine.

If your data has any TAB characters, the columns might not be where you expect. In that case, use expand (41.4) on the file, then pipe it to awk.

- JP, ML


Previous: 27.16 Faking Case-Insensitive Searches UNIX Power ToolsNext: 27.18 Fast Searches and Spelling Checks with  "look"
27.16 Faking Case-Insensitive Searches Book Index27.18 Fast Searches and Spelling Checks with "look"

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System