Author | Paul Rubin, |
Paul Finlason, | |
Richard Stallman | |
Maintainer | Arnold D. Robbins, |
David Trueman | |
Released | 1994 |
Source | gawk.git |
Man | gawk.1 |
Homepage |
---|
The gawk
package contains the GNU version of AWK text processing utility. AWK is
a programming language designed for text processing and typically used as a data
extraction and reporting tool.
The gawk
utility can be used to do quick and easy text pattern matching,
extracting or reformatting. It is considered to be a standard Linux tool for
text processing.
awk (1) - pattern scanning and pr ocessing language awk (1p) - pattern scanning and processing language gawk (1) - pattern scanning and processing language igawk (1) - gawk with include files
OPT | TYPE | ++V | –V | DESC |
---|---|---|---|---|
sort text by the length of lines:
~$ awk '{print length, $0;}' inptut_f | sort -n | grep -oP "(?<=\s).+"
swap 2 columns in a text file with SPS delimiter:
~$ awk '{tmp=$1; $1=$2; $2=tmp; print;}' ../tests/spices_files.md5
search text betwin two lines:
~$ awk '/line_begin/{f=1} /line_end/{f=0;print} f' ./file_name ~$ awk '/line_begin/{f=1} f; /line_end/{f=0}' ./file_name ~$ awk '/line_begin/,/line_end/' ./file_name
print unique line of a file:
~$ awk '!a[$0]++' file
word frequencies count in a file:
#!/usr/bin/env bash
FILE_PATH="../tests/test_big.txt"
awk '{ for (i = 1; i <= NF; i++) {print tolower($i);}}' "$FILE_PATH" \
| sort \
| uniq -c \
| sort -rm
measure total bytes writen since system boot, Linux:
#!/usr/bin/env bash
KERNEL_RELEASE="$(uname -r | sed 's/\.//g')"
if [[ "${KERNEL_RELEASE:0:2}" -gt "26" ]]; then
echo "Total Bytes Writen (TBW)"
awk '/sd/ {print $3"\t"$10 / 2 / 1024, "MB"}' /proc/diskstats
else
echo "Not supported in kernel v2.6 or lower."
fi
print all lines of a input_f:
~$ awk "{print}" input_f ~$ awk "{print 0}" input_f ~$ cat input_f | awk "{print}"
number of lines in a file:
~$ awk 'END {print NR}' input_f
number of fields (columns) in each row of input_f:
~$ awk '{print NF]' input_f
list all lines longer than 50 characters:
~$ awk 'length($0) > 50 {print}' input_f
calculate total size of files in directory including sub directories in Mb:
~$ find ./ -type f -print0 | xargs -0 ls -l | awk '{total += $5} END {print "subtotal: "total/1024/1024"Mb"}'
print just last line of a file or all files in a directory:
~$ awk 'END {print}' inptu_f ~$ find ./ -type f -name "PATERN" -ptint0 | xags -0 -n1 awk 'END {print}'
print file name and the last line for the list of files:
~$ find ./ -type f -name "PATERN" -print0 | xargs -0 -n1 awk '{s=$0};END{print FILENAME,s}' ~$ find ./ -type f -name "PATERN" -print0 | xargs -0 -n1 awk '{s=$0};END{if(s)print FILENAME,s}' ~$ find ./ -type f -name "PATERN" -print0 | xargs -0 -n1 awk 'END{if ($0) print FILENAME,$0}'
remove n-th (2nd in this example) field from the end of the each line of input_f:
~$ awk 'NF>1{$(NF-1)=""};1' input_f
list UserName UserID and GroupID in readable format:
~$ awk -F: '{ print "UN: " $1"*" "UID: " $3"*" "GID: " $4 }' /etc/passwd | column -t -s '*' | nl
coulmn nl
print from line number to end of file:
~$ awk 'NR>=5' input_f
print from line number 4 to 9 or print lines between linen number 3 and 10:
~$ awk 'NR>=4 && NR<=9' input_f
print all even or odd lines numbers:
~$ awk '{if (NR%2 == 0) {print $1}}' input_f ~$ awk '{if (NR%s != 0) {print $1}}' input_f
show accounts with 0 lenth password:
~# awk -F: '($2==""){print $1}'
egrep(1), sed(1), getpid(2), getppid(2), getpgrp(2), getuid(2), geteuid(2), getgid(2), getegid(2), getgroups(2), usleep(3)
- Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger; The AWK Programming Language; Addison-Wesley, 1988. ISBN 0-201-07981-X;
- GAWK: Effective AWK Programming, Edition 4.1;
- TCP/IP Internetworking With gawk https://www.gnu.org/software/gawk/manual/gawkinet/gawkinet.html
- The GNU Awk User’s Guide https://www.gnu.org/software/gawk/manual/gawk.html