
- Unix Commands Reference
- Unix - Tutorial Home
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
awk - Unix, Linux Command
NAME
awk - Finds and Replaces text, database sort/validate/index
SYNOPSIS
awkDESCRIPTION
awk command searches files for text containing a pattern. When a line or text matches, awk performs a specific action on that line/text. The Program statement tells awk what operation to do; Program statement consists of a series of "rules" where each rule specifies one pattern to search for, and one action to perform when a particular pattern is found. A regular expression enclosed in slashes (/) is an awk pattern to match every input record whose text belongs to that set.
OPTIONS
Tag | Description |
---|---|
-F FS --field-separator FS | Use FS for the input field separator (the value of the 'FS' predefined variable). |
-f PROGRAM-FILE --file PROGRAM-FILE | Read the awk program source from the file PROGRAM-FILE, instead of from the first command line argument. |
-mf NNN -mr NNN | The 'f' flag sets the maximum number of fields, and the 'r' flag sets the maximum record size. These options are ignored by 'gawk', since 'gawk' has no predefined limits; they are only for compatibility with the Bell Labs research version of Unix awk. |
-v VAR=VAL --assign VAR=VAL | Assign the variable VAR the value VAL before program execution begins. |
-W traditional -W compat --traditional --compat | Use compatibility mode, in which 'gawk' extensions are turned off. |
-W lint --lint | Give warnings about dubious or non-portable awk constructs. |
-W lint-old --lint-old | Warn about constructs that are not available in the original Version 7 Unix version of awk. |
-W posix --posix | Use POSIX compatibility mode, in which 'gawk' extensions are turned off and additional restrictions apply. |
-W re-interval --re-interval | Allow interval expressions, in regexps. |
-W source=PROGRAM-TEXT --source PROGRAM-TEXT | Use PROGRAM-TEXT as awk program source code. This option allows mixing command line source code with source code from files, and is particularly useful for mixing command line programs with library functions. |
-- | Signal the end of options. This is useful to allow further arguments to the awk program itself to start with a '-'. This is mainly for consistency with POSIX argument parsing conventions. |
'Program' | A series of patterns and actions |
Input-File | If no Input-File is specified then awk applies the Program to "standard input", (piped output of some other command or the terminal. Typed input will continue until end-of-file (typing 'Control-d') |
EXAMPLES
To return the second item($2) from each line of the output from an ls - l listing.
$ ls -l | awk '{print $2}' 13 3 17 7
To print the Row Number (NR), then a dash and space ("- ") and then the first item ($1) from each line in sample.txt.
First create a sample.txt file
Sample Line 1 Sample Line 2 Sample Line 3
$ awk '{print NR "- " $1 }' sample.txt 1 - Sample 2 - Sample 3 - Sample
To print the first item ($1) and then the second last item $(NF-1) from each line in sample.txt.
$ awk '{print $1, $(NF-1) }' sample.txt Sample Line Sample Line Sample Line
To print non-empty line from a file.
$ awk 'NF > 0' sample.txt
To print the length of the longest input line.
$ awk '{ if (length($0) > max) max = length($0) } END { print max }' sample.txt 13
To print seven random numbers from zero to 100, inclusive.
$ awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101 * rand()) }' 24 29 85 15 59 19 81
To count the lines in a file
$ awk 'END { print NR }' sample.txt 3