Skip to main content

GNU awk

gawk - pattern scanning and processing language

AWK is named after the surnames of its three authors, who are Alfred Aho, Peter Weinberger, and Brian Kernighan.

mawk, gawk



Pre-defined variables in awk

  • $0: entire current record/line
  • $1, $2, ...: the first, second,... fields in current record/line
  • NF: number of fields
  • NR: number of records/lines

Built-in functions

Examples

Print the first column of /etc/passwd file. The /etc/passwd uses ':' (colon) as separator between columns.

$ awk -F: '{ print $1}' /etc/passwd

Print process id (PID) column

ps | print '{print $1}'

Create get_tags_pre.awk file with content

/<pre>/,/<\/pre>/ { print; }

Get all content inside <pre> tags

cat inputfile.txt | awk -f get_tags_pre.awk > outputfile.txt

Get just indented lines below "Standard IP access list 11"

root@T420:~# cat acl1.txt |  awk '/access list 11/ && !f{f=1;x=$0;sub(/[^ ].*/,"",x);x=x" ";print;next} f {if (substr($0,1,length(x))==x)print; else f=0}'
Standard IP access list 11
    10 permit 212.1.116.12

Print parent rule and subrules in checkpoint firewall

awk -F";" -v parentrule="" 'BEGIN{print "===START==="}; $2 ~ /8030 -/ || $1 ~ parentrule {split($1,array,".");parentrule=array[1]; print $0;}; END{print "\n===END===\n";}' samplerule.csv

Extract 14-character-lines from a text file

┌──(root㉿LenovoX230)-[/usr/share/seclists/Usernames]
└─# gawk "length == 14" xato-net-10-million-usernames.txt >> 14-characters.txt

References

The GNU Awk User’s Guide

Books about gawk

GAWK: Effective AWK Programming by Arnold D. Robbins

Courses about awk

Linux Administration with sed and awk | Andrew Mallett

Practice

>>> THM | Linux Modules

Popular posts from this blog

Linux command: lastlog

lastlog command reports the most recent login of all users or of a given user. NAME lastlog - reports the most recent login of all users or of a given user SYNOPSIS lastlog [options] DESCRIPTION lastlog formats and prints the contents of the last login log /var/log/lastlog file. The login-name, port, and last login time will be printed. The default (no flags) causes lastlog entries to be printed, sorted by their order in /etc/passwd. OPTIONS The options which apply to the lastlog command are: -b, --before DAYS Print only lastlog records older than DAYS. -h, --help Display help message and exit. -R, --root CHROOT_DIR Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory. -t, --time DAYS Print the lastlog records more recent than DAYS. -u, --user LOGIN|RANGE Print the ...

Powershell: Test-NetConnection

PS C:\Users\tuyen> get-help Test-NetConnection NAME     Test-NetConnection SYNTAX     Test-NetConnection [[-ComputerName] <string>] [-TraceRoute] [-Hops <int>] [-InformationLevel {Quiet |     Detailed}]  [<CommonParameters>]     Test-NetConnection [[-ComputerName] <string>] [-CommonTCPPort] {HTTP | RDP | SMB | WINRM}     [-InformationLevel {Quiet | Detailed}]  [<CommonParameters>]     Test-NetConnection [[-ComputerName] <string>] -Port <int> [-InformationLevel {Quiet | Detailed}]     [<CommonParameters>]     Test-NetConnection [[-ComputerName] <string>] -DiagnoseRouting [-ConstrainSourceAddress <string>]     [-ConstrainInterface <uint32>] [-InformationLevel {Quiet | Detailed}]  [<CommonParameters>] ALIASES     TNC REMARKS     Get-Help cannot find the Help files fo...

Linux command: Check Current Shell

What is your current shell ?