- Unix Commands Reference
- Unix - Tutorial Home
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
cut command in Linux with Examples
cut - remove sections from each line of files.
cut OPTION... [FILE]...
-b, --bytes=LIST select for printing only the bytes in positions listed in Byte-list. -c, --characters=LIST select by specifying a character, a set of characters, or a range of characters. -d, --delimiter=DELIM specify a delimiter that will be used instead of the default “TAB” delimiter. An input delimiter may be specified only when operating on fields, i.e., -f option. -f, --fields=LIST select for printing only the fileds in positions listed in Field-list; also print any line that contains no delimiter character, unless the -s option is specified --complement complement the set of selected bytes, characters or fields -n Do not split multi-byte characters (no-op for now). Suppresses splitting of multibyte characters (like utf-8 characters). Use this option only with the -b flag. If the last byte of a character falls within the range denoted by the List variable of the -b flag, the character is written; otherwise, the character is excluded. -s, --only-delimited do not print lines not containing delimiters --output-delimiter=STRING use STRING as the output delimiter the default is to use the input delimiter -z, --zero-terminated line delimiter is NULL, not newline --help display this help and exit --version output version information and exit
N the Nth field, byte or character, starting from 1. N- from the Nth field, byte or character, to the end of the line. N-M from the Nth to the Mth field, byte, or character. -M from the first to the Mth field, byte, or character.
cut is an extremely powerful text manipulator, it manipulates text column wise (vertically). This is often used in combination with other Linux commands or filters. It is a command-line utility that allow you to cut parts of lines from specified files or piped data and print the result to standard output.
cut command can be used to cut parts of a line by delimiter, byte position, and character.
With no FILE specified, or when FILE is -, command reads from standard input.
1. When no option is specified with
cut command, it displays an error.
$ cut textfile1.txt cut: you must specify a list of bytes, characters, or fields Try 'cut --help' for more information.
Let's have a sample file test.txt
$ cat test.txt 2233|a.k. shukla|g.m.|12/12/52|6000 1122|jai sharma|production|03/12/50|7000 3344|sumit singh|marketing|04/19/43|6000
-d option to specify a delimiter that will be used instead of the default “TAB” delimiter. To display the 2nd and the 3rd field of test.txt file, here delimiter is '|' symbol.
$ cut -d "|" -f 2,3 test.txt a.k. shukla|g.m. jai sharma|production sumit singh|marketing
3. Use option
-b to display the 1st, 2nd and the 3rd bytes of test.txt file.
$ cut -b 1,2,3 test.txt 223 112 334
4. Alternatively, we can also use range format with
-b option, instead of sepecifying each bytes.
$ cut -b 1-3 test.txt 223 112 334
--complemet option to complement the selected characters, bytes or fields. The fields that have been selected by -b or -f or by -c flags are left out, i.e., the selected fields are not displayed in the output.
$ cut -b 1-3 --complement test.txt 3|a.k. shukla|g.m.|12/12/52|6000 2|jai sharma|production|03/12/50|7000 4|sumit singh|marketing|04/19/43|6000
-c option to dispay the set of characters from each line.
$ cut -c -5,11-17,20- test.txt 2233|shukla|m.|12/12/52|6000 1122|harma|pduction|03/12/50|7000 3344| singh|rketing|04/19/43|6000
cut command prints first 5 characters, 11-17 characters and 20 to the last characters from each line of the input file.
--output-delimiter option to set the output delimiter.
$ cut -d " " -f 1-2 --output-delimiter="|" textfile1.txt Dr.|Manish Mr.|Manish Mr.|Hitesh Mr.|Hitesh Dr.|Hitesh ...