Arithmetic Function (awk)
Option or argument |
Function |
atan2(y,x) |
For nawk. Return the arctangent of y/x in radians. |
cos(x) |
Return the cosine of x |
exp(arg) |
Return the natural exponent of arg |
int(arg) |
Return the integer value of arg |
log(arg) |
Return the natural logarithm of arg |
rand() |
For nawk. Generate a random number between 0 and 1. |
sin(x) |
Return the sine of x |
sqrt(arg) |
Return square root of arg |
srand(expr) |
For nawk. Use expr to set a new seed for random number generator. Default is time of day. |
e.g.
awk '{if($3~/48/) print $0}' grade.txt
who | awk '{print $1 " " $NF " is good."}'
Calculation & END
ls -l | awk '{ totalsize += $5; print totalsize }'
512
1024
2048
ls -l | awk '{ totalsize += $5; print totalsize }' | tail -1
ls -l | awk '{ totalsize += $5 } END { print totalsize }'
Script & Array & Looping – for
awkscript
{
count[length($1)]++
}
END {
for (i=1; i < 9; i++)
print “There are “ count[i] “ accounts with “ i “ letter names.”
}
awk -F: -f awkscript < /etc/passwd
Condition – if
awk -F: ‘{ if (length($1) == 1) print $0 }’ < /etc/passwd
ls -l | awk '{if($1=="-rw-r-----") print $9}'
M – message; S – solution.
M: &FILE=/sysp/cdclient/ndm/cfg/cliapi/ndmapi.cfg:&OSERR=13:&OSMSG=Permission denied
S: remove existing log.
Trimming a Directory - creating new files can make a directory bigger, deleting new files doesn't, it can never get smaller, unless you delete it.
e.g. 1. mv project project.toobig 2. mkdir project 3. mv project.toobig/*.txt project 4. rm -r project.toobig
Huge Files Might Not Take a Lot of Disk Space (if a lot of NUL characters in it), ls -ls to find the space allocation.
ftp <ip>
rename <original name> <new name>
put <local file> <remote file>
asc,pwd,dir,ls,rm,del,bye,Ctrl + C
Summary
1) Full name is “Practical Extraction and Report Language”. Slogan “There's More Than One Way To Do It”.
2) Automatically initialize variables and arrays start with a null or 0 value.
3) Like C, PERL statements must end in a semicolon (;).
4) Comments begin with a pound sign (#).
5) First line should mark with #!/usr/local/bin/perl. E.g.
#!/usr/bin/perl -w # prompt warning
6) list use (), string use ‘’ or “”.
7) compare with Double quotes (“”), Apostrophe (‘’): 1 – can’t replace variable 2 – 反斜线不支持转义字符. 3 – 可以跨多行.
'' – Single Quote / Apostrophe, string no variable replacement
"" - Double quotes, string with variable replacement
`` - Backquote / Backtick, run the command, generate output as input
8) Automatically transform string into numeric. E.g. $result = "12a34" +1; # $result = 13
Argument
-e |
used to enter one line of program. If -e is given, Perl will not look for a filename in the argument list. |
-p |
A –p overrides a –n switch. It causes Perl to assume the following loop. while (<>) { ... # your program goes here } continue { print or die "-p destination: $!\n"; } |
Data Type
Scalars |
纯量, string, number, or a reference. Denoted by $ |
Arrays of scalars |
ordered lists of scalars indexed by number, starting with 0. Denoted by @. Arrays of scalars are faster than associative arrays. |
Hashes |
known as ‘associative arrays (of scalars)’, unordered collections of scalar indexed by string. Denoted by % |
Note: Not divide data type into string, integer and float.