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
35
votes
4 answers

Is it possible to print the awk output in the same line

The awk output looks like: awk '{print $2}' toto titi tata I want to dispay the output of awk in the same line with space as separator instead of new line awk [option] '{print $2}' toto titi tata Is it possible to do that?
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
35
votes
4 answers

How to add text at the end of each line in unix

I am doing certain text processing operations and finally able to get a file something like this india sudan japan france now I want to add a comment in the above file like in the final file it should be something like india | COUNTRY sudan |…
user2647888
  • 721
  • 1
  • 13
  • 22
34
votes
2 answers

How to print the Nth column of a text file with AWK using argv

Suppose I have a text file with data separated by whitespace into columns. I want to write a shell script which takes as input a filename and a number N and prints out only that column. With awk I can do the following: awk < /tmp/in '{print $2}' >…
speciousfool
  • 2,620
  • 5
  • 28
  • 33
34
votes
7 answers

Replace "\n" with newline in awk

I'm tailing logs and they output \n instead of newlines. I thought I'd pipe the tail to awk and do a simple replace, however I cannot seem to escape the newline in the regex. Here I'm demonstrating my problem with cat instead of…
Cotten
  • 8,787
  • 17
  • 61
  • 98
34
votes
3 answers

How to add a column from a file to another file

I have a file with two columns as 1 1 2 3 3 4 and a file with one column as 6 7 9 I would like to add the second file in the first one. The output should be: 1 1 6 2 3 7 3 4 9
Valerio D. Ciotti
  • 1,369
  • 2
  • 17
  • 27
34
votes
6 answers

Can awk patterns match multiple lines?

I have some complex log files that I need to write some tools to process them. I have been playing with awk but I am not sure if awk is the right tool for this. My log files are print outs of OSPF protocol decodes which contain a text log of the…
Andres Gonzalez
  • 2,129
  • 5
  • 29
  • 43
33
votes
3 answers

How to run a .awk file?

I am converting a CSV file into a table format, and I wrote an AWK script and saved it as my.awk. Here is the my script: #AWK for test awk -F , ' BEGIN { aa = 0; } { hdng = "fname,lname,salary,city"; l1 =…
Sharad
  • 3,562
  • 6
  • 37
  • 59
33
votes
5 answers

print every nth line into a row using gawk

I have a very huge file in which I need to obtain every nth line and print it into a row. My data: 1 937 4.320194 2 667 4.913314 3 934 1.783326 4 940 -0.299312 5 939 2.309559 6 936 3.229496 7 611 -1.41808 8 …
user1269741
  • 437
  • 1
  • 5
  • 7
33
votes
2 answers

Using awk in BASH alias or function

I've a command that works just fine at the command line, but not when I try to put it in an alias or function. $ awk '{print $1}' /tmp/textfile 0 That's correct, as '0' is in position 1 of "textfile". $ alias a="awk '{print $1}' /tmp/textfile" $…
Corn-fuzed
  • 331
  • 1
  • 3
  • 3
32
votes
9 answers

Printing verbose progress from sed and awk

The programs sed and awk usually does their work quietly. Is there any way to get these programs to state what they are doing?
Village
  • 22,513
  • 46
  • 122
  • 163
32
votes
4 answers

Using sed/awk to print lines with matching pattern OR another matching pattern

I need to print lines in a file matching a pattern OR a different pattern using awk or sed. I feel like this is an easy task but I can't seem to find an answer. Any ideas?
rick
  • 1,075
  • 4
  • 20
  • 29
32
votes
6 answers

How to truncate of a real value and discard its non-integer part in awk?

I have the following dataset (all positive values) Input file 1 2.3456 1 5.02 2 3.9763333 2 0.123 I would like to truncate the numbers in the second column and discard its non-integer part. How would one do it in awk? Desired output file 1 2 1…
Tony
  • 2,889
  • 8
  • 41
  • 45
32
votes
2 answers

Awk print string with variables

How do I print a string with variables? Trying this awk -F ',' '{printf /p/${3}_abc/xyz/${5}_abc_def/}' file Need this at output /p/APPLE_abc/xyz/MANGO_abc_def/ where ${3} = APPLE and ${5} = MANGO
Glad
  • 321
  • 1
  • 4
  • 7
32
votes
7 answers

How to run an awk commands in Windows?

I have downloaded gawk from here, but I can't seem to figure out how to use it. I am simply trying to run a command, not to write one.
David Ball
  • 429
  • 1
  • 4
  • 7
32
votes
5 answers

How to initialize an array of arrays in awk?

Is it possible to initialize an array like this in AWK ? Colors[1] = ("Red", "Green", "Blue") Colors[2] = ("Yellow", "Cyan", "Purple") And then to have a two dimensional array where Colors[2,3]="Purple". From another thread I understand that it's…
BearCode
  • 2,734
  • 6
  • 34
  • 37