Unix

All about AWK command in Unix – Part 2

Command 11 –
———-
Find text at the start of line [ ^ ]
awk -F "|" '$2-/^s/{print $0}' tabfile.txt

Command 12 –
———-
Find text at the ent of line [ $ ]
awk -F "|" '$2 -/n$/{print $0}' file1.txt

Command 13 –
———-
perform condition check using if

awk -F "|" '{if ($3>2000) print $0;}' file2.txt

Command 14 –
———-
perform condition check using if-else

awk -F "|" '{if($3>=20000) print $2;
else print "*****" ; }' file2.txt

command 15 –
——–
perform condition check using else if

awk -F "|" '{
if ($3>=3000) print $2 "your tax is 30%";
else if($3>=2000) print $2 "your tax is 20%";
else print $2 "your tax is 10%;}' file2.txt

Command 16 –
——–
Begin Block

{Begin Block} {Action} {End}

awk -F "|" 'BEGIN{print "Display records"}{if($3==1000) print$0}' file2.txt

awk -F "|" 'BEGIN{salary=2}{if($3==1000){$3=$3*2;print$0;}' file2.txt

Command 17 –
———
End Block

awk -F "|" {if($3>1000) print $0}END{print "Task is done"}' file2.txt

Command 18 –
———
While Loop

awk -F"|" {i=1;while(i<=2){print $0; i++}} file2.txt

awk -F"|" '{i=1;while(i<=3){print $i; i++;}}' file2.txt

Command 19-
———
For Loop

awk -F "|" '{for(i=1;i<=2;i++){print $0}}' file2.txt

Share This Post

An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.saikumar.me

Lost Password

Register