awk Marking keywords

Marking keywords In the following example, we mark Java keywords in a source file. The program adds and tags around each of the keywords that it recognizes. This is a basic example; it works on keywords that are separate words. It does not address the more complicated structures. We load…

awk Rock-Paper-Scissors

Rock-Paper-Scissors Rock-paper-scissors is a popular hand game in which each player simultaneously forms one of three shapes with an outstretched hand. We create this game in AWK. We play the game against the computer, which chooses its options randomly. We seed the random number generator with the srand() function. The…

awk Spell Checking

Spell checking We create an AWK program for spell checking. The script compares the words of the provided text file against a dictionary. Under the standard /usr/share/dict/words path we can find an English dictionary; each word is on a separate line. Inside the BEGIN block, we read the words from…

awk Pipes

Pipes AWK can receive input and send output to other commands via the pipe. In this case, AWK receives output from the echo command. It prints the values of last column. Here, the AWK program sends data to the wc command via the pipe. In the AWK program, we find…

awk Passing variables

Passing variables to AWK AWK has the -v option which is used to assign values to variables. For the next program, we have the general file: mygrep.awk The example simulates the grep utility. It finds the provided word and prints its line and the its starting index. (The program finds…

awk Begin and End

AWK has several built-in variables. They are set by AWK when the program is run. We have already seen the NR, $0, and RSTART variables. The program prints the number of arguments of the AWK program and the first two arguments. ARGC is the number of command-line arguments; in our…

awk Programming

The name awk comes from the initials of its designers: Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan. The original version of awk was written in 1977 at AT&T Bell Laboratories. In 1985 a new version made the programming language more powerful, introducing user-defined functions, multiple input streams,…

Unix Comparison Operator

Comparison Operators String Comparison Description Str1 = Str2 Returns true if the strings are equal Str1 != Str2 Returns true if the strings are not equal -n Str1 Returns true if the string is not null -z Str1 Returns true if the string is null Numeric Comparison Description expr1 -eq…