May 2017
Beginner
552 pages
28h 47m
English
Awk supports a numeric for loop with a syntax similar to C:
for(i=0;i<10;i++) { print $i ; }
Awk also supports a list style for loop that will display the contents of an array:
for(i in array) { print array[i]; }
The following example shows how to collect data into an array and then display it. This script reads lines from /etc/password, splits them into fields at the : markers, and creates an array of names in which the index is the login ID and the value is the user's name:
$ awk 'BEGIN {FS=":"} {nam[$1]=$5} END {for {i in nam} \
{print i,nam[i]}}' /etc/passwd
root root
ftp FTP User
userj Joe User