Awk Programming

  Home  Operating System Linux  Awk Programming


“Awk Programming frequently Asked Questions in various Awk Programming job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting”



40 Awk Programming Questions And Answers

22⟩ What is the output of the program? #! /usr/bin/awk -f BEGIN { a[1]="google" a[2]="google" for(i=1;i<3;i++) { print a[i] } } a) "google" will print 2 times b) "google" will print 3 times c) program will generate error becasue 2 array elements have the same value d) program will generate syntax error

a) "google" will print 2 times

Output:

root@ubuntu:/home/google# ./test.awk

google

google

root@ubuntu:/home/google#

 162 views

24⟩ What is the output of the program? #! /usr/bin/awk -f #This filename is text.awk BEGIN { print FILENAME } a) test.awk b) program will print nothing c) syntax error d) fatal error

b) program will print nothing

Explanation:

The built-in variable FILENAME is the name of file that awk is currently reading and in this program there is no file listed on the command line.

Output:

root@ubuntu:/home/google# ./test.awk

root@ubuntu:/home/google#

 123 views

36⟩ What is the output of this program? #! /usr/bin/awk -f BEGIN { a=5 while (a<5) { print "google" a++; } } a) nothing will print b) "google" will print 5 times c) program will generate syntax error d) none of the mentioned

a) nothing will print

Explanation:

The condition of while statement is false so commands inside the loop will not execute.

Output:

root@ubuntu:/home/google# ./test.awk

root@ubuntu:/home/google#

 148 views

37⟩ What is the output of this program? #! /usr/bin/awk -f BEGIN { a=6 do { print "google" a++ } while (a<5) } a) nothing will print b) "google" will print 5 times c) "google" will print 4 times d) "google" will print only 1 time

d) "google" will print only 1 time

Explanation:

Even the condition is false of do-while loop, the body is executed once.

Output:

root@ubuntu:/home/google# ./test.awk

google

root@ubuntu:/home/google#

 148 views

40⟩ The command "awk '{if ("9″>"10″) print "google" else print "linux"}'" a) will print "google" b) will print "linux" c) will generate syntax error d) none of the mentioned

c) will generate syntax error

Explanation:

Semicolon is required just before the else statement to parse the statement.

Output:

root@ubuntu:/home/google# awk '{if ("9″>"10″) print "google" else print "linux"}'

awk: {if ("9″>"10″) print "google" else print "linux"}

awk: ^ syntax error

root@ubuntu:/home/google#

 130 views