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 [2016/03/18 23:20] billdozor [Awk] |
linux_wiki:awk [2019/05/25 23:50] (current) |
||
---|---|---|---|
Line 6: | Line 6: | ||
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. | + | |
**Checklist** | **Checklist** | ||
* Distro(s): Any | * Distro(s): Any | ||
- | * gawk installed | + | * 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 | ||
---- | ---- | ||
Line 38: | Line 48: | ||
true | 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 | ||
---- | ---- | ||
Line 72: | Line 95: | ||
* If the current line matches the regular expression " | * If the current line matches the regular expression " | ||
* Print the value of x ("{ print x }"). | * Print the value of x ("{ print x }"). | ||
- | * Next, store the current line in the variable " | + | * Next, store the current line in the variable " |
* This has the effect of making the previous line available when evaluating the next line. | * 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. | ||
---- | ---- | ||