Solved

awk - sample

Posted on 2013-05-11
4
355 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 51

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 11

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

746 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

12 Experts available now in Live!

Get 1:1 Help Now