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 |
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. |
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 The command sequence: Code:
cat ifconfig_data.txt | cut -d ":" -f 1-3 Quote:
The command sequence: Code:
cat ifconfig_data.txt | cut -d ":" -f 1-2 Quote:
The command sequence: Code:
cat ifconfig_data.txt | cut -d ":" -f 1-1 Quote:
The command sequence: Code:
cat ifconfig_data.txt | cut -d ":" -f 1 Quote:
The command sequence: Code:
cat ifconfig_data.txt | cut -d " " -f 5 Quote:
|
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 |
All times are GMT -5. The time now is 04:41 AM. |