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 Code:
ether 00:11:22:33:44:55 Code:
ifconfig en1 | grep ether | cut -f2 -d " " 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/ Code:
/dev/disk0s2 149Gi 91Gi 57Gi 62% / Code:
df -h | grep /dev/ | cut -f5 -d "?" Thanks |
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}' |
Quote:
Code:
$ df -h | grep /dev/sda6 But if you did need to use tab as a delimiter, then you could have written... Code:
$ cut -f5 -d $'\t' data.dat |
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. |
Thanks for the answers people, much appreciated
|
All times are GMT -5. The time now is 09:56 PM. |