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
5
votes
3 answers

Printing all contents EXCEPT matching range pattern using awk

In Awk, the range pattern is not an expression, so canot use the "!" to not it. so how to implement it (Printing all contents EXCEPT matching range pattern using awk)? e.g. $cat 1.t abd hfdh # fafa deafa 123 # end the result I wanted: cat…
Zhilong Jia
  • 2,329
  • 1
  • 22
  • 34
5
votes
1 answer

Archive of older versions of original Awk source code?

I'm trying to find "historical" versions of the source code to original Awk (a/k/a One True Awk). I found Kernighan's occasionally-updated site, which always seems to link to the latest version, but I haven't yet found a cache of older versions. My…
danfuzz
  • 4,253
  • 24
  • 34
5
votes
3 answers

AWK -Print the next to last field of each line of input file

I have an input file file the content of which constantly is updated with various number of fields, what I am trying to is to print out to a new file the next to last field of each line of input file: awk '{print $(NF-1)}' outputfile…
Zerg12
  • 307
  • 2
  • 5
  • 19
5
votes
2 answers

List all the words in a text file with occurrence counts?

Suppose I have file text.txt as below: she likes cats, and he likes cats too. I'd like my result to look like: she 1 likes 2 cats 2 and 1 he 1 too 1 If putting space , . into it would make the scripts easier, that would be fine. Is there a simple…
JackWM
  • 10,085
  • 22
  • 65
  • 92
5
votes
3 answers

Parse CSV in bash and assign variables

I have a csv of the format (Working on Bash on linux) DN , MAC , Partition , 123 , abc , xyz 321 , asd , asd I am able to parse it using awk by using eval MAC=($(awk -F "," '{print $1}' random.csv)) This is done for each column in the CSV…
Kaptain K
  • 57
  • 1
  • 1
  • 4
5
votes
6 answers

Subtructing n number of columns from two files with AWK

I have two files with N number of columns File1: A 1 2 3 ....... Na1 B 2 3 4 ....... Nb1 File2: A 2 2 4 ....... Na2 B 1 3 4 ....... Nb2 i want a output where 1st column value from File1 will be subtracted from…
5
votes
3 answers

Kill the previous command in a pipeline

I am running a simulation like this ./waf --run scratch/myfile | awk -f filter.awk How can I kill the waf command as soon as filter.awk detects that something happened (e.g. after a specific line is read)? I cannot change waf or myfile. I can…
user000001
  • 32,226
  • 12
  • 81
  • 108
5
votes
8 answers

How can I make the list of letters from A to Z and iterate through them in the shell?

Say I want to iterate from letter A to letter Z in csh shell. How do I succinctly do that? In bash I would do something like for i in 'A B C ...Z'; do echo $i; done The point is I don't want to write A through Z, I want something like [A-Z] Can…
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
5
votes
6 answers

awk/gsub - print everything between double quotes in multiple occurrences per line

I attempting to print all data between double quotes (sampleField="sampleValue"), but am having trouble to get awk and/or sub/gsub to return all instances of data between the double quotes. I'd then like to print all instances on the respective…
Travis Crooks
  • 139
  • 3
  • 12
5
votes
3 answers

Add timestamp to cat output from shell script

I have a small script that cats the output from the ttyUSB to a file I would like to prepend a timestamp to each line. From the command line this does everything I want: $ cat /dev/ttyUSB0 /home/pi/daily_logs/ttyUSSB0 | awk '{ print…
Argus
  • 53
  • 1
  • 4
5
votes
7 answers

Is there any better way to get mac address from arp table?

I want to get a mac address from arp table by using ip address. Currently I am using this command arp -a $ipAddress | awk '{print $4}' This command prints what I want. But I am not comfortable with it and I wonder if there is any built-in way or…
ibrahim
  • 3,254
  • 7
  • 42
  • 56
5
votes
2 answers

Using awk to transpose column to row

I have an input data file: anim gent FZ543 1 FZ543 2 FZ543 3 FZ543 1 FZ547 4 FZ547 3 FZ547 3 FZ547 1 I wanted to transpose these data to:- anim gent FZ543 1 2 3 1 FZ547 4 3 3 1 In other words, I wanted…
Johanna Ramirez
  • 161
  • 1
  • 9
5
votes
4 answers

Conditional Awk hashmap match lookup

I have 2 tabular files. One file contains a mapping of 50 key values only called lookup_file.txt. The other file has the actual tabular data with 30 columns and millions of rows. data.txt I would like to replace the id column of the second file with…
user836087
  • 2,271
  • 8
  • 23
  • 33
5
votes
5 answers

What can awk do that sed can't?

I used sed for a batch ptovess where I could not do it with awk. Vould awk have done it? Or is it more a matter of choice and call awk and sed equivalent for the usage. They both do the common search replace similar with i/o. Is there a good example…
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
5
votes
3 answers

awk select input files from list

basic Awk question, but I can't seem to find an answer anywhere: I have a folder of 50000 txt files, from which I would like to run AWK searches on a subset. I've saved the filenames I want to limit the search to in a separate document. This would…
Rolf Fredheim
  • 300
  • 3
  • 9
1 2 3
99
100