# Delve deeper into AWK

During my work as a deep learning researcher, too often I encounter various file processing with sophisticated requirements.

awk is often the shell script I turn to, but it is too complicated to be well grasped in a short time. Thus, I decide to record some error-prone awk usages here.

• Anyway, awk works as:
• awk for extracting particular file lines containing key words. Note that the key words should be surrounded by /.
• Tricks of the trade of the combination of FNR and NR. Similarily, both FNR and NR record the line number AWK currently reads in. But differently, FNR is loyal to the line number being processing in the current file, when a new file arrives, FNR resets itself and begins with 1 again, while NR directly ignores this. One useful application is to stitch or split two files. One obvisous example goes as:

From left to right, we have two original files and we want to create the third file as shown in the rightmost, which is the combination of the first two files. To achieve this, FNR and NR come to help:

where NR==FNR decides whether the file1 is being processed. While file1 is being processed, array $a$ is constructed with $2 of file1 being the array indices and$0 being the array value. When the condition NR==FNR violates, that is, file2 is being processed, {print a[$1]" "$2} is executed, which outputs the expected file.

• Combing awk and eval to dispatch datasets. For example, we have an image list file img_list.txt containing large amounts of image lists. We also created a large number of empty files ranging from 1, 2, …, n. Our goal is to dispatch these images (every 10,000 images) to different files. The relevant shell script can be written as
• Prepending line number for a file.
• Finding the lines existing in two files simultaneously