bash:cut
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
bash:cut [2019/12/12 21:59] – peter | bash:cut [2021/01/26 15:57] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== 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). | ||
- | |||
- | <WRAP info> | ||
- | Default delimiter is tab. | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | [[BASH: | ||
- | |||
- | [[BASH: | ||
- | |||
- | [[BASH: | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | ===== Character Cuts ===== | ||
- | |||
- | ==== Cut a single character ==== | ||
- | |||
- | <code bash> | ||
- | cut -c 3 | ||
- | abcdef | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | 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) | ||
- | |||
- | <code bash> | ||
- | cut -c 2-4 | ||
- | abcdef | ||
- | </ | ||
- | |||
- | Press CTRL+D to stop inputting | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | bcd | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Cut a variety of characters ==== | ||
- | |||
- | <code bash> | ||
- | cut -c 2,4,7 | ||
- | alongtext | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | lne | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Cut characters up to a specific position ==== | ||
- | |||
- | <code bash> | ||
- | cut -c -2 | ||
- | abcdef | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | ab | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Cut characters from a specific position ==== | ||
- | |||
- | <code bash> | ||
- | cut -c 2- | ||
- | abcdef | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | bcdef | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Cut various characters ==== | ||
- | |||
- | <code bash> | ||
- | cut -c 1,6-9,16- | ||
- | alongtextwithnospaces | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | atextspaces | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Field Cuts ===== | ||
- | |||
- | ==== Cut fields from a string, using a delimiter ==== | ||
- | |||
- | <code bash> | ||
- | cut -f 2- -d ':' | ||
- | 23:34:45:56 | ||
- | </ | ||
- | |||
- | NOTE: -d specifies delimiter | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | 34:45:56 | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ==== Cut fields from a string, without specifying a delimiter ==== | ||
- | |||
- | <code bash> | ||
- | cut -f 2 | ||
- | er rt fg wd ji | ||
- | </ | ||
- | |||
- | returns: | ||
- | |||
- | <code bash> | ||
- | er rt fg wd ji | ||
- | </ | ||
- | |||
- | NOTE: cut didn't find the delimiter (default is tab) so returns whole line. | ||
- | |||
- | ---- | ||
- | |||
- | ==== Cut fields from a string, without specifying a delimiter; and prevent printing when delimiter not found ==== | ||
- | |||
- | <code bash> | ||
- | 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 ==== | ||
- | |||
- | <code bash> | ||
- | cut -d: -f1 /etc/passwd > | ||
- | </ | ||
bash/cut.1576187965.txt.gz · Last modified: 2020/07/15 09:30 (external edit)