Fedora Linux Support Community & Resources Center
  #1  
Old 6th May 2012, 06:09 PM
ldelossa Offline
Registered User
 
Join Date: Feb 2012
Location: United States
Posts: 21
windows_7chrome
Help with linux cut command

What's up guys got a quick question,

I'm studying for the linux+ exam and I've come across this command which has me kind of baffled. The command itself is very simple however using it to cut out a section and echo it into the terminal has be kind of confused. Let me explain.

the example has the output of ifconfig which looks like this. I'm trying to extract the hardware address and just have it echo in terminal

$ ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:76:96:A3:73
inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:76ff:fe96:a373/64 Scope:Link
UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7127424 errors:0 dropped:0 overruns:0 frame:0
TX packets:5273519 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6272843708 (5982.2 Mb) TX bytes:1082453585 (1032.3 Mb)
Interrupt:10 Base address:0xde00

The cut command in which I would like to use goes something like this:

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

Now what I'm confused about is why when we set the field switch to 11 (-f 11) does it give us the hardware address we want?? the -d makes a space character the delimiting field but I'm not counting 11 space characters before the hardware address? My intuition would lead me to pass the command

$ ifconfig eth0 | grep HWaddr | cut -d “ “ -f 9 (since I count nine space characters from the begging of hte output to the begging of the hardware address)
00:0C:76:96:A3:73


if anyone can clear this up for me I'd greatly appreciate it.
Reply With Quote
  #2  
Old 6th May 2012, 07:59 PM
marriedto51 Online
Registered User
 
Join Date: Jul 2009
Location: England, UK
Posts: 913
linuxfirefox
Re: Help with linux cut command

Well, when I run the command you give, I do get ten space characters before the MAC address... Make sure you use a fixed-width font in the terminal; here's what I get:
Code:
$ ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:1F:16:70:18:C4
Reply With Quote
  #3  
Old 6th May 2012, 08:15 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,870
linuxfirefox
Re: Help with linux cut command

Also note - some commands will use a tab character to get columns instead of multiple spaces. This will affect what gets counted as a delimiter.

This is one reason why awk works better for manipulating fields --- unless you first pass the data through a tab fill utility.

Last edited by jpollard; 6th May 2012 at 08:18 PM.
Reply With Quote
  #4  
Old 6th May 2012, 08:17 PM
ldelossa Offline
Registered User
 
Join Date: Feb 2012
Location: United States
Posts: 21
windows_7chrome
Re: Help with linux cut command

Yes you are correct I pasted that in error. But to be specific if I use a space as the delimiter I count the spaces from the field then add 1 to specify that field correct?

So now I'm just a little confused about how cut determines multiple spaces. From eth0 to link is 6 spaces, cut with -f 1 gives me eth0 and cut with -f 7 gives me link, my assumption then would be that cut -f 8 would give me the space however it does not cut -f 8 gives me encap:ethernet. I get that by delimiting space characters that -f 8 and -f 7 giving me what it did is correct but why is that -f 1 is eth0 and then f -7 is Link. If spaces are delimiting wouldn't it be the space characters between each space would be counted as field?

I'm sorry if I'm wording this terribly!

While I'm out if is there a way to pipe that operation further into a command that will echo with special characters visible?

Last edited by ldelossa; 6th May 2012 at 08:20 PM.
Reply With Quote
  #5  
Old 6th May 2012, 08:23 PM
meine Online
Registered User
 
Join Date: Apr 2009
Location: Brabant NL
Posts: 215
linuxfirefox
Re: Help with linux cut command

Quote:
Originally Posted by ldelossa View Post
if anyone can clear this up for me I'd greatly appreciate it.
# ifconfig eth0 gives the input
grep HWaddr selects the line containing the string
cut counts chunks with "[space]" counting spaces as a delimeter instead of a TAB

the output of # ifconfig eth0 has a lot of spaces after the first chunk
6 after eth0
1 after Link
2 after encap:Ethernet
1 after HWaddr
2 after the actual address

as marriedto51 mentioned, there are a lot of spaces to count!

here is my output:
Code:
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 6

