bash:cut:cut_using_fields
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
bash:cut:cut_using_fields [2020/07/15 09:30] – external edit 127.0.0.1 | bash:cut:cut_using_fields [2021/01/26 16:05] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== BASH - cut - Cut using Fields ====== | ||
- | |||
- | The cut command allows users to cut sections of a text very easily. | ||
- | |||
- | For this, we need to use both the d and the f flag of cut. | ||
- | |||
- | The d flag stands for delimiters and f for fields. | ||
- | |||
- | Delimiters are special characters that separate section of a text from others. | ||
- | |||
- | Common examples include ‘-‘, ‘:’, and ” ” (space). | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut the First Section of Input Stream ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | The above cut command will cut the first section of text (“Let’s” in this case) from the input stream. | ||
- | |||
- | Note that the value to the delimiter flag -d is a single space. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut the First Section of a File ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Returns the first columns of each row inside the file. | ||
- | |||
- | The value provided to the delimiter flag was a colon because that’s how our file separates the columns. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Specific Sections of the Input Stream ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Displays only the first three field of the given input string. | ||
- | |||
- | It is done using a comma-separated array of field positions. | ||
- | |||
- | The output of this command will be ‘Let’s cut this‘. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Specific Sections of a File ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | This command will also provide the same sort of output as the previous command. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Range of Fields from the Input Stream ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Cuts the first five fields of the string and displays it in the terminal. | ||
- | |||
- | The apostrophes are required when space is used as the delimiter between multiple fields. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Range of Fields from a File ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Cuts each of the first three columns of our text file and show it as the output. | ||
- | |||
- | The apostrophes aren’t mandatory for characters like – or :. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Each Entry from a Specific Field and List them Alphabetically ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | List the result alphabetically sorted. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Each Entry from a Field and List them in Alphabetically Reverse Order ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Sorts the entries in a reverse manner. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut from a Fixed Field to the End of the Input Stream ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | This cut command will cut starting from the second field to the end of the string. | ||
- | |||
- | It can be beneficial when you need to cut from a specified position until the end of the input. | ||
- | |||
- | You can change the value of -f while keeping the trailing – on for cutting from different fields. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut from a Fixed Field to the End of a File ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | Start cutting from the specified field and go till the end of each line. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut a Specified Number of Columns Starting from the First One ===== | ||
- | |||
- | <code bash> | ||
- | echo " | ||
- | </ | ||
- | |||
- | Cuts the first five fields of the given input. | ||
- | |||
- | You need to add the preceding hyphen(-) else the output will not match your expectation. | ||
- | |||
- | ----- | ||
- | |||
- | ===== Cut Some Specified Columns of a File Starting from the First One ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ':' | ||
- | </ | ||
- | |||
- | This will start cutting the file file test.txt from the first column and terminate after it has finished cutting the second command. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Multiple Fields of CSV Files ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ',' | ||
- | </ | ||
- | |||
- | The cut command will often prove to be a viable tool when you’re working with massive CSV documents. | ||
- | |||
- | The above command, for example, will cut the first two columns of a comma-separated CSV file called file.csv. | ||
- | |||
- | ---- | ||
- | |||
- | ===== Cut Specific Fields of CSV Files and Sort them in Reverse Order ===== | ||
- | |||
- | <code bash> | ||
- | cut -d ',' | ||
- | </ | ||
- | |||
- | The above command will cut the first, third, and fifth columns of a comma-separated CSV file named file.csv and display the output in the reverse order. | ||
- | |||
- | ---- | ||
- | |||
- | ************************************************************************************************************************************************* | ||
- | ************************************************************************************************************************************************* | ||
- | ************************************************************************************************************************************************* | ||
- | ************************************************************************************************************************************************* | ||
- | |||
- | |||
- | ---- | ||
- | |||
- | ===== 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/cut_using_fields.1594805433.txt.gz · Last modified: 2020/07/15 09:30 by 127.0.0.1