Home > Uncategorized > Have a fun, enjoying by awk, part 1

Have a fun, enjoying by awk, part 1

Let’s learn a bit of awk command once for all. SO HERE WE GO!!!!

Here I wanna describe some certain command sentences where there’s people that find it useful. I’m right one of them.

So, let’s say if you someday need to put in action a simple command line which can be able to get part of a command out and implement it whatever you want.

For exemple.

Have a look right down on this content

ifconfig eth0

eth0 Link encap:Ethernet HWaddr 00:09:6B:7F:FA:29
inet addr:10.0.46.66 Bcast:10.0.46.95 Mask:255.255.255.224
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:414766283 errors:0 dropped:0 overruns:0 frame:0
TX packets:329442281 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:100

Now you predict to catch all of your servers and workstations IPs and Masks up, once they’ve been put in a file list.

ifconfig eth0 | grep inet |awk {‘print $2’} >> /tmp/foobars.txt

addr:10.0.46.66

ifconfig eth0 | grep inet |awk {‘print $4’} >> /tmp/foobars.txt

Mask:255.255.255.224

Don’t be afraid, I’ll make a quick explanation about the command above.

Well, ifconfig command comes out all that description lines previously. After that, everything what came out from the ifconfig command will be redirected to other command in order. The second one (grep) will get the ifconfig output and it must return a line like this:

“inet addr:10.0.46.66 Bcast:10.0.46.95 Mask:255.255.255.224”.
We can notice some gaps after each information, awk (our next command) will get that information and split by gap.

Actually, each gap consists an information stored by a variable starting for $1.

So if you just wanna get the IP’s number and Netmask’s number information based on the list returned (inet addr:10.0.46.66 Bcast:10.0.46.95 Mask:255.255.255.224). where we’ve found:

inet ($1)
addr:10.0.46.66($2),
Bcast:10.0.46.95 ($3),
Mask:255.255.255.224 ($4).

So, here you are.
Now it’s just redirect its output to a file with “>> / tmp/foobars.txt”

Until next time!!!

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment