Cheatsheets
HomeWriteupsResourcesCheatsheets
Linux
Linux
  • Know Yourself and Your System
    • id
    • logname
    • uname
    • w
    • who
    • whoami
  • Surfing / Knowing Your File System
    • cd
    • df
    • du
    • ls
    • pwd
    • stat
    • tree
  • Knowing About Files / Commands
    • alias
    • file
    • info
    • whatis
    • apropos
    • man
    • help
    • history
    • script
  • Manipulating Files / Directories
    • touch
    • mkdir
    • rm
    • rmdir
    • cp
    • mv
    • ln
  • Interacting with Files
    • cat
    • head
    • less
    • middle
    • more
    • tail
  • STROPS / Text Manipulation
    • awk
    • cut
    • grep
    • jq
    • join
    • paste
    • sed
    • sort
    • tr
    • uniq
    • xargs
    • xclip
    • wc
    • tee
    • echo
    • comm
    • diff
    • patch
    • aspell
    • Combos
  • Formatting the Output
    • nl
    • fold
    • fmt
    • pr
    • printf
  • Searching / Finding
    • find
    • locate
    • which
    • whereis
    • type
  • Web Interaction
    • curl
    • wget
  • xxd
  • References
Powered by GitBook
On this page
  • Introduction
  • Syntax
  • Program Structure
  • BEGIN Block
  • BODY Block
  • END Block
  • Important Flags
  • Built in Variables
  • printf statement
  • awk printf conversion characters
  • printf examples
  • Examples

Was this helpful?

  1. STROPS / Text Manipulation

awk

awk linux command cheatsheet by Thamizhiniyan C S

Introduction

Awk is a scripting language used for manipulating data and generating reports.The awk command programming language requires no compiling, and allows the user to use variables, numeric functions, string functions, and logical operators.


Syntax

awk [flags] [select pattern/find(sort)/commands] [input file]

Program Structure

BEGIN Block

BEGIN {awk-commands}

BODY Block

/pattern/ {awk-commands}

END Block

END {awk-commands}

Important Flags

Flags
Description

-F

With this flag you can specify FIELD SEPARATOR (FS), and thus don't need to use the BEGIN

-v

Can be used to specify variables(like we did in BEGIN{OFS=":"})

-D

You can debug your .awk scripts specifying this flag(awk -D script.awk)

-o

To specify the output file (if no name is given after the flag, the output is defaulted to awkprof.out)

f

Reads the AWK program source from the file program-file, instead of from the first command line argument

--dump-variables

Prints a sorted list of global variables and their final values to file


Built in Variables

Variable
Description
Example

ARGC

It implies the number of arguments provided at the command line

ARGV

It is an array that stores the command-line arguments. The array's valid index ranges from 0 to ARGC-1

CONVFMT

It represents the conversion format for numbers. Its default value is %.6g

ENVIRON

It is an associative array of environment variables

FILENAME

It represents the current file name

FS

It represents the (input) field separator and its default value is space

NF

It represents the number of fields in the current record

NR

It represents the number of the current record

FNR

It is similar to NR, but relative to the current file

-

OFMT

It represents the output format number and its default value is %.6g

OFS

It represents the output field separator and its default value is space

ORS

It represents the output record separator and its default value is newline

RLENGTH

It represents the length of the string matched by match function

RS

It represents (input) record separator and its default value is newline

RSTART

It represents the first position in the string matched by match function

SUBSEP

It represents the separator character for array subscripts and its default value is \034

$0

It represents the entire input record

$n

It represents the nth field in the current record where the fields are separated by FS


printf statement

awk's printf statement is the same as that in C, except that the * format specifier is not supported.

SYNTAX: printf format, expr1, expr2, ..., exprn

awk printf conversion characters

Character
Prints expression as

%c

single character

%d

decimal integer

%e

[-]d.dprecisionE[+-]dd

%f

[-]ddd.dprecision

%g

e or f conversion, whichever is shorter, with nonsignificant zeros suppressed

%o

unsigned octal number

%s

string

%x

unsigned hexadecimal number

%%

print a %; no argument is converted

printf examples

Command
Output

49

4.950000e+01

49.500000

49.50

49.5

61

000061

31

|January|

| January|

|January |

|Jan|

| Jan|

|Jan |

%


Examples

Command
Description

