LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-18-2014, 07:30 PM   #1
tt1187
LQ Newbie
 
Registered: Jan 2014
Posts: 13

Rep: Reputation: Disabled
Explain this "cut" example


Hello,

Could someone explain to me what's going on here with this cut example? I'm reading a book and trying to understand how this works. I've read the man pages, but still am not sure. The example is;

S ifconfig eth0
eth0 Link encap:Ethernet Hwaddr OO:OC:76:96:A3:73

$ ifconfig eth0 | grep HWaddr | cut -d “ “ -f 11
00:0C:76:96:A3:73

I understand it greps this line (eth0 Link encap:Ethernet Hwaddr OO:OC:76:96:A3:73), and then cuts this part of it (cut -d “ “ -f 11). Just don't understand how it knows where to cut from and to.

Please explain what the following are (in terms of what they mean);

-d, --delimiter=DELIM
-f, --fields=LIST
11


Thanks

Last edited by tt1187; 01-18-2014 at 07:32 PM.
 
Old 01-18-2014, 07:49 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
cut returns a number of fields from each line of the input data. A "field" is basically a column.

The "-d" or "--delimiter=" parameter is used to specify the delimiting character(s), in this case a space.

The "-f" or "--fields=" parameter is used to specify which field(s) you want cut to return, in this case the 11th field, that is, the data in the 11th column if the input data is treated as a number of space-separated fields.

If you look at the output from ifconfig, it seems the MAC address returned after piping the data through cut is not in the 11th column, but that's because there are multiple spaces between some of the text. cut will treat two consecutive delimiters as an empty field.
 
1 members found this post helpful.
Old 01-18-2014, 08:11 PM   #3
rigor
Member
 
Registered: Sep 2011
Posts: 212

Rep: Reputation: Disabled
I usually seem to remember things best if I "play" with them, to see how they behave under different circumstances.

So I put the one line grep'ed from your ifconfig output:

Code:
eth0 Link encap:Ethernet Hwaddr OO:OC:76:96:A3:73
in a file named ifconfig_data.txt, then show different variations of the cut like the following.

The command sequence:
Code:
cat ifconfig_data.txt | cut -d ":" -f 1-3
produces this output:
Quote:
eth0 Link encap:Ethernet Hwaddr OO:OC

The command sequence:
Code:
cat ifconfig_data.txt | cut -d ":" -f 1-2
produces this output:
Quote:
eth0 Link encap:Ethernet Hwaddr OO

The command sequence:
Code:
cat ifconfig_data.txt | cut -d ":" -f 1-1
produces this output:
Quote:
eth0 Link encap

The command sequence:
Code:
cat ifconfig_data.txt | cut -d ":" -f 1
produces this output:
Quote:
eth0 Link encap
Hopefully from that you can see that the value for the -f option is either the "number" of a single field or a dash separated range of field numbers. The fields are numbered from 1 to however many there are on an input line. The fields are separated by the delimiter.

The command sequence:
Code:
cat ifconfig_data.txt | cut -d " " -f 5
produces this output:
Quote:
OO:OC:76:96:A3:73
HTH.

Last edited by rigor; 01-18-2014 at 08:13 PM.
 
2 members found this post helpful.
Old 01-19-2014, 06:24 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
It works because 'cut' treats each delimiter instance (in this case space) separately, as opposed to awk which collapses consecutive runs of the delim char
Code:
 ifconfig eth1|grep HWadd
eth1      Link encap:Ethernet  HWaddr 00:02:A5:42:D0:CC  

# using cut
 ifconfig eth1|grep HWadd|cut -d' ' -f11
00:02:A5:42:D0:CC

# using awk
ifconfig eth1|grep HWadd|awk '{print $5}'
00:02:A5:42:D0:CC
HTH
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
unpredictable "delete" "move to trash" or "cut" file menu option dorianrenato Linux - General 3 11-28-2011 07:41 PM
bash: "cut" and "case" help please tredegar Linux - Newbie 14 06-26-2008 05:31 PM
Please explain "386" vs. "686" versions vinoloco Linux - Newbie 4 04-05-2005 08:03 PM
Can you explain the difference between "Free Software (GNU)" and "Open Source"? vharishankar General 5 03-03-2005 10:40 AM
Kernel compiling: any links to explain all the "make xconfig" parameters? vharishankar Linux - General 5 02-21-2005 12:27 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration