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

grep out load average from uptime

what I want to do is take the command uptime, and get the load averages $ uptime 07:01:30 up 20:29, 2 users, load average: 0.32, 0.39, 0.54 I have a feeling this is something I can do with awk, but I am not quite sure how. pls assist.
j0h
  • 1,675
  • 6
  • 27
  • 50
5
votes
2 answers

Messy bash variable

I'm writing a script to ssh in to a list of machines and compare a variable to another value.. I've run into a problem (I have a couple workarounds, but at this point I'm just wondering why this method isn't working). VAR=`ssh $i "awk -F: '/^bar/…
Kyle
  • 269
  • 1
  • 2
  • 8
5
votes
2 answers

How to store a string (line) in an AWK variable

Writing an AWK script, I want to store a line I find in a variable, and only later, if I also match another line, print out that original line. Example: a <-- save this one b c <-- Now that we found c, let's print a Psudo,…
David Parks
  • 30,789
  • 47
  • 185
  • 328
5
votes
1 answer

awk single or double quote usage

Why does the awk command not produce the same results with simple-quotes and double-quotes? root@vm90:/root# who | awk "{ print $2 }" ... root@vm90:/root# who | awk '{ print $2 }' ... I'd like to use awk in a PHP shell_exec() function, which uses…
geohei
  • 696
  • 4
  • 15
5
votes
4 answers

grep (awk) a file from A to first empty line

I need to grep a file from a line containing Pattern A to a first empty line. I used awk but I don't know how to code this empty line. cat ${file} | awk '/Pattern A/,/Pattern B/'
herder
  • 412
  • 2
  • 5
  • 16
5
votes
6 answers

Sed script to edit csv file Or Python

In our project we need to import the csv file to postgres. There are multiple types of files meaning the length of the file changes as some files are with fewer columns and some with all of them. We need a fast way to import this file to postgres. I…
Sujit
  • 2,403
  • 4
  • 30
  • 36
5
votes
2 answers

Use for loop with two variable

I have a question about for loop in Bash I want to run awk command for specific range increasing by 34 but I don't know how to specify two variable in a for loop. I know how to do it for one variable but it is not working for two. this is my code…
user3576287
  • 932
  • 3
  • 16
  • 30
5
votes
3 answers

BASH: grep/awk/sed to extract variable data

UPDATE I need to clarify that Jon8RFC-LT and DOMAIN are also just generic examples of dynamic content, like the IP address and MAC address; nmblookup retrieves and displays entirely dynamic content based on the ip address. If awk is used, I need to…
Jon8RFC
  • 85
  • 1
  • 8
5
votes
4 answers

How can I pull a line out of /etc/passwd if its corresponding shadow entry is 999999?

I want to compare each user in the passwd file with his entry in the shadow file, and print out the whole line of the passwd file if the entry in the shadow file matches 999999. What is the easiest way in Perl to do this? Or I suppose I could awk…
paul44
  • 149
  • 3
5
votes
2 answers

How to pass filename through variable to be read it by awk

Good day, I was wondering how to pass the filename to awk as variable, in order to awk read it. So far I have done: echo file1 > Aenumerar echo file2 >> Aenumerar echo file3 >> Aenumerar AE=`grep -c '' Aenumerar` r=1 while [ $r -le…
Another.Chemist
  • 2,386
  • 3
  • 29
  • 43
5
votes
1 answer

Apache access log for the most common IP address bash script

so i am running a bash script on an apache log file i can sort the IP addresses and show the most common on but it shows it at the bottom of the page no the top how do i show it from highest to lowest this is my script so far cat access_log.txt |…
user3504020
  • 49
  • 1
  • 4
5
votes
3 answers

awk '{print $2,",",$1}' in Emacs Lisp?

Occasionally I use AWK to extract and/or reverse columns in a data file. awk '{print $2,",",$1}' filename.txt How would I do the same using Emacs Lisp? (defun awk (filename col1 &optional col2 col3 col4 col5) "Given a filename and at least once…
anon
5
votes
4 answers

awk to compare two files

I am trying to compare two files and want to print the matching lines... The lines present in the files will be unique File1.txt GERMANY FRANCE UK POLLAND File2.txt POLLAND GERMANY I tried with below command awk 'BEGIN { FS="\n" } ;…
upog
  • 4,965
  • 8
  • 42
  • 81
5
votes
1 answer

Escaping an exclamation mark in a ssh remote call to awk?

I have this line: ssh server1 "df -h | sed 's/%/ /g' | awk '{ if (\$5 > 90 && !/^[a-zA-Z]/) { var1=1 }} END { if (var1 == 1) { print 1 } else { print 0 }}'" However, this produces the following error: bash: !/^[a-zA-Z]/: event not found Not quite…
davidl
  • 151
  • 10
5
votes
2 answers

count pattern occurrence per line

The desired output keeps for each line the first two 'columns' and adds the number of occurrences of 'word' on that same line. Input: string1 string2 aaaaaaaaa word aaaaaaaa word string3 string4 ccccccccccc word dddaaaaaaacccd word dddddaaaaa…
user1875323
  • 137
  • 1
  • 2
  • 9