LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using cut -f -d with 'tab' as the delimiter (https://www.linuxquestions.org/questions/linux-newbie-8/using-cut-f-d-with-tab-as-the-delimiter-4175446642/)

akuma_linux 01-21-2013 03:14 PM

Using cut -f -d with 'tab' as the delimiter
 
Hi there,

I'm currently learning about the command cut, and specifically using cut with -f & -d.

If I run the command
Code:

ifconfig en1 | grep ether
I get the example output
Code:

ether 00:11:22:33:44:55
If I only want the actual MAC address without the word "ether" I use the command
Code:

ifconfig en1 | grep ether | cut -f2 -d " "
using " " (a space) as the delimiter which gives me the MAC address only, which is the result I want.

The problem I am having is when trying to use this command to get only the capacity% of the hard drive.

If I type the command
Code:

df -h | grep /dev/
and get the output
Code:

/dev/disk0s2  149Gi  91Gi  57Gi    62%    /
I would guess that the command I would need to use to get only the capacity% (62%) would be :
Code:

df -h | grep /dev/ | cut -f5 -d "?"
My question is what would the delimiter be in this scenario, what would replace "?" in my example above. I'm guessing the fields in the output are delimited by a tab, but I'm not sure how to execute this. Can anyone help?

Thanks

unSpawn 01-21-2013 04:17 PM

In a regular well-behaved terminal window "CTRL+v CTRL+TAB" should get you your literal tab, but learning about 'cut' IMO also means learning when and when not to use it. Given output with an equal amount of items per line, space separated values may for example be easier to extract using 'awk' instead:
Code:

df -h | awk '/[0-9]/ {print $5}'

dru8274 01-21-2013 11:23 PM

Quote:

Originally Posted by akuma_linux (Post 4874911)
I'm guessing the fields in the output are delimited by a tab, but I'm not sure how to execute this. Can anyone help?

In this case, the output doesn't contain any tabs. And I can check this by using hexdump to convert the ascii to hex...
Code:

$ df -h | grep /dev/sda6
/dev/sda6      7.9G  6.5G 1006M  87% /

$ df -h | grep /dev/sda6 | hexdump -C
00000000  2f 64 65 76 2f 73 64 61  36 20 20 20 20 20 20 20  |/dev/sda6      |
00000010  37 2e 39 47 20 20 36 2e  35 47 20 31 30 30 36 4d  |7.9G  6.5G 1006M|
00000020  20 20 38 37 25 20 2f 0a                          |  87% /.|

Spaces show up in hex as 20, and tabs as 09. So there are no tabs present.

But if you did need to use tab as a delimiter, then you could have written...
Code:

$ cut -f5 -d $'\t' data.dat
Happy with ur solution... then tick "yes" and mark as Solved!

chrism01 01-21-2013 11:42 PM

Basically, cut relies on one & only one consecutive delimiter instance as separators, so fixed format with eg single spaces or single tabs, commas whatever is do-able.
For variable amts of whitespace eg df, ps etc, use awk.
Awk collapses all consecutive whitespace and treats it as one instance.

akuma_linux 01-22-2013 04:52 PM

Thanks for the answers people, much appreciated


All times are GMT -5. The time now is 09:56 PM.