Solved

awk - sample

Posted on 2013-05-11
4
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 56

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

624 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