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
Next revision
Previous revision
bash:cut [2019/12/12 21:58] peterbash: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]] 
- 
- 
----- 
- 
-===== Byte Cuts ===== 
- 
-The **b** option provided by the **cut** utility allows us to cut sections of a text-based on their byte positions. 
- 
-Use the cut command with the flag -b followed by the byte numbers for this purpose. 
- 
-==== Cut a Single Byte from the Input Stream ==== 
- 
-<code bash> 
-echo "cutting text from input" | cut -b 1 
-</code> 
- 
-The above command echoes the string “cutting text from input” to the standard output and pipes it as an input to the cut command. 
- 
-The cut command will cut just the first byte(c) from this string as only 1 was provided with the -b flag. 
- 
----- 
- 
- 
-==== Cut Specific Bytes from the Input Stream ==== 
- 
-<code bash> 
-echo "cutting text from input" | cut -b 1,3 
-</code> 
- 
-This command will only cut the first and third byte of the string “cutting text from input” and will display “ct” as its output. 
- 
----- 
- 
-==== Cut a Range of Bytes from the Input Stream ==== 
- 
-<code bash> 
-echo "cutting text from input" | cut -b 1-12 
-</code> 
- 
-The above command will cut the byte range 1-12 from the given string and print out “cutting text” on the standard output. 
- 
-<WRAP important> 
-**WARNING:** Providing byte ranges that are outside of the string’s occupation will result in a message showing “cut: invalid byte or character range”. 
-</WRAP> 
- 
----- 
- 
-==== Cut a Single Byte from the Text file ==== 
- 
-<code bash> 
-cut -b 1 test.txt 
-</code> 
- 
-This command will display only the first bytes of each of the five rows inside the file test.txt. 
- 
-It is equivalent to the command <code bash>cat test.txt | cut -b 1</code> 
- 
------ 
- 
-==== Cut Specific Bytes from a Text File ==== 
- 
-<code bash> 
-cut -b 1,3 test.txt 
-</code> 
- 
-The above command cuts only the first and third bytes of each row. 
- 
-You can specify any byte numbers as long as they fall within the range of bytes available. 
- 
----- 
- 
-==== Cut a Range of Bytes from a Text File ==== 
- 
-<code bash> 
-cut -b 1-12 test.txt 
-</code> 
- 
-This command will output the first one to twelfth bytes of each row in the test.txt file. 
- 
----- 
- 
-==== Cut from a specific Byte to the End of the Input Stream ==== 
- 
-<code bash> 
-echo "cutting text from input" | cut -b 5- 
-</code> 
- 
-The above cut command will cut the text from the fifth byte to the end of the string. 
- 
----- 
- 
-==== Cut from a specific Byte to the End of a File ==== 
- 
-<code bash> 
-cut -b 5- test.txt 
-</code> 
- 
-This command will start cutting every one of the rows of the file test.txt from the fifth-byte position and finish only after each row ends. 
- 
-The trailing hyphen (-) is mandatory for this operation. 
- 
----- 
- 
-==== Cut a Specified Amount of Bytes from the beginning of a string ==== 
- 
-<code bash> 
-echo "cutting text from input" | cut -b -5 
-</code> 
- 
-This command will cut the first five bytes of the input string. 
- 
-Remember to add the preceding hyphen(-) else the output will not be as expected. 
- 
----- 
- 
-==== Cut from the First Byte to a Specified Position from a File ==== 
- 
-<code bash> 
-cut -b -5 test.txt 
-</code> 
- 
-The above command will cut just the first five bytes of each line from the file. 
- 
----- 
- 
-==== Cut Bytes in Alphabetical Order ==== 
- 
-<code bash> 
-cut -b 1-7 test.txt | sort 
-</code> 
- 
-The output of the cut command is passed to the sort command for displaying the first seven bytes of each row alphabetically. 
- 
-For alphabetical sorting, the sort command doesn’t require any options. 
- 
----- 
- 
-==== Cut Bytes in Reverse Order ==== 
- 
-<code bash> 
-cut -b 1-7 test.txt | sort -r 
-</code> 
- 
-This cut command will cut the first 7 bytes from each row and will output them in the reverse order. 
- 
-Look how the output of the cut command is being fed to the sort command using a pipe. 
- 
----- 
- 
- 
-===== Character Cuts ===== 
- 
-==== Cut a single character ==== 
- 
-<code bash> 
-cut -c 3 
-abcdef 
-</code> 
- 
-returns: 
- 
-<code bash> 
-c 
-</code> 
- 
----- 
- 
-==== 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 
-</code> 
- 
-Press CTRL+D to stop inputting 
- 
-returns: 
- 
-<code bash> 
-bcd 
-</code> 
- 
----- 
- 
-==== Cut a variety of characters ==== 
- 
-<code bash> 
-cut -c 2,4,7 
-alongtext  
-</code> 
- 
-returns: 
- 
-<code bash> 
-lne 
-</code> 
- 
----- 
- 
-==== Cut characters up to a specific position ==== 
- 
-<code bash> 
-cut -c -2 
-abcdef 
-</code> 
- 
-returns: 
- 
-<code bash> 
-ab 
-</code> 
- 
----- 
- 
-==== Cut characters from a specific position ==== 
- 
-<code bash> 
-cut -c 2- 
-abcdef 
-</code> 
- 
-returns: 
- 
-<code bash> 
-bcdef 
-</code> 
- 
----- 
- 
-==== Cut various characters ==== 
- 
-<code bash> 
-cut -c 1,6-9,16- 
-alongtextwithnospaces 
-</code> 
- 
-returns: 
- 
-<code bash> 
-atextspaces 
-</code> 
- 
----- 
- 
-===== Field Cuts ===== 
- 
-==== Cut fields from a string, using a delimiter ==== 
- 
-<code bash> 
-cut -f 2- -d ':' 
-23:34:45:56 
-</code> 
- 
-NOTE: -d specifies delimiter 
- 
-returns: 
- 
-<code bash> 
-34:45:56 
-</code> 
- 
----- 
- 
-==== Cut fields from a string, without specifying a delimiter ==== 
- 
-<code bash> 
-cut -f 2 
-er rt fg wd ji 
-</code> 
- 
-returns: 
- 
-<code bash> 
-er rt fg wd ji 
-</code> 
- 
-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     
-</code> 
- 
-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 >users.txt 
-</code> 
  
bash/cut.1576187915.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki