If the code has been first tried out on command line, add -o option to get a pretty printed version. Output filename can be passed along -o option, otherwise awkprof.out will be used by default.
$ # adding -o after the one-liner has been tested $ # input filenames and -v would be simply ignored $ awk -o -v OFS='\t' 'NR==FNR{r[$1]=$2; next} {$(NF+1) = FNR==1 ? "Role" : r[$2]} 1' role.txt marks.txt $ # pretty printed version $ cat awkprof.out NR == FNR { r[$1] = $2 next } { $(NF + 1) = FNR == 1 ? "Role" : r[$2] } 1 { print } $ # calling the script $ # note that other command line options have to be provided as usual $ awk -v OFS='\t' -f awkprof.out role.txt marks.txt Dept Name Marks Role ECE Raj 53 class_rep ECE Joel 72 EEE Moi 68 CSE Surya 81 EEE Tia 59 placement_rep ECE Om 92 CSE Amy 67 sports_rep
Summary
So, now you know how to write program files for awk instead of just the one-liners. And about the useful -o option, helps to convert complicated one-liners to pretty printed program files.
Next chapter will discuss a few gotchas and tricks.
Exercises
a) Before explaining the problem statement, here's an example of markdown headers and their converted link version. Note the use of -1for the second occurrence of Summary header. Also note that this sample doesn't simulate all the rules.
# Field separators ## Summary # Gotchas and Tips ## Summary * [Field separators](#field-separators) * [Summary](#summary) * [Gotchas and Tips](#gotchas-and-tips) * [Summary](#summary-1)
For the input file gawk.md, construct table of content links as per the details described below.
Identify all header lines
there are two types of header lines, one starting with # and the other starting with ##
As the input file gawk.md is too long, only the commands to verify your solution is shown.
$ awk -f toc.awk gawk.md > out.md $ diff -sq out.md toc_expected.md Files out.md and toc_expected.md are identical
b) For the input file odd.txt, surround first two whole words of each line with {} that start and end with the same word character. Assume that input file will not require case insensitive comparison. This is a contrived exercise that needs around 10 instructions and makes you recall various features presented in this book.
$ cat odd.txt -oreo-not:a _a2_ roar<=>took%22 RoaR to wow- $ awk -f same.awk odd.txt -{oreo}-not:{a} _a2_ roar<=>took%22 {RoaR} to {wow}-