🏡 Blamechance's Digital Cottage

Search

Search IconIcon to open search

bash-awk

Last updated Sep 20, 2023 Edit Source

# Resources:

# Usecase:

awk is a scripting language that allows text manipulation, but also arithmetic and conditional logic. It’s commonly used to:

TL;DR:

# Syntax:

# Basic syntax:

1
awk options 'selection _criteria {action }' input-file > output-file

# Quick Cheatsheet:

DescriptionSyntax
Replace all occurrences of a pattern with a replacement string{print $0 ~ /pattern/ ? replacement : $0}
Delete all lines that match a pattern{if ($0 ~ /pattern/) next; print $0}
Print only the lines that match a pattern{if ($0 ~ /pattern/) print $0}
Insert a line before or after the line that matches a pattern{if ($0 ~ /pattern/) {print line; print $0}} or {if ($0 ~ /pattern/) print $0; print line}
Append a line to the end of the file{print line}
Change the delimiter from white space to another characterawk -F'new_char_here' '{ command }' file.txt
To only print the last field per line, use $NFawk '{print $NF }' file.txt

Common commands/arguments: Example syntax incorporation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#print the first and last fields
awk '{print $1,$NF}' employee.txt

#print all lines, with line numbers
awk '{print NR,$0}' employee.txt 

#print lines from 3 to 6
awk 'NR3, NR6 {print NR,$0}' employee.txt

#count the number of lines in a file
awk 'END { print NR }' test.txt

# User-defined and Shell Generated Variables

END: - END is a special block used to specify an action once all records are processed. It’s useful when looking to validate an entire file, such as by counting number of lines.

Positional variables: - When Awk processes a line/record, it will split each delimited set of characters (string) into variables. - “$1”, “$2”, “$3” etc. refer to each field within the record – it is NOT zero-indexed. - “$0” refers to the entire input record (or line) in “awk”.

Variable assignment:

1
2
3
4
5
6
7
8
# syntax:
variable_name=value

# e.g:
awk '{ name = $1; age = $2; print "Name:", name, "Age:", age }' data.txt

# you can also declare variables with defined values, prior to processing: 
awk -v age=25 '{ print "Age:", age }' data.txt

Common special variables:

# Deleting with awk

awk can be used to delete both fields or lines.

Deleting per pattern:

1
2
#The following command will delete all lines that contain the word "foo":
awk '/foo/ delete' input.txt

Deleting per positional variable:

1
2
#The following deletes the second field from each line, but only after printing it first:
awk '/foo/ {print $2} delete $1' input.txt

Notes:

# Conditional logic:

Example if-else logic:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
awk '{  
if(condition_here)  
{  
print "condition matched!"  
}  
else  
{  
print "condition not matched!"  
}  
   
}' sample_data.txt