“awk” is a language for writing scripts that is used to change data and make reports. The “awk” command programming language does not need to be compiled and lets the user use variables, numeric functions, string functions, and logical operators. The letters in “awk” stand for the names of the program’s creators: Aho, Weinberger, and Kernighan.
Operations at awk:
- Reads each line of a file in sequence
- Divides each line of input into separate fields
- Compares the lines and fields of the input to the pattern
- Executes the specified action (or actions) on matching lines
Utilization of the awk Command:
- Making modifications to the data files
- Generating reports in the desired format
Concepts in Programming for the awk Command:
- Line formatting for output
- Iterations and conditions
- Mathematical and string manipulation
Command
$ awk options ‘selection _criteria {action }’ file |
Examples
- Following command will output the 2nd and 3rd columns
awk '{print $2 "t" $3}' elearnbee.txt
- Use following syntax to find all items that start with the letter ‘a’ is
awk '/a/ {print $0}' elearnbee.txt
Some built-in variables in awk
Option | Details |
---|---|
NR | The NR command keeps track of how many records have been read in. Keep in mind that most records are made up of lines. The pattern/action statements are run once for each record in a file when the awk command is used. |
NF | The number of fields in the current input record is kept track of by the NF command. |
FS | The field separator character is in the FS command. This character is used to divide fields on the input line. The default is “white space,” which means nothing but spaces and tabs. To change the field separator, FS can be given to a different character (usually BEGIN). |
RS | The RS command saves the current character used to separate records. Since an input line is the input record by default, a newline is the character used to separate records. |
OFS | The OFS command stores the output field separator, which is used by Awk to separate fields when they are printed. By default, a blank space is used. When print has several parameters that are separated by commas, it will print the value of OFS between each parameter. |
ORS | The ORS command saves the output record separator, which is used by Awk to separate lines of output when they are printed. A new line character is used by default. print adds the contents of ORS to the end of whatever it is told to print. |
Examples
- Following command will print the entire file with line numbers
awk '{print NR,$0}' elearnbee.txt
- Following command will print 1st column using $1 and the last field using NF
awk '{print $1,$NF}' elearnbee.txt
- To see how many lines are in a file
awk 'END { print NR }' elearnbee.txt
- To see lines with more than 15 characters
awk 'length($0) > 15' elearnbee.txt