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
2 answers

Multiline pattern matching in bash

I have a long file of the type Processin SCRIPT10 file.. Submitted batch job 1715572 Processin SCRIPT100 file.. Processin SCRIPT1000 file.. Submitted batch job 1715574 Processin SCRIPT10000 file.. Processin SCRIPT10001 file.. Processin SCRIPT10002…
VojtaK
  • 483
  • 4
  • 13
5
votes
1 answer

How to escape a percent sign in AWK printf?

I'm making an awk statement that will allow me to print a number of unicode nop's to the screen (in testing, 18 of them). It currently looks like the following: awk 'BEGIN {while (c++<18) printf "%u9090"}' When this executes this returns a run time…
Michael A
  • 9,480
  • 22
  • 70
  • 114
5
votes
2 answers

Remove \r\n in awk

I have a simple awk command that converts a date from MM/DD/YYYY to YYYY/MM/DD. However, the file I'm using has \r\n at the end of the lines, and sometimes the date is at the end of the line. awk ' BEGIN { FS = OFS = "|" } { split($27, date,…
richie
  • 91
  • 1
  • 6
5
votes
10 answers

How to add html attributes and values for all lines quickly with vim and plugins?

My os:debian8. uname -a Linux debian 3.16.0-4-amd64 #1 SMP Debian 3.16.39-1+deb8u2 (2017-03-07) x86_64 GNU/Linux Here is my base file. home help variables compatibility modelines searching selection markers indenting reformatting folding…
showkey
  • 482
  • 42
  • 140
  • 295
5
votes
2 answers

How to compare two csv files in UNIX and create delta ( modified/ new records )

I have two csv files old.csv and new.csv. I need only new or updated records from new.csv file. Delete records from new.csv if that is exists in old.csv.…
user6742120
  • 153
  • 2
  • 2
  • 12
5
votes
2 answers

How to pass BASH shell variables into AWK statement

I want to pass 2 shell (bash) variables from into an awk statement. In this example the variables are marker1 and marker2 ubuntu@ubuntutest:/tmp$ echo $marker1 ###___showhost___### ubuntu@ubuntutest:/tmp$ echo…
vtecdec
  • 51
  • 1
  • 1
  • 2
5
votes
1 answer

grep -vf too slow with large files

I am trying filter data from data.txt using patterns stored in a file filter.txt. Like below, grep -v -f filter.txt data.txt > op.txt This grep takes more than 10-15 minutes for 30-40K lines in filter.txt and ~300K lines in data.txt. Is there any…
user3150037
  • 172
  • 1
  • 10
5
votes
2 answers

using variables in search pattern in awk script

#!/usr/local/bin/gawk -f ` { awkvar2="/id=22/"; awkvar3="/end/"; if ($0 ~ awkvar2) { triggered=1; } if (triggered) { print; if ($0 ~ awkvar3) { triggered=0; print…
Omkar
  • 65
  • 1
  • 6
5
votes
3 answers

Subtracting lines in one file from another file

I couldn't find an answer that truly subtracts one file from another. My goal is to remove lines in one file that occur in another file. Multiple occurences should be respected, which means for exammple if one line occurs 4 times in file A and only…
Hawk
  • 63
  • 4
5
votes
4 answers

Add prefix in bash command output

I would like to add a prefix in each new line of my command output. I would like to do this because I will run multiple commands in parralel whiches will log in same output log. I tried to do this with AWK without success runcommand1 | "[prefix1]" +…
Lombric
  • 830
  • 2
  • 11
  • 23
5
votes
2 answers

Sum durations in bash

I am getting execution time of various processes in a file from their respective log files. The file with execution time looks similar to following (it may have hundreds of entries) 1:00:01.11 2:2.20 1.02 The first line is hours:minutes:seconds,…
zatka
  • 63
  • 6
5
votes
5 answers

How to increment a column value with an increasing number in a csv file

I have a text file with 3 columns as below. $ cat test.txt 1,A,300 1,B,300 1,C,300 Till now i have tried as, awk -F, '{$3=$3+1;print}' OFS=, test.txt But output is coming as: 1,A,301 1,B,301 1,C,301 & below is my desired output Now i want to…
swapneil
  • 53
  • 1
  • 1
  • 6
5
votes
4 answers

Extract email addresses from log with grep or sed

Jan 23 00:46:24 portal postfix/smtp[31481]: 1B1653FEA1: to=, relay=mta5.am0.yahoodns.net[98.138.112.35]:25, delay=5.4, delays=0.02/3.2/0.97/1.1, dsn=5.0.0, status=bounced (host mta5.am0.yahoodns.net[98.138.112.35] said: 554…
sherpaurgen
  • 3,028
  • 6
  • 32
  • 45
5
votes
6 answers

UNIX(AIX) script to process a file using only awk or other file processing utilities

I have a task to write a script that will filter an input from an MQ runmqsc command and redirect the output into another file. I have been working around using many other Linux commands piped together and it seems to work just fine in Linux, but my…
Cristian Baciu
  • 133
  • 1
  • 2
  • 15
5
votes
4 answers

Remove lines with specific pattern with Bash

I have a file with one word/character for each line. Example: a abandonado esta estabelecimento o onibus c casa police I need remove lines with specific pattern (ex. pattern "esta"). I tried with awk cat file | awk '!/^esta/' but this solution…
vivas
  • 53
  • 1
  • 5