Awk built-in variables:
FS: input field separator OFS: output field separator RS: record separator. Defines a line. Awk reads line by line by default ORS: output record separator NR: number of records (line number) NF: number of fields in a record FILENAME: name of the current input file
- awk ‘condition { action statements }’
- awk ‘/regex/ { action statements }’
- awk ‘expression ~ /regex/ {action statements}’
- awk ‘1; { print «» }’ — double space a file
- awk ‘{ print NR «\t» $0 }’ — numerar un archivo línea a línea
- awk ‘{ printf(«%5d : %s\n», NR, $0) }’ — igual que antes
- awk ‘NF { $b=++a » :» $b }; { print }’ — como antes, pero sólo numera las líneas no vacías
- awk ‘END { print NR }’ — cuenta el número de líneas (como wc -l)
- awk ‘/HOME/ { n++ }; END { print n+0 }’ — muestras el número de ocurrencias de la palabra HOME
- awk ‘$1 > max { max=$1; maxline=$0 }; END { print max, maxline }’ — Find the line containing the largest (numeric) first field
- awk ‘{ print $NF }’ — Print the last field of each line
- awk ‘NF > 4’ — Print every line with more than 4 fields
- awk ‘{ sub(/\r$/,»»); print }’ — Convert Windows/DOS newlines (CRLF) to Unix newlines (LF) from Unix
- awk ‘{ sub(/^[ \t]+/, «»); print }’ — Delete leading whitespace (spaces and tabs) from the beginning of each line (ltrim)
- awk ‘{ gsub(/^[ \t]+|[ \t]+$/, «»); print }’ — Delete both leading and trailing whitespaces from each line (trim). gsub == global sub
- awk ‘{ $1=$1; print }’ — Remove whitespace between fields
- awk ‘{ printf «%79s\n», $0 }’ — Align all text flush right on a 79-column width
- awk ‘{ l=length(); s=int((79-l)/2); printf «%»(s+l)»s\n», $0 }’ — Center all text on a 79-character width
- awk ‘/baz/ { gsub(/foo/, «bar») }; { print }’ — Substitute «foo» with «bar» only on lines that contain «baz»
- awk ‘{ gsub(/scarlet|ruby|puce/, «red»); print}’ — Change «scarlet» or «ruby» or «puce» to «red»
- awk ‘{ a[i++] = $0 } END { for (j=i-1; j>=0;) print a[j–] }’ — Reverse order of lines (emulate «tac»)
- awk ‘/\\$/ { sub(/\\$/,»»); getline t; print $0 t; next }; 1’ — Join a line ending with a backslash with the next line
- awk -F «:» ‘{ print $1 | «sort» }’ /etc/passwd — Print and sort the login names of all users
- awk ‘a != $0; { a = $0 }’ — Remove duplicate, consecutive lines (emulate «uniq»)
- awk ‘!a[$0]++’ — Remove duplicate, nonconsecutive lines COOOOOL!
- awk ‘ORS=NR%5?»,»:»\n»‘ — Concatenate every 5 lines of input with a comma
- awk ‘/regex/ { getline; print }’ — Print the line immediately after a line that matches «/regex/» (but not the line that matches itself)
- awk ‘NR==8,NR==12’ — Print lines 8 to 12 (inclusive). Uses a range pattern. Starts to print from pattern1 and stops in pattern2
- awk ‘/Iowa/,/Montana/’ — Print section of a file between two regular expressions (inclusive)
- awk ‘/regex/,0’ — Print a section of file from regular expression to end of file
- awk ‘/^\[section\]/,/^$/{if ($0!=»») print}’ — imprimir una sección de un archivo de confguración
- awk NF — Delete all blank lines from a file
- awk ‘BEGIN{system(«date»)}’ — llamadas a la shell
- awk ‘NR>2{print line}{line=$0}’ — removes first and last line
Todos estos trucos son originales de: http://www.catonmat.net/blog/awk-one-liners-explained-part-one/
Funciones: http://www.gnu.org/software/gawk/manual/gawk.html#Built_002din
Sed:
Del mismo sitio: http://www.catonmat.net/blog/sed-one-liners-explained-part-one/
Apuntes awk y sed