Cut

If you have a file that consists of lines of text that are split into fields by white space or a delimiter character, cut can get a particular field or fields from every line of the file. For example, in /etc/passwd the file is delimited by the colon character (:). The cut option, -f, tells it which field or fields you are selecting, and the -d option tells it what the delimiter character is. The file /etc/passwd is made up of lines like this:

user:x:1001:100:Guest User:/home/user:/bin/bash

To get just the real name (the fifth) and the shell (the seventh) fields from the file, you could do this:

This would produce many lines of output like this:

Guest User:/bin/bash

You can also select particular characters from each line, using the -c option as follows:

[email protected]:~ > cat file first second third fourth [email protected]:~ > cut -c2-4 file irs eco hir our

Was this article helpful?

0 0

Post a comment