strtonum(string)
returns the numeric value of stringint(x)
returns the nearest integer to x, located between x and zero and truncated toward zero. Examples :
int(3)
is 3int(3.9)
is 3int(-3.9)
is −3int(-3)
is −3awk ' \
{ if(length(dataField) > 0) print dataField } \
\
' file.log
o
with 0
:o
/, "0
"); print }'e
with E
in the 2
nd word only :e
/, "E
", $2); print }'[]
:\1
to \9
. Since replacement is a quoted string, the \
characters need to be escaped, and callbacks will appear as \\n
g
or G
(i.e. "global") : replace all matches1
.$0
.o
/, "O
", 1); print result; }'hellO world
o
/, "O
", "g"); print result; }'hellO wOrld
$
.wrapthe whole line:
testString='Lorem ipsum dolor sit amet, consectetur adipiscing elit';
echo "$testString" | awk '{ result=gensub( /(ipsum)/, "\\1", "g"); print result }'
echo "$testString" | awk '{ result=gensub( /.*(ipsum).*/, "\\1", "g"); print result }'
echo "$testString" | awk '{ result=gensub(/^.*(ipsum).*$/, "\\1", "g"); print result }'
Lorem ipsum dolor sit amet, consectetur adipiscing elit ipsum ipsum
X
:
^(..).(.*)
/, "\\1X\\2
", 1, $2); print $1" "result; next; } { print }'alpha brXvo charlie delta echo foXtrot
{ print $0 }
or even { print }
works, but is redundant.h e l l o
cdefghi
z y x
line 1 line 2 line 3
1 2 3
SPACE
(default) or the specified OFS1 line 2 line 3 line 1line 2line 3line
\n
to the output (printf doesn't) :
\n
printf(string, expression list)
syntax :
echo 'abc' | awk '{ switch ($0) { case /[[:lower:]]+/: print "lowercase" break case /[[:upper:]]+/: print "uppercase" break } }'As a one-liner that can be pasted into the shell :
echo 'abc' | awk '{ switch ($0) { case /[[:lower:]]+/: print "lowercase"; break; case /[[:upper:]]+/: print "uppercase"; break; } }'
checkPageExists() { local page=$1 curl -sI "$page" | awk '/^HTTP\/1.1/ { if ($2=="200") exit 0 else exit 1 }' } main() { for page in $pageList; do checkPageExists "$page" || continue done }
system(myCommand)
myVariable=system(myCommand)
stores the return code of myCommand into myVariable
0 1
myCommand | getline
awk 'BEGIN { myCommand = "date --iso-8601=seconds" myCommand | getline myResultVariable close(myCommand) print "Current date = "myResultVariable }'
[ -e someFile ] && rm someFile; for i in 1 2; do echo -e 'hello someFile world' | awk '{ print "\ninput : "$0; if(system("[ -f "$2" ]")) { print $2" exists" } }' ls someFile; touch someFile done; rm someFile
for
loop that runs twicesystem
input : hello someFile world someFile exists mmmkay ls: cannot access 'someFile': No such file or directory make up your mind ! input : hello someFile world someFile
success | failure | |
---|---|---|
Awk | anything else | 0 |
Bash | 0 | anything else |
[ -f "$2" ]
is a Bash success : 0system
returns this code as-isif(system(Bash success))
is false
echo | awk '{ if(system("true")) print "ok" }' (nothing) echo | awk '{ if(system("false")) print "ok" }' ok