Would you please explain this shell sentence for me?

Would you please explain this shell sentence for me?

Post by fish » Mon, 04 Mar 2002 00:35:22



Hi all,

ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

I wanna know the function of "awk" and "print $2"

fish

 
 
 

Would you please explain this shell sentence for me?

Post by Karl Heye » Mon, 04 Mar 2002 01:02:07



> Hi all,

> ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

> I wanna know the function of "awk" and "print $2"

it gets the interfaces IP numbers. The awks just print the second
field.

karl.

 
 
 

Would you please explain this shell sentence for me?

Post by Dave Carriga » Mon, 04 Mar 2002 01:11:11



> ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

The first awk command prints the second field (whitespace separated) of
any line that matches /inet/. The second awk prints the second field
(colon separated) of that result.

--

UNIX-Apache-Perl-Linux-Firewalls-LDAP-C-DNS |
Seattle, WA, USA                            |
http://www.rudedog.org/                     |

 
 
 

Would you please explain this shell sentence for me?

Post by Tim Robert » Tue, 05 Mar 2002 09:26:07



>Hi all,

>ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

>I wanna know the function of "awk" and "print $2"

awk is basically a string processing language interpreter.  To simplify,
you specify a regular expression to match, and then a block of code to
execute when a match is found.  The first awk says "for every line that
contains the string "inet", split the line into white-space-delimited
fields and print the second such field.

Fields are normally delimited by whitespace (tabs, blanks, newlines, etc.),
but it can be overridden with the -F flag.  The second awk does not have a
regular expression, so it matches every line.  Remember that the input to
this awk will only be the SECOND field on lines that included the string
"inet".  It says "for every input line, chop the line into fields separated
by colons and print the second such field."

Look at an ifconfig output:

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Bcast:127.255.255.255  Mask:255.0.0.0
          UP BROADCAST LOOPBACK RUNNING  MTU:3584  Metric:1
          RX packets:261442 errors:0 dropped:0 overruns:0 frame:0
          TX packets:261442 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0

eth0      Link encap:Ethernet  HWaddr 00:00:21:F9:5B:3F
          inet addr:63.197.32.220  Bcast:63.197.32.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:4959857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:5235925 errors:0 dropped:0 overruns:0 carrier:0
          collisions:9433
...

The first awk chooses only the lines containing inet:
          inet addr:127.0.0.1  Bcast:127.255.255.255  Mask:255.0.0.0
          inet addr:63.197.32.220  Bcast:63.197.32.255  Mask:255.255.255.0
and then prints only the second space-delimited field:

addr:127.0.0.1
addr:63.197.32.220

The second awk should be obvious now.  Divide it into fields at the ':' and
print the second field:

127.0.0.1
63.197.32.220

Thus, it prints all the IP addresses by which this machine can be known.
--

  Providenza & Boekelheide, Inc.

 
 
 

Would you please explain this shell sentence for me?

Post by Faux_Pseud » Tue, 05 Mar 2002 19:55:49


--(Once apon a time, in comp.os.linux.networking,)--
                --(Tim Roberts said it like only they can.)--


>>Hi all,

>>ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

Thanks for really answering his question. It's nice to see something
other than a RTFM.

But couldn't this be done with just one instance of awk instead of
two?
I don't know enough awk to pull it off but i can do it in sed.
And it is more efficiant.

$ time ifconfig | awk '/inet/{print $2}' | awk -F: '{print$2}'
192.168.0.5
127.0.0.1

real    0m0.012s
user    0m0.010s
sys     0m0.010s
You have mail in /var/spool/mail/faux


+-($:~)-
time ifconfig | sed -e '/inet/!d' -e 's/inet addr:\(.*\) \
[[:upper:]].*/\1/g' -e \
's/[[:upper:]].*//' -e 's/[[:blank:]]//g'
192.168.0.5
127.0.0.1

real    0m0.007s
user    0m0.010s
sys     0m0.000s

--

It's a damn poor mind that can only think of one way to spell a word.
        - Andrew Jackson

 
 
 

Would you please explain this shell sentence for me?

Post by Vilmos Sot » Tue, 05 Mar 2002 15:06:59



> Hi all,

> ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

> I wanna know the function of "awk" and "print $2"

Hmmm. Feeding awk into awk. While it makes the program a bit more
readable, but it also starts another process.

$ /sbin/ifconfig | awk -F'[: ]+' '/inet/ {print $4}'
192.168.0.1
127.0.0.1
$

Hint. The '[: ]+', the field separator, is a regular expression.

