Skip to content

Commit e11a402

Browse files
Managorfazlearefinsebastiaanspeck
authored
gawk: add GNU version of awk (#16491)
* Create gawk.md --------- Co-authored-by: Fazle Arefin <[email protected]> Co-authored-by: Sebastiaan Speck <[email protected]>
1 parent 2f6e5d1 commit e11a402

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pages/common/awk.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# awk
22

33
> A versatile programming language for working on files.
4+
> Note: Different implementations of AWK often make this a symlink of their binary.
5+
> See also: `gawk`.
46
> More information: <https://github.com/onetrueawk/awk>.
57
68
- Print the fifth column (a.k.a. field) in a space-separated file:

pages/common/gawk.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# gawk
2+
3+
> GNU version of awk, a versatile programming language for working on files.
4+
> See also: `awk`.
5+
> More information: <https://www.gnu.org/software/gawk/manual/gawk.html>.
6+
7+
- Print the fifth column (a.k.a. field) in a space-separated file:
8+
9+
`gawk '{print $5}' {{path/to/file}}`
10+
11+
- Print the second column of the lines containing "foo" in a space-separated file:
12+
13+
`gawk '/{{foo}}/ {print $2}' {{path/to/file}}`
14+
15+
- Print the last column of each line in a file, using a comma (instead of space) as a field separator:
16+
17+
`gawk {{[-F|--field-separator]}} ',' '{print $NF}' {{path/to/file}}`
18+
19+
- Sum the values in the first column of a file and print the total:
20+
21+
`gawk '{s+=$1} END {print s}' {{path/to/file}}`
22+
23+
- Print every third line starting from the first line:
24+
25+
`gawk 'NR%3==1' {{path/to/file}}`
26+
27+
- Print different values based on conditions:
28+
29+
`gawk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' {{path/to/file}}`
30+
31+
- Print all the lines which the 10th column value is between a min and a max:
32+
33+
`gawk '($10 >= {{min_value}} && $10 <= {{max_value}})'`
34+
35+
- Print table of users with UID >=1000 with header and formatted output, using colon as separator (`%-20s` mean: 20 left-align string characters, `%6s` means: 6 right-align string characters):
36+
37+
`gawk 'BEGIN {FS=":";printf "%-20s %6s %25s\n", "Name", "UID", "Shell"} $4 >= 1000 {printf "%-20s %6d %25s\n", $1, $4, $7}' /etc/passwd`

0 commit comments

Comments
 (0)