7 UNIX Cut Command Examples of How to Extract Columns or Fields from a File - Part I
Background: A filter, such as the UNIX cut command, is a program that processes an input stream of data to produce an output stream of data. The input data may be fed into the program's standard input or read from a file, and the output data may be sent to standard output or sent to a file.
The UNIX cut command is used to extract a vertical selection of columns (character position) or fields from one or more files. The syntax for extracting a selection based on a column number is:
$ cut -c n [filename(s)]
where n equals the number of the column to extract. Consider the contents of a file named class:
$ cat class
A Johnson Sara
D Smith Tom
B Jones Mindy
D Anderson Bob
The following UNIX cut example will extract the first column of the class file:
$ cut -c 1 class
The syntax for extracting a selection based on a field number is:
$ cut -f n [filename(s)]
where n represents the number of the field to extract. The default field delimiter is the tab character.
This cut example will extract the second field of the class file and redirect standard output to the file class.lastname:
$ cut -f 2 class > class.lastname
$ cat class.lastname
The -d option ("cut -d") can be used to change the field delimiter to something other than the default tab character as demonstrated in these UNIX cut examples.
The UNIX passwd file will be our input file for this example. If you are not familiar with this file, it stores system/user account credentials for a UNIX system and each field of each line of information is delimited by a colon (:). Here is a snippet (excerpt) from a passwd file:The colon immediately following "cut -d" instructs the cut command to look at the data in between (delimited by) each colon.
If you wanted to create a new file that contained just the username (field 1) and the user ID (UID), which is the third field in each line, running "cut -f 1,3 /etc/passwd" would not accomplish this since the cut command is using the default field delimiter (the tab character).
Instead, you would use the following cut -d command:
$ cut -d: -f 1,3 /etc/passwd
Do you need to learn UNIX or Linux, including how to read and write shell scripts...on a REAL SERVER? If you are ready to move past the basics, either of these online courses is a good place to start...
UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.
UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.
Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.