Challenge yourself with these complex awk programming tasks
These exercises are designed to push your awk skills to the next level. Don't hesitate to consult the awk manual (man awk) or experiment with different approaches.
Create an awk script that can parse a CSV file, correctly handling fields that contain commas within quoted strings.
Name,Age,"Address, City",Country John Doe,30,"123 Main St, Anytown",USA Jane Smith,28,"456 Elm St, Somewhere",Canada
This script uses FPAT to define field patterns, correctly parsing quoted fields with commas. It then removes the quotes and outputs the fields separated by '|'.
Analyze a web server log to generate a report of unique visitors per hour, sorted by the hour with the most visitors.
2023-05-01 08:30:45 192.168.1.1 2023-05-01 08:45:30 192.168.1.2 2023-05-01 09:15:20 192.168.1.1 2023-05-01 09:30:10 192.168.1.3
This script extracts the hour and IP from each log entry, counts unique IPs per hour, and outputs the results. The sort command then orders the output by visitor count.
Normalize a dataset by calculating the z-score for each value in a column.
Name Score Alice 85 Bob 92 Charlie 78 David 88
This script calculates the mean and standard deviation of the scores, then computes and appends the z-score for each entry.
Create an awk script that can find and highlight the longest common substring between two lines of text.
The quick brown fox jumps over the lazy dog A quick brown dog jumps over the lazy fox
This script implements the Longest Common Substring algorithm using dynamic programming, then applies it to find the common substring between two lines of text.
Try modifying these exercises or combining techniques from different exercises to create even more complex awk scripts. The more you practice, the more proficient you'll become with awk's powerful features.