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 22:45] – 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: | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | ===== Miscellaneous ===== | ||
- | |||
- | ===== Inspect The passwd File Using Cut Command ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | The passwd file stored inside /etc in most systems contain very sensitive information about the system and its users. | ||
- | |||
- | You can inspect this file quickly using the cut command. | ||
- | |||
- | Delimiter ‘:’ is used as the columns of this file are separated using it. | ||
- | |||
- | Change the value of -f to monitor different fields. | ||
- | |||
- | ---- | ||
- | |||
- | |||
- | ===== Cut Specific Fields and Show Only the Unique Entries ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Cut the third column of the file test.txt and only show the unique entries. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut All Bytes of Input Stream Except the Specified Ones ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Cut all the characters of the given input string except the ones supplied to -b. | ||
- | |||
- | So, byte positions first, third, fifth, and seventh will be omitted from the output. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut All Bytes of a File Except the Specified Ones ===== | ||
- | |||
- | <code bash> | ||
- | cut -b 2,4,6 test.txt --complement | ||
- | </ | ||
- | |||
- | Cut all the bytes of the file test.txt except the one mentioned in the command. | ||
- | |||
- | Thus, the output will not contain the second, fourth, and sixth bytes of each line. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut All Characters of Input Stream Except the Specified Ones ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | This command refrains from cutting the first, third, fifth, and seventh characters of the input string and instead cuts all other characters except these four. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut All Characters of a File Except the Specified Ones ===== | ||
- | |||
- | <code bash> | ||
- | cut -c 2,4,6 test.txt --complement | ||
- | </ | ||
- | |||
- | The output will contain all characters of the test.txt files except the ones mentioned. | ||
- | |||
- | So, characters second, fourth, and sixth will not be displayed. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut all Input Sections Except the Ones Specified ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Output the string “cut input section by section“. | ||
- | |||
- | So, it will display all the input sections without the ones mentioned after the field flag. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut All Columns of a File Except the Specified Ones ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Cut only the first and last columns of the file test.txt. | ||
- | |||
- | So, you can easily deselect some fields when processing large tabular documents using the complement flag. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut a Section of Input and Reverse them Characterwise ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Cut the first and third section of the input and reverse them characterwise. | ||
- | |||
- | Notice, how the output of one command is being fed as the input to other commands. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Specific Columns in a File and Reverse them Characterwise ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Cut the specified fields of the file test.txt and display the result in a characterwise reverse manner. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Modify the Output Delimiter of the Cut Command ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Cut allows us to modify the output delimiter when displaying the result. | ||
- | |||
- | Cuts all sections of the comma-separated list but replaces the commas with spaces when showing the result. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Example of Cut+Sed Command with Tab Delimiter ===== | ||
- | |||
- | <code bash> | ||
- | sed ' | ||
- | </ | ||
- | |||
- | Replace the colons in our file with tabs. | ||
- | |||
- | You can replace \t with some other characters like – or ; for changing to an output delimiter of your choice. | ||
bash/cut.1576190713.txt.gz · Last modified: 2020/07/15 09:30 (external edit)