To simply print a file

To search for a pattern inside a file

To list the words that are at 1st and 3rd fields

To number the lines

Split based on the character 'o'

Split with the character 'o' and print the total number of characters

Separate rows base with 'o'

To specify field delimeter while outputing

To specify record delimeter while outputing

Checks if fields 2, 3, and 4 are all empty. If they are, it prints a message indicating that not all scores are available for the item identified in the first field

Checks if fields 2, 3, and 4 are all empty. If they are, it prints a message indicating that not all scores are available for the item identified in the first field

Evaluates student scores and assigns a grade of "Pass" or "Fail" based on whether any of their scores are below 50. It then prints the student identifier along with their grade

To identify the performance grade for each student. If the average of the three scores is 80 or more, the grade is 'A'. If the average is 60 or above, but less than 80, the grade is 'B'. If the average is 50 or above, but less than 60, the grade is 'C'. Otherwise the grade is 'FAIL'.

Formats input data by printing each line as is, but inserts a newline character every two lines and a semicolon after every odd-numbered line. This formatting creates a specific structure in the output

PrevioustailNextcut

Last updated 1 year ago

Was this helpful?

awk 'BEGIN {print "Arguments =", ARGC}' One Two Three Four
awk 'BEGIN { 
   for (i = 0; i < ARGC - 1; ++i) { 
      printf "ARGV[%d] = %s\n", i, ARGV[i] 
   } 
}' one two three four
awk 'BEGIN { print "Conversion Format =", CONVFMT }'
awk 'BEGIN { print ENVIRON["USER"] }
awk 'END {print FILENAME}' marks.txt
awk 'BEGIN {print "FS = " FS}' | cat -vte
echo -e "One Two\nOne Two Three\nOne Two Three Four" | awk 'NF > 2'
echo -e "One Two\nOne Two Three\nOne Two Three Four" | awk 'NR < 3'
awk 'BEGIN {print "OFMT = " OFMT}'
awk 'BEGIN {print "OFS = " OFS}' | cat -vte
awk 'BEGIN {print "ORS = " ORS}' | cat -vte
awk 'BEGIN { if (match("One Two Three", "re")) { print RLENGTH } }'
awk 'BEGIN {print "RS = " RS}' | cat -vte
awk 'BEGIN { if (match("One Two Three", "Thre")) { print RSTART } }'
awk 'BEGIN { print "SUBSEP = " SUBSEP }' | cat -vte
awk '{print $0}' marks.txt
awk '{print $3 "\t" $4}' marks.txt
printf "%d", 99/2
printf "%e", 99/2
printf "%f", 99/2
printf "%6.2f", 99/2
printf "%g", 99/2
printf "%o", 99/2
printf "%06o", 99/2
printf "%x", 99/2
printf "|%s|", "January"
printf "|%10s|", "January"
printf "|%-10s|", "January"
printf "|%.3s|", "January"
printf "|%10.3s|", "January"
printf "|%-10.3s|", "January"
printf "%%"
awk '{print}' file.txt
awk '/ctf/' file.txt 
awk '{print $1,$3}' file.txt 
awk '{print NR,$0}'
awk 'BEGIN {FS="o"} {print $2}' file.txt 
awk "BEGIN {FS='o'} {print $1,$3} END{print 'Total Rows=',NR}"
awk 'BEGIN {RS="o"} {print $0}' file.txt 
awk 'BEGIN {OFS=":"} {print $1,$2,$3,$4}' file.txt
awk 'BEGIN {ORS=":"} {print $0}' file.txt 
awk '!($2 && $3 && $4) {print "Not all scores are available for " $1}'
awk '{
    if ( $2 == "" || $3 == "" || $4 == "" ) {
        print "Not all scores are available for", $1;
    }
}'
awk '{
    grade="Pass"
    if ( $2<50 || $3<50 || $4<50) grade="Fail"
    print $1,":",grade
}'
awk '{
    average = ( $2 + $3 + $4 ) / 3
    if ( average >= 80 ) print $0, ":", "A"
    else if ( average >= 60 ) print $0, ":", "B"
    else if ( average >= 50 ) print $0, ":", "C"
    else print $0, ":", "FAIL"
}'
awk '{
    printf ($0)
    if ( NR % 2 == 0 ) printf "\n"
    else printf ";"
}'