# wypisuje PID-y procesów z PPID=1234
ps -o pid,ppid | awk '$2==1234 {print $1}'
# wypisuje PID-y procesów uruchomionych komendą 'qqq'
ps -o pid,cmd | awk '$2~/^qqq$/ {print $1}'
--> tablice w "awk" :
Tablice "awk" są "asocjacyjne" tj indeksy nie muszą być liczbami całkowitymi
lecz mogą być dowolnymi napisami.
Elementy tablic są tworzone podczas pierwszej próby dostępu do takiego
elementu.
Elementy tablic mogą być puste.
Przeglądanie tablic (bez niezamierzonego tworzenia elementów):
for(i in tablica) { print i, tablica[i] }
Usuwanie niepotrzebnego elementu tablicy:
delete tablica[123]Przykład:
awk 'BEGIN {
for(i=1; i<15; i++) tablica[i]="!!!" i "???";
for(elem in tablica) print elem, tablica[elem];
print "1-----------------1";
for(elem in tablica) {
if (tablica[elem]~/!!![0-9]\?\?\?/) {
print elem, tablica[elem];
delete tablica[elem];
}
}
print "2-----------------2";
for(elem in tablica) print elem, tablica[elem];
}'
--> uruchamianie "awk":
# wykonywany jest skrypt "awk" w pliku "skrypt.awk" # istnieją zmienne o nazwach "zm1" i "zm2" oraz mają nadane wartości awk -f skrypt.awk zm1=warosc1 zm2=wartosc2 # zamiast "stdin" jest czytany plik "plik.txt" awk -f skrypt.awk plik.txt--> opis wyrażeń regularnych
--> dodatkowa garść szczegółów na temat "awk":
Przykłady "wzorców":
Example Meaning
----------------------------------------------------
/stop/ line contains the string "stop"
$1 == 3 first field is the number 3
/x/ && NF > 2 more that two fields and contain "x"
NR==10,NR==20 records ten through twenty inclusive
function x(y) define a function called x
Instrukcje jakich można używać w "czynnościach":
{ STATEMENT_LIST }
EXPRESSION
print EXPRESSION-LIST
printf FORMAT, EXPRESSION_LIST
if ( EXPRESSION ) STATEMENT [ else STATEMENT ]
for ( VARIABLE in ARRAY ) STATEMENT
for ( EXPRESSION; EXPRESSION; EXPRESSION) STATEMENT
while ( EXPRESSION ) STATEMENT
do STATEMENT while ( EXPRESSION )
break
continue
next
delete ARRAY[SUBSCRIPT]
exit [ EXPRESSION ]
return [EXPRESSION ]
Operatory jakich można używać w wyrażeniach:
Operation Operator Example Meaning
------------------------------------------------------------
assignment = *= /= %= x += 2 two is added to x
+= -= ^=
conditional ?: x?y:z if x then y else z
logical OR || x||y if (x) 1 else if (y) 1 else 0
logical AND && x&&y if (x) if (y) 1 else 0 else 0
array membership in x in y if (exists(y[x])) 1 else 0
matching ~ !~ $1~/x/ if ($1 contains x) 1 else 0
relational == != > x==y if (x equals y) 1 else 0
<= >= <
concatenation "x" "y" a new string "xy"
add, subtract + - x+y sum of x and y
mul, div, mod * / % x*y product of x and y
unary plus minus + - -x negative of x
logical not ! !x if (x is 0 or null) 1 else 0
exponentiation ^ x^y x to the yth power
inc, dec ++ -- x++ x then add 1 to x
field $ $3 the 3rd field
grouping () ($1)++ increment the 1st field
Predefiniowane zmienne:
Variable Meaning
----------------------------------------------------
ARGC number of command line arguments
ARGV array of command line arguments
FILENAME name of current input file
FNR record number in current file
FS controls the input field separator
NF number of fields in current record
NR number of records read so far
OFMT output format for records
OFS output field separator
ORS output record separator
RLENGTH length of string matched by match function
RS controls input record separator
RSTART start of string match by match function
SUBSEP subscript separator
Predefiniowane funkcje i procedury:
Function Value returned
----------------------------------------------------------------------
atan2(y, x) arctangent of y/x in the range -pi to pi
cos(x) cosine of x x in radians
exp(x) exponentiation of x (e ^ x)
gsub(r, s) number of substitutions substitute s for all r in $0
gsub(r, s, t) number of substitutions substitute s for all r in t
index(s) position of s in $0 0 if not in $0
index(s, t) position of t in s 0 if not in s
int(x) integer part of x
length(s) number of characters in s
log(x) natural log of x
match(s, r) position of r in s or 0 sets RSTART and RLENGTH
rand() random number 0 <= rand < 1
sin(x) sine of x x in radians
split(s, a) number of fields split s into a on FS
split(s, a, fs) number of fields split s into a on fs
sprintf(f, e, ...) formatted string
sqrt(x) square root of x
sub(r, s) number of substitutions substitute s for one r in $0
sub(r, s, t) number of substitutions substitute s for one r in t
substr(s, p) substring of s from p to end
substr(s, p, n) substring of s from p of length n
system(s) exit status execute command s
print print $0 on standard output
print expression, ... prints expressions separated by OFS
print(expression, ...)
printf format, expression, ...
printf(format, expression, ...)
print >"file" print $0 on file "file"
print >>"file" append $0 to file "file"
printf(format, ...) >"file"
printf(format, ...) >>"file"
close("file") close the file "file"
getline read the next record into $0
getline s read the next record into s
getline <"file" read a record from file "file" into $0
getline s <"file" read a record from file "file" into s