home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 35.13 Other Conversions with dd Chapter 35
You Can't Quite Call This Editing
Next: 35.15 Cutting Columns with colrm
 

35.14 Cutting Columns or Fields with cut

cut+paste
System V includes a nifty command called cut that lets you select a list of columns or fields from one or more files. We've also included a public-domain version on the disc, for those of you whose systems do without.

You must specify either the -c option to cut by column or -f to cut by fields. (Fields are separated by tabs unless you specify a different field separator with -d . Use quotes (8.14 ) if you want a space or other special character as the delimiter.)

The column(s) or field(s) to cut must follow the option immediately, without any space. Use a comma between separate values and a hyphen to specify a range (e.g., 1-10 , 15 , 20 , or 50- ).

cut is incredibly handy. Here are some examples:

  • Find out who is logged in, but list only login names:

    who
     
    % who | cut -d" " -f1
    
    

  • Extract usernames and real names from /etc/passwd (36.3 ) :

    % cut -d: -f1,5 /etc/passwd
    
    

  • Cut characters in the fourth column of file , and paste them back as the first column in the same file:

    paste
     
    % cut -c4 file 
    
    | paste - file
    
    
    
    

- TOR , DG


Previous: 35.13 Other Conversions with dd UNIX Power Tools Next: 35.15 Cutting Columns with colrm
35.13 Other Conversions with dd Book Index 35.15 Cutting Columns with colrm

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