LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Explain this "cut" example (https://www.linuxquestions.org/questions/linux-newbie-8/explain-this-cut-example-4175491755/)

tt1187 01-18-2014 06:30 PM

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

Ser Olmy 01-18-2014 06:49 PM

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.

rigor 01-18-2014 07:11 PM

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.

chrism01 01-19-2014 05:24 AM

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


All times are GMT -5. The time now is 04:41 AM.