Summary
Below are some basic AWK one liners that can assist in dividing data and writing shell scripts.
I used the Apache access_log below as an example but these awk examples can be piped in to just about anything.
Print only row number supplied
The variable below labeled $1 is the row number, the number can be $0 through how many rows are in the file.
Note: row $0 represents the entire line.
awk ‘{print $1}’ access_log
Add data between your varibles
awk ‘{print “This user ” $1 ” accessed the site”}’ access_log
Print Entire Entry if row is equal to value
Here we have some examples that searches the row for matches, the reason we would not use grep is because if you piped the data through
grep then awk-ed it, it would match the entire line and not a given row.
awk ‘{ if ($1 = “10.1.177.41″) print $0}’ access_log
Print Entire Entry if row is LIKE value
awk ‘{ if ($1 ~ 42) print $0}’ access_log
Loops with AWK
If you have a multi rowed text file you can use your AWK syntax within a for loop to achieve your goal.
The below line will take the 1st row from ip.txt and ping each ip address one time with a ttl of 2, i used this to test a bulk IP addresses which were added for connectivity.
—ip.txt—
10.1.1.3 –> 192.168.100.210 255.255.255.0
10.1.1.4 –> 192.168.100.211 255.255.255.0
————
for i in `awk ‘{print $1}’ ip.txt` ; do ping -c 1 -t 2 $i ; done

