I have been using the cut command a lot recently to shed extra data from large text files. I typically use cut with a specific delimiter by activating the -d switch and I thought that the -d switch was required. It turns out that -d is not required and by default -d actually defaults to the delimiter being a tab. When you need to have a tab as the delimiter for cut you simply don’t specify the -d switch. Below I show a couple examples of a file trimmed down using cut with and without the -d switch as well as another way to convert tabs in a file to spaces which then will allow you to use the Linux cut command with the -d” ” switch.
Represent Tab When Using Linux Cut Command:
Below we will use a single file to show how it is not possible to represent a tab when using the Linux cut command and the -d switch unless you first modify the file using the Linux expand command. First we show the file we are working with which is a couple columns from a Bluetooth Class of Device list I have been using to keep track of Bluetooth CoD’s on this page. The below file has been cut down to five lines and includes three columns split by tabs in between each one. The file in these examples is called cod1.txt.
Bluetooth CoD List Used For Linux cut Examples: cod1.txt
- 0x3a010c 001110100000000100001100 21,20,19,17 | 8 | 3,2
- 0x3c0104 001111000000000100000100 21,20,19,18 | 8 | 2
- 0x3c010c 001111000000000100001100 21,20,19,18 | 8 | 3,2
- 0x3e0104 001111100000000100000100 21,20,19,18,17 | 8 | 2
- 0x3e0108 001111100000000100001000 21,20,19,18,17 | 8 | 3
As noted previously there are three columns in the data set above. Each of the columns has a tab separating it from the next column so for instance the first line is 0x3a010c, TAB, 001110100000000100001100, TAB and then 21,20,19,17 | 8 | 3,2. So because we know there is a tab in between each column I had first tried to use the Linux cut command with the -d switch as displayed below.
Linux cut Command Error When Specifying Tab With -d Switch:
- root@bt:~# cut -d"\t" -f1 cod1.txt
- cut: the delimiter must be a single character
- Try `cut --help' for more information.
As you can see it provides an error complaining about the use of -d”\t” because the Linux cut command delimiter switch or -d only accepts a single character no matter what. I also tried other representations of TAB such as ^V, TAB, [::space::], etc. and all failed in the same manner. Anyhow since TAB is the default delimiter when using cut you can simply not use the -d switch and it will return the results you are looking for as shown in the below example.
TAB Delimiter With Linux cut Command: No -d Switch
- root@bt:~# cut -f1 cod1.txt
This time we have success as shown above and without the -d switch. If you wanted the second column you would just use “cut -f2 cod.txt” and the Bluetooth Class of Device’s in binary would be listed instead of the CoD hex list above. Another way to accomplish this goal would be to first convert the cod1.txt files tabs to spaces before using cut which can be done with the Linux expand command as shown below.
Linux cut Command – Expand TAB’s To Spaces Before Using cut:
- root@bt:~# cut -d" " -f1 cod1.txt
- 0x3a010c 001110100000000100001100 21,20,19,17
- 0x3c0104 001111000000000100000100 21,20,19,18
- 0x3c010c 001111000000000100001100 21,20,19,18
- 0x3e0104 001111100000000100000100 21,20,19,18,17
- 0x3e0108 001111100000000100001000 21,20,19,18,17
- root@bt:~# expand cod1.txt > cod2.txt
- root@bt:~# cut -d" " -f1 cod2.txt
In this last example it takes us two commands to get to the result we want. We first show the failed cut command when using cut -d” ” which doesn’t find any spaces because there are only tabs between columns. In this scenario the entire line is printed because we are using a space as the delimiter and there are no spaces or if there are they fall after the comma separated numbers at the end of each row. Next we use the expand command to convert tabs to spaces and output the results to a new file called cod2.txt. Now if we run the first command again on the new file you will see that the results we are looking for are printed to stdout or standard out.
So the easiest way to accomplish your goal is obviously to just not specify the -d switch but if you ever do run into a scenario where the -d switch is required you can always use the Linux expand command to convert any tabs in a file to spaces.