text2:
1 Hello world!
2 Hello Chicago!
3 Hello Boston!
script_1:
#! /bin/bash
awk '{
if($1 == 1)
print $0;
else if($1 == 2)
print $2;
else
print $3;
}' text2;
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
1 Hello world!
Hello
Boston!
2. Iterative Statements: for(1)
text2:
Hello world!
Hello Chicago!
script_1:
"i=0" is executed at the very beginning.
"i<=NF" is executed before each iteration, if true, continue to run
"i++" is executed after each iteration.
#! /bin/bash
awk '{
for (i=0; i<=NF; i++)
{
print $i;
}
}' text2;
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
Hello world!
Hello
world!
Hello Chicago!
Hello
Chicago!
Don't use float number in condition:
script_1:
#! /bin/bash
awk 'BEGIN {
for (i=1; i>=0; i-=0.05)
{
print i;
}
}'
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
1
0.95
0.9
0.85
0.8
0.75
0.7
0.65
0.6
0.55
0.5
0.45
0.4
0.35
0.3
0.25
0.2
0.15
0.1
0.05
It doesn't output the final 0. Because of the implementation of float number, every time i is decreased by more than 0.05 a little bit.
No comments:
Post a Comment