Solved

awk - sample

Posted on 2013-05-11
4
357 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 53

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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 …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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