Solved

awk - sample

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Backing Up Ipset 9 120
remove spaces from a file name linux 3 65
Debugging a shell script 3 31
sqlldr issue 1 15
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…

839 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