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
95
votes
9 answers

How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?

I have a file like the following and I would like to print the lines between two given patterns PAT1 and PAT2. 1 2 PAT1 3 - first block 4 PAT2 5 6 PAT1 7 - second block PAT2 8 9 PAT1 10 - third block I have read How to select lines between…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
94
votes
4 answers

awk - concatenate two string variable and assign to a third

In awk, I have 2 fields: $1 and $2. They are both strings that I want to concatenate and assign to a variable.
user3738926
  • 1,178
  • 1
  • 10
  • 17
92
votes
4 answers

Using sed, Insert a line above or below the pattern?

I need to edit a good number of files, by inserting a line or multiple lines either right below a unique pattern or above it. Please advise on how to do that using sed, awk, perl (or anything else) in a shell. Thanks! Example: some text lorem ipsum…
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
89
votes
3 answers

Why doesn't `\d` work in regular expressions in sed?

I am trying to use \d in regex in sed but it doesn't work: sed -re 's/\d+//g' But this is working: sed -re 's/[0-9]+//g'
user2036880
  • 1,463
  • 2
  • 11
  • 9
81
votes
9 answers

Command line: search and replace in all filenames matched by grep

I'm trying to search and replace a string in all files matched by grep: grep -n 'foo' * will give me output in the form: [filename]:[line number]:[text] For each file returned by grep, I'd like to modify the file by replacing foo with bar.
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
81
votes
9 answers

Printing with sed or awk a line following a matching pattern

Question: I'd like to print a single line directly following a line that contains a matching pattern. My version of sed will not take the following syntax (it bombs out on +1p) which would seem like a simple solution: sed -n '/ABC/,+1p' infile I…
user1537723
  • 1,001
  • 1
  • 9
  • 9
76
votes
8 answers

How to remove leading whitespace from each line in a file

I have a file that looks something like this: for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i < 100; i++) for (i = 0; i <…
Lazer
  • 90,700
  • 113
  • 281
  • 364
76
votes
4 answers

Use awk to find average of a column

I'm attempting to find the average of the second column of data using awk for a class. This is my current code, with the framework my instructor provided: #!/bin/awk ### This script currently prints the total number of rows processed. ### You must…
Ben Zifkin
  • 892
  • 2
  • 8
  • 16
76
votes
6 answers

Can we use shell variables in awk?

Can we use shell variables in AWK like $VAR instead of $1, $2? For example: UL=(AKHIL:AKHIL_NEW,SWATHI:SWATHI_NEW) NUSR=`echo ${UL[*]}|awk -F, '{print NF}'` echo $NUSR echo ${UL[*]}|awk -F, '{print $NUSR}' Actually am an oracle DBA we get lot of…
Akhil Chinnu
  • 771
  • 1
  • 5
  • 4
76
votes
9 answers

how to remove the first two columns in a file using shell (awk, sed, whatever)

I have a file with many lines in each line there are many columns(fields) separated by blank " " the numbers of columns in each line are different I want to remove the first two columns how to?
wenzi
  • 1,235
  • 2
  • 11
  • 11
75
votes
10 answers

How can I get the length of an array in awk?

This command echo "hello world" | awk '{split($0, array, " ")} END{print length(array) }' does not work for me and gives this error message awk: line 1: illegal reference to array array Why?
softghost
  • 1,167
  • 1
  • 10
  • 16
75
votes
3 answers

Using AWK to Process Input from Multiple Files

Many people have been very helpful by posting the following solution for AWK'ing multiple input files at once: $ awk 'FNR==NR{a[$1]=$2 FS $3;next}{ print $0, a[$1]}' file2 file1 This works well, but I was wondering if I someone could explain to me…
jkovba
  • 1,229
  • 2
  • 11
  • 20
75
votes
10 answers

Swap two columns - awk, sed, python, perl

I've got data in a large file (280 columns wide, 7 million lines long!) and I need to swap the first two columns. I think I could do this with some kind of awk for loop, to print $2, $1, then a range to the end of the file - but I don't know how to…
Charley Farley
  • 995
  • 2
  • 7
  • 10
73
votes
7 answers

Convert date formats in bash

I have a date in this format: "27 JUN 2011" and I want to convert it to 20110627 Is it possible to do in bash?
vehomzzz
  • 42,832
  • 72
  • 186
  • 216
73
votes
7 answers

How can I format the output of a bash command in neat columns

I have a function which outputs many rows of information which I want to format in columns. The problem is that the width of any particular "cell" (if I may use that term) of data is variable, so piping it to something like awk does not give me…
iconoclast
  • 21,213
  • 15
  • 102
  • 138