Solved

awk - sample

Posted on 2013-05-11
4
356 Views
Last Modified: 2013-05-27
$(/sbin/ifconfig -a | /usr/bin/awk 'BEGIN {ips=""} /inet/ && $2 !~ /127.0.0/ {ips=ips", "$2} END {print substr(ips,3)}')

can some one explain me how BEGIN and END works with awk?

what is IPS?

I also want to know how above line executes?
0
Comment
Question by:ittechlab
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39158846
man awk
...
       A pattern-action statement has the form

              pattern { action }
...
       The  special  patterns  BEGIN  and END may be used to capture control before the first input line is read and after the last.  BEGIN and END do not combine
       with other patterns.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39158858
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 395 total points
ID: 39160014
The line is in three parts.

BEGIN {ips=""}
/inet/ && $2 !~ /127.0.0/ {ips=ips", "$2}
END {print substr(ips,3)}

The BEGIN part initialises a variable called "ips" to the empty string, before any lines are read from the "ifconfig" command.

The second part is the body of the awk script - the line processes each input line in turn.  It looks for the string "inet" anywhere on the line, and also looks for the second field on the line "$2") not matching ("!~") the pattern "127.0.0".  If both of these are true (i.e. inet is found, and field 2 idoes not match 127.0.0), it adds a comma and a space to the end of the ips variable, then adds the second input field (here an IP address) to the end of ips too.  It will repeat this for each input line, so as it finds "inet" lines which are not for the loopback address, it adds the IP addresses to the "ips" variable.

The END part is processed after all of the lines in the input text have been read.  At this time, "ips" will contain all of the non-loopback IP addresses of the interfaces on the machine, with ", " between them, and starting with ", ", e.g. ", 192.168.0.1, 10.4.33.22, 169.54.34.2".  It prints out the value of "ips", starting from the third character so that it skips the the comma and space which start ips.
0
 
LVL 12

Expert Comment

by:tel2
ID: 39173493
Hi ittechlab,

I agree with what the experts above have said, but would add that in this example, the:
  BEGIN {ips=""}
section seems to be unnecessary because in awk, variables can be referenced and appended (e.g. {ips=ips", "$2}) even if they have not been explicitly initialised to "" beforehand.

So, this should do the same thing (and based on my tests, it does):
    $(/sbin/ifconfig -a | /usr/bin/awk '/inet/ && $2 !~ /127.0.0/ {ips=ips", "$2} END {print substr(ips,3)}')
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now