Search

cut: To pick parts of lines from a file.

The command "cut" can be used to extract parts of a lines from a file.
The syntax of the command is



Let us take the following example file and see how to use the various options of cut.

temp:



The option "-b" allows us to pick specific byte or a number of bytes from each line.

example :



The range need not start from the beginning i.e. "1", it could be any range of bytes or even a single byte from the line.

The -c option allows us to pick a specific character or range of characters from every line.

example



We can also specify a range of characters



The default delimiter for the command "cut" is a tab space, i.e any sequence of characters separated by one tab space is treated as a field and different fields can be selected using the option -f.
The default delimiter can be changed using the option -d.

example:



In the above example, we have passed a single space as an argument to the option "-d" thus the command treat the single space as delimiter and every sequence of characters which are separated by a space are numbered starting from 1, in veery line . Thus when we pass the number 2 to the option "-f" we are requesting for the second field which is what gets displayed on the screen.

We can change the output to characters that don't, match the options we are passing by using the options --complement.

example



Thus the parts of the line which did not match the options passed to the command are printed on the screen.
The output delimiter is treated as same as the input delimiter unless specified using the option --output-demlimiter

example



The fields are separated by an ":" instead of a space.

No comments:

Post a Comment