LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-21-2013, 03:14 PM   #1
akuma_linux
Member
 
Registered: Aug 2006
Distribution: Ubuntu, CentOS
Posts: 31

Rep: Reputation: 15
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
 
Old 01-21-2013, 04:17 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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}'
 
1 members found this post helpful.
Old 01-21-2013, 11:23 PM   #3
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Quote:
Originally Posted by akuma_linux View Post
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!
 
1 members found this post helpful.
Old 01-21-2013, 11:42 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
1 members found this post helpful.
Old 01-22-2013, 04:52 PM   #5
akuma_linux
Member
 
Registered: Aug 2006
Distribution: Ubuntu, CentOS
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks for the answers people, much appreciated
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How use CUT -d 'delimiter' is delimiter is a TAB? frenchn00b Programming 12 11-06-2013 03:17 AM
the cut delimiter dilemma logicalfuzz Linux - General 9 08-12-2010 02:05 PM
How use CUT with several chars delimiter (not a single char) ? frenchn00b Programming 6 04-22-2009 10:11 PM
Need 'cut' with mulit char delimiter endfx Programming 5 03-04-2009 01:37 AM
tab delimiter codename000 Programming 3 04-04-2003 10:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration