cut -f list [-d delim] [-s] [file …]
cut -c list [file …]
cut -b list [-n] [file …]
Writes portions of each line from each file or/and standard input to standard output.
list can be in terms of column position or fields delimited by a delim character.
Numbering starts from 1.
list | n1 n2,n3,n4 n5 | a set of increasing numbers and/or ranges separated by comma or whitespace.
|
range | n1-n2 | inclusive. |
-n | from 1 to n
| |
n- | from n to the end of the line.
| |
numbers and ranges may be repeated, overlap, and occur in any order.
| ||
| It is not an error to specify fields or columns not present in the input line. | ||
-c list |
Return code
1 cut: bad delimiter
Specifing both -c & -f is not permitted
On Mac OS returns 0 and displays usage!
usage: cut -b list [-n] [file ...] cut -c list [file ...] cut -f list [-s] [-d delim] [file ...]On linux returns 1 and displays:
cut: only one type of list may be specified Try `cut --help' for more information.
ENVIRONMENT
The LANG, LC_ALL and LC_CTYPE environment variables affect cut .
see environ, locale.
EXAMPLES
Extract users' login names and shells from the system passwd file as name:shell pairs (i.e. 1st and 7th columns).:/etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin 1 2 3 4 5 6 7
cut -d : -f 1,7 /etc/passwd #select fields 1 & 7 i.e. name and shell
root:/bin/bash
bin:/sbin/nologin
daemon:/sbin/nologin
Show the names and login times of the currently logged in users:
> who
dgerman console Dec 28 11:26
samual ttys000 Dec 30 14:44
dger ttys001 Dec 30 14:44
123456789012345678901234567890
> who | cut -c -9,26-
dgerman 11:26
samual 14:44
dger 14:44
Operating on fixed width fields, with right justified, space padded values is problematic with -f
> history |head
1 2017-04-19 13:07:54 cd docs
2 2017-04-10 10:55:00 cd computerstuff/
3 2017-04-10 10:55:01 du
4 2017-04-10 10:55:03 lt
5 2017-04-10 10:55:08 du
6 2017-04-10 10:55:10 cd raspberrypi/
7 2017-04-10 10:55:11 du
8 2017-04-10 10:55:13 lt
9 2017-04-10 10:55:24 rm raspbian-jessie-lite.161125.img
10 2017-04-10 10:55:34 cd
> history|cut -f5-6 -d' ' |head
1
2
3
4
5
6
7
8
9
2017-04-10
> history|sed "s/ /,/g" |head #show varying number of leading spaces
,,,,1,,2017-04-19,13:07:54,cd,docs
,,,,2,,2017-04-10,10:55:00,cd,computerstuff/
,,,,3,,2017-04-10,10:55:01,du
,,,,4,,2017-04-10,10:55:03,lt
,,,,5,,2017-04-10,10:55:08,du
,,,,6,,2017-04-10,10:55:10,cd,raspberrypi/
,,,,7,,2017-04-10,10:55:11,du
,,,,8,,2017-04-10,10:55:13,lt
,,,,9,,2017-04-10,10:55:24,rm,raspbian-jessie-lite.161125.img,
,,,10,,2017-04-10,10:55:34,cd
|
The Mac OSX darwin BSD version as of 10.5.6 exits if a copyright symbol (x'A9', © ), left double quote (x'93', “) etc, B8, D1, C0, CF, C2, D8, D4 is encountered with the message: Illegal byte sequence and a exit status of 0.
This is easily corrected with:
|