This is an old revision of the document!
BASH - cut
The cut command allows cutting of sections based on byte positions, characters, or fields separated by a delimiter like the ‘-‘ or ‘:’ characters.
cut - remove sections from each line of files.
cut can return section based on number of bytes (-b), characters (-c), or fields (-f) when fields are separated by a delimiter(-d).
Default delimiter is tab.
A range must be provided in each case which consists of one of N, N-M, N-(N to last) or -M (first to M)
cut -c 2-4 abcdef
Press CTRL+D to stop inputting
returns:
bcd
cut -c 3 abcdef
returns:
c
cut -c 2,4,7 alongtext
returns:
lne
cut -c -2 abcdef
returns:
ab
cut -c 2- abcdef
returns:
bcdef
cut -c 1,6-9,16- alongtextwithnospaces
returns:
atextspaces </code ---- <code bash> cut -f 2- -d ':' 23:34:45:56
NOTE: -d specifies delimiter
returns:
34:45:56
cut -f 2 er rt fg wd ji
returns:
er rt fg wd ji
NOTE: cut didn't find the delimiter (default is tab) so returns whole line.
cut -f 2 -s er rt fg wd ji
NOTE: cut wont print as -s flag is used to prevent printing when delimiter not found.
cut -d: -f1 /etc/passwd >users.txt