Skip to content

Latest commit

 

History

History
172 lines (145 loc) · 6.09 KB

cix-gawk.org

File metadata and controls

172 lines (145 loc) · 6.09 KB

awk

|≣|

AuthorPaul Rubin,
Paul Finlason,
Richard Stallman
MaintainerArnold D. Robbins,
David Trueman
Released1994
Sourcegawk.git
Mangawk.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.


Index

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

Receipts

awk

awk::options

OPTTYPE++V–VDESC

awk::examples

awk-161111213155

sort text by the length of lines:

   ~$ awk '{print length, $0;}' inptut_f | sort -n | grep -oP "(?<=\s).+"

sort (1) grep (1)

awk-161120234902

swap 2 columns in a text file with SPS delimiter:

   ~$ awk '{tmp=$1; $1=$2; $2=tmp; print;}' ../tests/spices_files.md5

awk-161122203549

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

awk-161225233030

print unique line of a file:

   ~$ awk '!a[$0]++' file

awk-161120183934

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

sort (1) uniq (1)

awk-170118210126

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

uname (1), sed (1), echo (1)

awk-170822224800

print all lines of a input_f:

   ~$ awk "{print}" input_f 
   ~$ awk "{print 0}" input_f 
   ~$ cat input_f | awk "{print}"

awk-170822225001

number of lines in a file:

   ~$ awk 'END {print NR}' input_f

awk-170822225128

number of fields (columns) in each row of input_f:

   ~$ awk '{print NF]' input_f

awk-170822225351

list all lines longer than 50 characters:

   ~$ awk 'length($0) > 50 {print}' input_f

awk-170822230017

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"}'

find xargs ls

awk-170904001614

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}'

awk-170904182410

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}'

xargs find

awk-171211191635

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

awk-170917194956

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

awk-180102230353

print from line number to end of file:

   ~$ awk 'NR>=5' input_f

awk-180102230535

print from line number 4 to 9 or print lines between linen number 3 and 10:

   ~$ awk 'NR>=4 && NR<=9' input_f

awk-180102230659

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

awk-181202003745

show accounts with 0 lenth password:

   ~# awk -F: '($2==""){print $1}'

awk::files

awk::see-also

egrep(1), sed(1), getpid(2), getppid(2), getpgrp(2), getuid(2), geteuid(2), getgid(2), getegid(2), getgroups(2), usleep(3)

References

Boooks

  • 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;

Links