[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 7
Link
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 8
encap:Ethernet
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 9

[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 10
HWaddr
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 11
00:1D:72:AB:03:13
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 12
nice puzzle for a sunday evening!
__________________
//meine
Fedora 20 -- i3 & LXDE
Reply With Quote
  #6  
Old 6th May 2012, 08:31 PM
ldelossa Offline
Registered User
 
Join Date: Feb 2012
Location: United States
Posts: 21
windows_7chrome
Re: Help with linux cut command

Quote:
Originally Posted by meine View Post
# ifconfig eth0 gives the input
grep HWaddr selects the line containing the string
cut counts chunks with "[space]" counting spaces as a delimeter instead of a TAB

the output of # ifconfig eth0 has a lot of spaces after the first chunk
6 after eth0
1 after Link
2 after encap:Ethernet
1 after HWaddr
2 after the actual address

as marriedto51 mentioned, there are a lot of spaces to count!

here is my output:
Code:
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 6

[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 7
Link
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 8
encap:Ethernet
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 9

[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 10
HWaddr
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 11
00:1D:72:AB:03:13
[meine@acer ~]$ ifconfig eth0 |grep HWaddr |cut -d " " -f 12
nice puzzle for a sunday evening!
I'm starting to understand this now, I think my definition of what a delimiter is was a little off. Your phrasing " cut counts chunks with "[space]" counting spaces as a delimeter instead of a TAB "

I was assuming cut will not count two spaces after each other but they are counting "[space]" as one field. However what happens to the [space] between -f10 and -f11 if it sees another character vs a space it's actually seeing this right: f10 = HWaddr f11 = [(space)00:1D:72:ab:03:13] the space is telling cut to COUNT THIS BLOCK AS A FIELD UNTIL ANOTHER SPACE OCCURS
Reply With Quote
  #7  
Old 6th May 2012, 08:31 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,687
linuxfedorafirefox
Re: Help with linux cut command

This may help you understand how it works:
Code:
eth0_|_|_|_|_|_Link_encap:Ethernet_|_HWaddr_00:1F:16:70:18:C4
 ^   ^ ^ ^ ^ ^  ^     ^            ^   ^       ^
 1   2 3 4 5 6  7     8            9   10      11
The underscores represent spaces, the vertical bars represent the empty fields between the spaces, and the numbers below represent the fields.

cut is definitely not the best choice for this task (have to count the spaces, unclear if spaces or tabs are used). It's easier to use awk, which treats all whitespace the same, making the MAC address the 5th field:
Code:
$ ifconfig eth0 | awk '/HWaddr/ {print $5}'
00:1F:16:70:18:C4
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #8  
Old 6th May 2012, 09:38 PM
ldelossa Offline
Registered User
 
Join Date: Feb 2012
Location: United States
Posts: 21
windows_7chrome
Re: Help with linux cut command

Quote:
Originally Posted by RupertPupkin View Post
This may help you understand how it works:
Code:
eth0_|_|_|_|_|_Link_encap:Ethernet_|_HWaddr_00:1F:16:70:18:C4
 ^   ^ ^ ^ ^ ^  ^     ^            ^   ^       ^
 1   2 3 4 5 6  7     8            9   10      11
The underscores represent spaces, the vertical bars represent the empty fields between the spaces, and the numbers below represent the fields.

cut is definitely not the best choice for this task (have to count the spaces, unclear if spaces or tabs are used). It's easier to use awk, which treats all whitespace the same, making the MAC address the 5th field:
Code:
$ ifconfig eth0 | awk '/HWaddr/ {print $5}'
00:1F:16:70:18:C4
Thank you so much! This defintely cleared it up for me. Yes I know cut is a very basic tool but I'm worried about the exam giving me a question like if you were to cut the hardware address out what would the cut cmd look like, so I feel like I need to know it a little bit.

Thanks for typing that out for me it helped a ton!
Reply With Quote
  #9  
Old 7th May 2012, 06:03 AM
aleph Offline
Banned (for/from) behaving just like everybody else!
 
Join Date: Jul 2007
Location: Nanjing, China
Posts: 1,332
linuxfirefox
Re: Help with linux cut command

I understand this is an exam scenario, which means it is not intended for Real World (TM) usage. But since we're at it, let me point out that scraping the output of ifconfig is never a good idea. The output is not intended to be a machine-readable representation of the network socket information. It is mean to be a human-readable info sheet for admins. Nothing guarantees its output's conformance to any formal specification of format or syntax.

If you write scripts or programs that calls ifconfig to set up the network, then it's fine, it being the intended and documented usage. If you want a machine-friendly data structure of current network information, it's better to dig into the system's API for that, e.g. getifaddrs() in C and other languages that interfaces libc.

And the command ifconfig is recognized as obsolete. Whenever possible, use ip instead.

More examples with ip here: http://stackoverflow.com/questions/2...fconfig-output
__________________
Code:
from rlyeh import cthulhu
cthulhu.fhtagn()
Reply With Quote
  #10  
Old 7th May 2012, 07:00 AM
ldelossa Offline
Registered User
 
Join Date: Feb 2012
Location: United States
Posts: 21
windows_7chrome
Re: Help with linux cut command

Quote:
Originally Posted by aleph View Post
I understand this is an exam scenario, which means it is not intended for Real World (TM) usage. But since we're at it, let me point out that scraping the output of ifconfig is never a good idea. The output is not intended to be a machine-readable representation of the network socket information. It is mean to be a human-readable info sheet for admins. Nothing guarantees its output's conformance to any formal specification of format or syntax.

If you write scripts or programs that calls ifconfig to set up the network, then it's fine, it being the intended and documented usage. If you want a machine-friendly data structure of current network information, it's better to dig into the system's API for that, e.g. getifaddrs() in C and other languages that interfaces libc.

And the command ifconfig is recognized as obsolete. Whenever possible, use ip instead.

More examples with ip here: http://stackoverflow.com/questions/2...fconfig-output
I think what your talking about is a little over my head as I have no programming experience. Basically your presenting me with a function call specified in the api the system uses. However how would I actually access that? I would have to write a program in C which calls this function?
Reply With Quote
  #11  
Old 7th May 2012, 07:34 AM
aleph Offline
Banned (for/from) behaving just like everybody else!
 
Join Date: Jul 2007
Location: Nanjing, China
Posts: 1,332
linuxfirefox
Re: Help with linux cut command

Yes, I was being off-topic and intentionally so, and I admitted doing so. The point was, that grepping the output of ifconfig could be useful but never reliable. But then again, what kind of application scenario asks for such reliability? It's the more or less "bottom-level" networking apps, for example a packet analyzer or a firewall-building program. In such cases the need of a "real" programming language (i.e. one able to interface the low-level API) emerges naturally. Clearly, this is not directly related to your exam questions but I fear someone doing a search will catch this thread in the "Programming" forum and mislead to think that grepping the output of ifconfig (by popen(), for example) is the intended method to get the Ethernet address.

Sorry for causing the confusion. For your exam question I think all the posts here on "cut" and "awk" are great, and the link to the stackoverflow post in my previous post could be helpful.
__________________
Code:
from rlyeh import cthulhu
cthulhu.fhtagn()
Reply With Quote
  #12  
Old 7th May 2012, 08:11 AM
ldelossa Offline
Registered User
 
Join Date: Feb 2012
Location: United States
Posts: 21
linuxsafari
Re: Help with linux cut command

Quote:
Originally Posted by aleph View Post
Yes, I was being off-topic and intentionally so, and I admitted doing so. The point was, that grepping the output of ifconfig could be useful but never reliable. But then again, what kind of application scenario asks for such reliability? It's the more or less "bottom-level" networking apps, for example a packet analyzer or a firewall-building program. In such cases the need of a "real" programming language (i.e. one able to interface the low-level API) emerges naturally. Clearly, this is not directly related to your exam questions but I fear someone doing a search will catch this thread in the "Programming" forum and mislead to think that grepping the output of ifconfig (by popen(), for example) is the intended method to get the Ethernet address.

Sorry for causing the confusion. For your exam question I think all the posts here on "cut" and "awk" are great, and the link to the stackoverflow post in my previous post could be helpful.
Oh okay I understand now, off topic or not your comment was still helpful as I already was contemplating using this command within a script, more insight can only help
Reply With Quote
  #13  
Old 7th May 2012, 05:14 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,687
linuxfedorafirefox
Re: Help with linux cut command

Quote:
Originally Posted by aleph View Post
If you want a machine-friendly data structure of current network information, it's better to dig into the system's API for that
I agree, even though that has nothing to do with ldelossa's question. For example, using Java:
Code:
import java.net.*;
public class getMAC {
 public static void main(String[] args) {
   try {
      NetworkInterface eth = NetworkInterface.getByName(args[0]);
      byte[] MAC = eth.getHardwareAddress();
      String octet = "";
      String sMAC = "";
      for (int i = 0; i < MAC.length; i++) {
         octet = Integer.toHexString(MAC[i]);
         if (octet.length() == 1) {
            octet = "0" + octet;
         }
         sMAC = sMAC + ":" + octet.substring(octet.length() - 2);
      }
      System.out.println(sMAC.substring(1));
   } catch (Exception e) {
      System.out.println(e.getMessage());
   }
 }
}
Save that code in a file getMAC.java, then compile and run (with the network interface as a command-line parameter):
Code:
$ javac getMAC.java
$ java getMAC eth0
00:26:ec:95:83:a7
__________________
OS: Fedora 20 x86_64 | Machine: HP Pavilion a6130n | CPU: AMD 64 X2 Dual-Core 5000+ 2.6GHz | RAM: 5GB PC5300 DDR2 | Disk: 400GB SATA | Video: ATI Radeon HD 4350 512MB | Sound: Realtek ALC888S | Ethernet: Realtek RTL8201N
Reply With Quote
  #14  
Old 7th May 2012, 05:54 PM
stevea Online
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,935
linuxfirefox
Re: Help with linux cut command

ifconfig is obsolete and based on a deprecated socket call. Instead something like ....

ip link show eth0 | awk '/link\/ether/ {print $2}'
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe
Reply With Quote
Reply

Tags
command, cut, linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
linux command Shiva01 Using Fedora 11 22nd September 2009 09:30 PM
Linux SED command help Witlof Using Fedora 8 2nd April 2009 04:18 PM
Linux: GUI vs. Command Line hermouche Linux Chat 19 10th December 2008 01:16 AM
Linux Command for Windows dir s*.* /s Sr Ramitas Using Fedora 6 19th July 2005 11:06 PM


Current GMT-time: 09:51 (Saturday, 01-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Loch Garman - Riverside - Barcarena Travel Photos on Instagram