text2:
2 Chicago
1 Boston
3 Atlanta
script_1:
i will iterate the index of all cities.
#! /bin/bash
awk '{ cities[$1] = $2; }
END {
for ( i in cities )
{
print i ":" cities[i];
}}' text2
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
1:Boston
2:Chicago
3:Atlanta
2. Iterative Statements: for(3)
text2 is same as above
script_1:
"break" is getting out of current loop, "continue" is going to next iteration of the loop and skipping remaining operations.
#! /bin/bash
awk '{ cities[$1] = $2; }
END {
for ( i in cities )
{
if (i==2)
break;
print i ":" cities[i];
}
print "";
for (i in cities)
{
if (i==1)
continue;
print i ":" cities[i];
}
}' text2
terminal:
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
1:Boston
2:Chicago
3:Atlanta
3. Iterative Statements: while
text2:
Hello Chicago
Hello Boston
script_1:
#! /bin/bash
awk '{
i=0;
while(i<=NF)
{
print $i;
i++;
}
}' text2
terminal:
while loop is checking the condition before each iteration, if true, continue to run.The last empty line is for the last empty line in "text2". awk is still taking that as one record.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1
Hello Chicago
Hello
Chicago
Hello Boston
Hello
Boston
No comments:
Post a Comment