cut command examples in Unix/Linux - Bash

cut command examples in Unix/Linux - Bash

                             Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text.

In cut command -d stand for specifying delimiter -f will help to specify the column. You can specify delimiter as like follows,
-d" " --> space as delimiter
-d"." --> dot as delimiter
-d":" --> colon as delimiter
You can give any single character as delimiter, even alphabets and nummbers.

Please note the following variety of cut command samples. Here i am using dot as delimiter to give you some samples.

cut and print 2 to rest of all columns.
# echo | cut -d"." -f2-

cut and print 2nd column.
# echo | cut -d"." -f2

cut and print first to 2nd column.
# echo | cut -d"." -f-2

cut and print 1st,3rd columns.
# echo | cut -d"." -f1,3

cut and print 1st and 4 to rest of all columns.
# echo a.b.c.d.e | cut -d"." -f4-,1

cut and print 1 to 3 and 5th columns.
# echo a.b.c.d.e | cut -d"." -f1-3,5

cut and print 3rd character.
# echo abcde | cut -c3

cut and print 3-rest of characters.
# echo abcde | cut -c3-

cut and print first-3 of all characters.
# echo abcde | cut -c-3

That's it...

This entry was posted by Arun Ghosh. Bookmark the permalink.

One thought on “cut command examples in Unix/Linux - Bash”