User Tools

Site Tools


bash:cut

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
bash:cut [2020/07/15 09:30] – external edit 127.0.0.1bash: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. 
-</WRAP> 
- 
----- 
- 
-[[BASH:cut:Cut using Bytes|Cut using Bytes]] 
- 
-[[BASH:cut:Cut using Characters|Cut using Characters]] 
- 
-[[BASH:cut:Cut using Fields|Cut using Fields]] 
- 
- 
----- 
- 
-===== Miscellaneous ===== 
- 
-===== Inspect The passwd File Using Cut Command ===== 
- 
-<code bash> 
-cut -d ':' -f1 /etc/passwd 
-</code> 
- 
-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 ':' -f 3 test.txt | uniq -u 
-</code> 
- 
-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 "Let's cut this input stream section by section" | cut -b 1,3,5,7 --complement 
-</code> 
- 
-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 
-</code> 
- 
-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 "Let's cut this input stream section by section" | cut -c 1,3,5,7 --complement 
-</code> 
- 
-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 
-</code> 
- 
-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 "Let's cut this input stream section by section" | cut -d ' ' -f 1,3,5 --complement 
-</code> 
- 
-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 ':' -f 2,3 test.txt --complement 
-</code> 
- 
-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 "Let's cut this input stream section by section" | rev | cut -d ' ' -f 1,3 
-</code> 
- 
-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 ':' -f 1,3 test.txt | rev 
-</code> 
- 
-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 "A,comma,separated,list,for,demonstration,purposes" | cut -d ',' -f 1- --output-delimiter=' ' 
-</code> 
- 
-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 's/:/\t/g' test.txt | cut -f 1-4 
-</code> 
- 
-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.1594805433.txt.gz · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki