bash:cut:cut_using_bytes
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
bash:cut:cut_using_bytes [2020/07/15 09:30] – external edit 127.0.0.1 | bash:cut:cut_using_bytes [2021/01/26 16:05] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== BASH - cut - Cut using Bytes ====== | ||
- | |||
- | ===== 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 " | ||
- | </ | ||
- | |||
- | 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 " | ||
- | </ | ||
- | |||
- | 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 " | ||
- | </ | ||
- | |||
- | 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: | ||
- | </ | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut a Single Byte from the Text file ===== | ||
- | |||
- | <code bash> | ||
- | cut -b 1 test.txt | ||
- | </ | ||
- | |||
- | 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</ | ||
- | |||
- | ----- | ||
- | |||
- | ===== Cut Specific Bytes from a Text File ===== | ||
- | |||
- | <code bash> | ||
- | cut -b 1,3 test.txt | ||
- | </ | ||
- | |||
- | 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 | ||
- | </ | ||
- | |||
- | 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 " | ||
- | </ | ||
- | |||
- | 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 | ||
- | </ | ||
- | |||
- | 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 " | ||
- | </ | ||
- | |||
- | 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 | ||
- | </ | ||
- | |||
- | 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 | ||
- | </ | ||
- | |||
- | 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 | ||
- | </ | ||
- | |||
- | 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. | ||
- | |||
- | ---- | ||
- | |||
bash/cut/cut_using_bytes.1594805433.txt.gz · Last modified: 2020/07/15 09:30 by 127.0.0.1