Vilmos

 
 
 

Would you please explain this shell sentence for me?

Post by Nico Kadel-Garci » Wed, 06 Mar 2002 01:24:39



> --(Once apon a time, in comp.os.linux.networking,)--
> --(Tim Roberts said it like only they can.)--

> >>Hi all,

> >>ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

> Thanks for really answering his question. It's nice to see something
> other than a RTFM.

> But couldn't this be done with just one instance of awk instead of
> two?
> I don't know enough awk to pull it off but i can do it in sed.
> And it is more efficiant.

Well, yes. Or one line of perl, or one small binary. But *legibility* and
ease of re-writing is a big, big deal. And at boot time, when this is
normally run, there really isn't much CPU load going on, so another instance
of awk is not a big deal.
 
 
 

Would you please explain this shell sentence for me?

Post by Faux_Pseud » Wed, 06 Mar 2002 07:04:20


--(Once apon a time, in comp.os.linux.networking,)--
                --(Vilmos Soti said it like only they can.)--


>> Hi all,

>> ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

>> I wanna know the function of "awk" and "print $2"

> Hmmm. Feeding awk into awk. While it makes the program a bit more
> readable, but it also starts another process.

> $ /sbin/ifconfig | awk -F'[: ]+' '/inet/ {print $4}'
> 192.168.0.1
> 127.0.0.1
> $

> Hint. The '[: ]+', the field separator, is a regular expression.

> Vilmos

$ time /sbin/ifconfig | awk -F'[: ]+' '/inet/ {print $4}'
192.168.0.5
127.0.0.1

real    0m0.009s
user    0m0.000s
sys     0m0.010s

Not as fast as my sed version but I knew there was a better way to do
it than two runs of awk.
After all whats 0m0.001s between friends?

Thanks for proving my point.

--

It's a damn poor mind that can only think of one way to spell a word.
        - Andrew Jackson

 
 
 

Would you please explain this shell sentence for me?

Post by Paul D F » Wed, 06 Mar 2002 18:05:05



> > Hi all,

> > ifconfig | awk '/inet/{print $2}' | awk -F: '{print $2}'

> > I wanna know the function of "awk" and "print $2"

In general in awk, commands are of the form:

   /pattern/ {group-of-statements}

Only lines matching the pattern have their statements executed.

In the case above, only lines containing 'inet' are manipulated.

The print command says print the second field of the line (by default
the line is split into fields based on tabs/space separators).

The final awk command says to split the input line on a ':', so
something like:

   interface eth0:1

first gets split into

   eth0:1

and the 2nd awk splits it into

   1

 
 
 

1. Weird C shell behavior: Please help explain.

        I have run into the following "undesirable" behavior of the C shell
on Solaris machines, although it may behave the same on many others as well.
When I csh a file, the C shell seems to need to stat() or lstat() most, if not
all of the sibling directories.  This can be a real problem if I start
in someplace like /home/xyz, which is an automounted directory.  The
problem is that other /home/machine directories may not be present
if the particular client machine is not up.  Then the script hangs!!!

        For example, if I do csh some file and truss it under Solaris
(2.3 or 2.4), I see something like:

getdents(3, 0x0004F014, 1048)                   = 172
lstat("./../.", 0xEFFFCA60)                     = 0
lstat("./../..", 0xEFFFCA60)                    = 0
lstat("./../tolstoy", 0xEFFFCA60)               = 0
lstat("./../bigtime", 0xEFFFCA60) (sleeping...)

In this case, the machine named bigtime was shutdown, so the csh script
just hangs!

Here are the questions:

1)  Why does the C shell need to "rummage around" doing lstat() calls in
    sibling directories?

2)  Since sh, ksh, and perl don't do this, why does csh?  [tcsh does something similar].

    The irony is that I can csh the same file if I start from someplace else.
    Wierd.  Anyone have an explanation????

Ric Steinberger
Software Engineer

"We figured it 17 different ways, and every way we figured it, somebody
 didn't like the way we figured it!"   Buddy Hackett
                                       It's a Mad Mad Mad Mad World

2. Setting-up Anonymous FTP

3. explaining "What is a shell?"

4. Wabi, keyboard, Xservers

5. Explain this (shell/tput oddity)

6. HELP

7. Explain KDE please...........

8. PPP routing problem?

9. TCP: please explain it to me...

10. Please explain overlaping partitions

11. Please Explain Simple Error Msg

12. Please explain those field reported by 'free'

13. Extra Ethernet Interfaces - Please explain