Questions tagged [awk]

AWK is an interpreted programming language designed for text processing and typically used as a data extraction and reporting tool. AWK is used largely with Unix systems.

AWK is an interpreted programming language (AWK stands for Aho, Weinberger, and Kernighan) designed for text processing and typically used as data extraction and reporting tool. It is a standard feature of most Unix-like operating systems.

Source: Wikipedia.

An awk program is a series of pattern-action pairs, written as:

condition { action }
condition { action }
...

where condition is typically an expression and action a series of one or more commands, separated by a semi-colon ; character. The input is split into records, and each record is split into fields (by default, records are separated by the newline character and fields by horizontal whitespace.) Per record, each condition is checked and, if true, the commands in the action block are executed. Within the action block, fields are accessed by a 1-based index – e.g. $2 for the second field. If the condition is missing, the action block will always be executed. If the condition is present but the action block is absent, the default action is print $0 which is to print the current line after any transformations. Since a non-zero number is equivalent to true, then awk '1' file instructs awk to perform the default action (print) for every line.

Awk can have an optional BEGIN and optional END, where the BEGIN action is invoked before reading any input, and END action is invoked after all input is read:

BEGIN     { action } 
condition { action }
condition { action }
...
END       { action }

Awk was originally developed by Alfred Aho, Brian Kernighan and Peter Weinberger in 1977 and updated in 1985. Since then, various versions and dialects of awk have emerged. The most common are :

  • awk - the most common and will be found on most Unix-like systems. It also has a well defined IEEE standard.
  • mawk - a fast AWK implementation which it's code base is based on a byte-code interpreter.
  • nawk - during the development of AWK, the developers released a new version (new awk) to avoid confusion but it is itself now very old and lacking functionality present in all POSIX awks.
  • gawk - Also known as GNU awk. The only version in which the developers attempted to add i18n support. Allowed users to write their own C shared libraries to extend it with their own "plug-ins". This version is the standard implementation for Linux.

When asking questions about data processing using awk, please include complete input and desired output.

Some frequently occurring themes:

Books:

Resources:

Other StackExchange Resources:

Related tags:

  • (GNU's version of awk)
  • (A very old, pre-POSIX version also from AT&T)
  • (A different interpreter written by Mike Brennan)
  • (A kindred tool often mentioned in the same breath)
32722 questions
153
votes
7 answers

sed or awk: delete n lines following a pattern

How would I mix patterns and numeric ranges in sed (or any similar tool - awk for example)? What I want to do is match certain lines in a file, and delete the next n lines before proceeding, and I want to do that as part of a pipeline.
Martin DeMello
  • 11,876
  • 7
  • 49
  • 64
147
votes
16 answers

Printing everything except the first field with awk

I have a file that looks like this: AE United Arab Emirates AG Antigua & Barbuda AN Netherlands Antilles AS American Samoa BA Bosnia and Herzegovina BF Burkina Faso BN Brunei Darussalam And I 'd like to invert the order, printing first…
cfischer
  • 24,452
  • 37
  • 131
  • 214
147
votes
10 answers

Get last field using awk substr

I am trying to use awk to get the name of a file given the absolute path to the file. For example, when given the input path /home/parent/child/filename I would like to get filename I have tried: awk -F "/" '{print $5}' input which works…
Mariappan Subramanian
  • 9,527
  • 8
  • 32
  • 33
146
votes
3 answers

What are the differences among grep, awk & sed?

I am confused about the differences between grep, awk and sed in terms of their role in Unix/Linux system administration and text processing.
user288609
  • 12,465
  • 26
  • 85
  • 127
145
votes
9 answers

How to escape a single quote inside awk

I want do the following awk 'BEGIN {FS=" ";} {printf "'%s' ", $1}' But escaping single quote this way does not work awk 'BEGIN {FS=" ";} {printf "\'%s\' ", $1}' How to do this? Thanks for help.
user1096734
138
votes
10 answers

How to use multiple arguments for awk with a shebang (i.e. #!)?

I'd like to execute an gawk script with --re-interval using a shebang. The "naive" approach of #!/usr/bin/gawk --re-interval -f ... awk script goes here does not work, since gawk is called with the first argument "--re-interval -f" (not splitted…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
132
votes
6 answers

using awk with column value conditions

I'm learning awk from The AWK Programming Language and I have a problem with one of the examples. If I wanted to print $3 if $2 is equal to a value (e.g.1), I was using this command which works fine: awk '$2==1 {print $3}' | more But when…
user1687130
  • 1,841
  • 6
  • 19
  • 18
127
votes
6 answers

What are NR and FNR and what does "NR==FNR" imply?

I am learning file comparison using awk. I found syntax like below, awk 'NR==FNR{a[$1];next}$1 in a{print $1}' file1 file2 I couldn't understand what is the significance of NR==FNR in this? If I try with FNR==NR then also I get the same output?…
Amit
  • 1,371
  • 2
  • 9
  • 3
127
votes
10 answers

How to use awk sort by column 3

I have a file (user.csv)like this ip,hostname,user,group,encryption,aduser,adattr want to print all column sort by user, I tried awk -F ":" '{print|"$3 sort -n"}' user.csv , it doesn't work.
user2452340
  • 1,355
  • 2
  • 10
  • 5
126
votes
26 answers

Is there still any reason to learn AWK?

I am constantly learning new tools, even old fashioned ones, because I like to use the right solution for the problem. Nevertheless, I wonder if there is still any reason to learn some of them. awk for example is interesting to me, but for simple…
Bite code
  • 578,959
  • 113
  • 301
  • 329
125
votes
19 answers

Print all but the first three columns

Too cumbersome: awk '{print " "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13}' things
hhh
  • 50,788
  • 62
  • 179
  • 282
123
votes
1 answer

Append file contents to the bottom of existing file in Bash

Possible Duplicate: Shell script to append text to each file? How to append output to the end of text file in SHELL Script? I'm trying to work out the best way to insert api details into a pre-existing config. I thought about using sed to insert…
Grimlockz
  • 2,541
  • 7
  • 31
  • 38
122
votes
6 answers

How to print last two columns using awk

All I want is the last two columns printed.
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
117
votes
5 answers

awk partly string match (if column/word partly matches)

My dummy file looks like this: C1 C2 C3 1 a snow 2 b snowman snow c sowman I want to get line if there is string snow in $3. I can do this like this: awk '($3=="snow" || $3=="snowman") {print}'…
pogibas
  • 27,303
  • 19
  • 84
  • 117
116
votes
5 answers

Show filename and line number in grep output

I am trying to search my rails directory using grep. I am looking for a specific word and I want to grep to print out the file name and line number. Is there a grep flag that will do this for me? I have been trying to use a combination of -n and -l…
trev9065
  • 3,371
  • 4
  • 30
  • 45