User Tools

Site Tools


bash:cut

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.


Cut a single character

cut -c 3
abcdef

returns:

c

Cut a range of characters

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 a variety of characters

cut -c 2,4,7
alongtext 

returns:

lne

Cut characters up to a specific position

cut -c -2
abcdef

returns:

ab

Cut characters from a specific position

cut -c 2-
abcdef

returns:

bcdef

Cut various characters

cut -c 1,6-9,16-
alongtextwithnospaces

returns:

atextspaces

Cut fields from a string, using a delimiter

cut -f 2- -d ':'
23:34:45:56

NOTE: -d specifies delimiter

returns:

34:45:56

Cut fields from a string, without specifying a delimiter

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 fields from a file, using a delimiter

cut -d: -f1 /etc/passwd >users.txt
bash/cut.1576185050.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki