awk - jasper-zanjani/dotfiles GitHub Wiki
ARGC
ARGV
FILENAME
FNR
FS
NF
NR
OFS
ORS
RS
gsub()
sub()
index()
length()
match()
printf()
split()
substr()
tolower()
toupper()
"The basic function of awk is to search files for lines that contain certain patterns." (GEAP: 17
Pattern-scanning utility and processing language, one of the two primary commands which accept regular expressions in Unix systems.
awk programs can be defined inline or in a program-file (PGL: 528)
- inline:
awk options 'program' input-files -
program-file, also "source-file" (GEAP:18):
awk options -f program-file input-files
awk programs can be run without defining input-files, in which case awk will accept input from STDIN
awk programs are the equivalent of sed "instructions", and similarly combine patterns and actions (PGL: 530, GEAP: 17)
Patterns can be:
- regular expressions or fixed strings
- line numbers using builtin variable
NR - predefined patterns
BEGINorEND, whose actions are executed before and after processing any lines of the data file, respectively
change ":" to newlines in PATH variable; equivalent to echo $PATH \| tr ":" "\n"
echo $PATH | awk 'BEGIN {RS=":"} {print}'print the first field of all files in the current directory, taking semicolon ; as the field separator, outputting filename, line number, and first field of matches, with colon : between the filename and line number
awk 'BEGIN {FS=";"} /enable/ {print FILENAME ":" FNR,$1}' *search for string MA in all files, outputting filename, line, and line number for matches
awk '/MA/ {OFS=" " print FILENAME OFS FNR OFS $0} *change field separator (FS) to a colon (:) and run awkscr
awk -F: -f awkscr /etc/passwdflag also works for awk
awk -f script files` `-fprint the first field of each line in the input file
awk '{ print $1 }' listequivalent to grep MA * ({print} is implied)
awk '/MA/' * | awk '/MA/ {print}' *-F flag is followed by field separator
awk -F, '/MA/ { print $1 }' listpipe output of free to awk to get free memory and total memory
free -h | awk '/^Mem|/ {print $3 "/" $2}pipe output of sensors to awk to get CPU temperature
sensors | awk '/^temp1/ {print $2}replace initial "fake." with "real;" in file fake_isbn
awk 'sub(^fake.,"real;")' fake_isbnprint all lines
awk '1 { print }' fileremove file header
awk 'NR>1' fileremove file header
awk 'NR>1 { print } fileprint lines in a range
awk 'NR>1 && NR < 4' fileremove whitespace-only lines
awk 'NF' fileremove all blank lines
awk '1' RS='' fileextract fields
awk '{ print $1, $3}' FS=, OFS=, fileperform column-wise calculations
awk '{ SUM=SUM+$1 } END { print SUM }' FS=, OFS=, filecount the number of nonempty lines
awk '/./ { COUNT+=1 } END { print COUNT }' filecount the number of nonempty lines
awk 'NF { COUNT+=1 } END { print COUNT }' filecount the number of nonempty lines
awk '+$1 { COUNT+=1 } END { print COUNT }' fileArrays
awk '+$1 { CREDITS[$3]+=$1 } END { for (NAME in CREDITS) print NAME, CREDITS[NAME] }' FS=, fileIdentify duplicate lines
awk 'a[$0]++' fileRemove duplicate lines
awk '!a[$0]++' fileRemove multiple spaces
awk '$1=$1' fileJoin lines
awk '{ print $3 }' FS=, ORS=' ' file; echoawk '+$1 { SUM+=$1; NUM+=1 } END { printf("AVG=%f",SUM/NUM); }' FS=, file` | format awk '+$1 { SUM+=$1; NUM+=1 } END { printf("AVG=%6.1f",SUM/NUM); }' FS=, fileConvert to uppercase
awk '$3 { print toupper($0); }' fileChange part of a string
awk '{ $3 = toupper(substr($3,1,1)) substr($3,2) } $3' FS=, OFS=, fileSplit the second field ("EXPDATE") by spaces, storing the result into the array DATE; then print credits ($1) and username ($3) as well as the month (DATE[2]) and year (DATE[3])
awk '+$1 { split($2, DATE, " "); print $1,$3, DATE[2], DATE[3] }' FS=, OFS=, fileawk '+$1 { split($4, GRP, ":"); print $3, GRP[1], GRP[2] }' FS=, fileawk '+$1 { split($4, GRP, /:+/); print $3, GRP[1], GRP[2] }' FS=, fileSearch and replace with comma
awk '+$1 { gsub(/ +/, "-", $2); print }' FS=, fileAdding date
awk 'BEGIN { printf("UPDATED: "); system("date") } /^UPDATED:/ { next } 1' fileModify a field externally
awk '+$1 { CMD | getline $5; close(CMD); print }' CMD="uuid -v4" FS=, OFS=, fileInvoke dynamically generated command
awk '+$1 { cmd = sprintf(FMT, $2); cmd | getline $2; close(cmd); print }' FMT='date -I -d "%s"' FS=, fileJoin data
awk '+$1 { CMD | getline $5; print }' CMD='od -vAn -w4 -t x /dev/urandom' FS=, fileAdd up all first records to {sum}, then print that number out at the end
awk '{sum += $1} END {print sum}' file