Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux_wiki:awk [2014/12/08 20:35] billdozor |
linux_wiki:awk [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Awk ====== | ====== Awk ====== | ||
+ | |||
+ | **General Information** | ||
+ | |||
AWK is a pattern scanning and processing language. | AWK is a pattern scanning and processing language. | ||
Gawk is the GNU Project' | Gawk is the GNU Project' | ||
- | I typically stick to using gawk and in most cases, awk is a symlink to gawk. | + | In most cases, awk is a symlink to gawk. |
- | This page will contain useful gawk commands as I run into them. | + | |
- | ===== gsub ===== | + | **Checklist** |
+ | * Distro(s): Any | ||
+ | * Software: awk/gawk installed | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ====== General Awk Variables ====== | ||
+ | |||
+ | * -F":" | ||
+ | * This can be set to any character | ||
+ | * $0 => The entire line (excluding new line at the end) | ||
+ | * $1 - $number => The fields | ||
+ | * NF => Number of fields on the current line | ||
+ | * NR => Current line number | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Gsub ===== | ||
gawk's gsub string function matches and replaces regular expressions. This can replace a grep | sed combination. | gawk's gsub string function matches and replaces regular expressions. This can replace a grep | sed combination. | ||
Line 15: | Line 34: | ||
</ | </ | ||
Notice that the last line "I am fine." is not displayed at all because it doesn' | Notice that the last line "I am fine." is not displayed at all because it doesn' | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Number Comparison ===== | ||
+ | |||
+ | Bash doesn' | ||
+ | |||
+ | Check to see if VERSION is >= to 3.5.3 | ||
+ | <code bash> | ||
+ | VERSION=4.3.0 | ||
+ | echo | gawk -v n1=${VERSION} -v n2=3.5.3 | ||
+ | |||
+ | true | ||
+ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Print Line After Match ===== | ||
+ | |||
+ | Search for a regex string and print the line AFTER the match. | ||
+ | |||
+ | <code bash> | ||
+ | awk '/ | ||
+ | </ | ||
+ | * mystring => regex string to search for | ||
+ | * getline => set the line $0 to the next line | ||
+ | * print => print the line | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Print Line Before Match ===== | ||
+ | |||
+ | This trick allows you to search for a regex string, and print the line BEFORE the match. | ||
+ | |||
+ | FreeIPA Example: I want to get only enabled account usernames. | ||
+ | |||
+ | - Find users and account disabled status< | ||
+ | / | ||
+ | |||
+ | User login: rjones | ||
+ | Account disabled: False | ||
+ | User login: sanderson | ||
+ | Account disabled: True | ||
+ | </ | ||
+ | - Narrow it down to just usernames and True/False values< | ||
+ | / | ||
+ | |||
+ | rjones | ||
+ | False | ||
+ | sanderson | ||
+ | True | ||
+ | </ | ||
+ | - Add in the awk magic that will display ONLY the username with " | ||
+ | / | ||
+ | |||
+ | rjones | ||
+ | </ | ||
+ | |||
+ | **Explanation** | ||
+ | * If the current line matches the regular expression " | ||
+ | * Print the value of x ("{ print x }"). | ||
+ | * Next, store the current line in the variable " | ||
+ | * This has the effect of making the previous line available when evaluating the next line. | ||
+ | * Note: This will not work if the very first line matches the pattern, as x will not contain any lines yet. | ||
+ | |||
+ | ---- | ||
+ |