How to use CUT command (with sample)
Video
Click here for the video (https://www.youtube.com/watch?v=_FZWHh4nNPI&t=69s) (click the image to go to youtube)
[ v = _FZWHh4nNPI & t = 69s)
CUT command
A command that can retrieve a specific column of data delimited by delimiters such as CSV and TSV.
Use Case
- I want to retrieve the column of table data retrieved from DB.
- I want to check the contents of the file that failed when importing to DB.
→ I can't think of it at all, and it's a basic DB, but there are quite a few other use cases.
syntax
cut option filename
Options to remember
- -f: Specify the column number to retrieve. ex: -f1,5 (remember with filed)
- -d: Specify the field delimiter (remember with delimiter)
- --complement: Extract the data of the column other than the number specified by -f.
(There are others, but this is enough to remember)
Sample command
- cat sample.txt | cut -d'' -f1,5 → Extract the data in the 1st and 5th columns from sample.txt with a space as the delimiter.
- cut -d',' sample.txt -f1 → Extract the data in the first column of the CSV file
- cat sample.txt | grep 'a' | cut -d' ' -f1 → Narrow down a specific column from the result of grep.