30+ TOP AWK Interview Questions and Answers [UPDATED]

If you’re preparing for AWK job interview and whether you’re experienced or fresher & don’t know what kind of questions will be asked in AWK interview, then go through the below 30+ Top AWK Interview Questions and Answers to crack your job interview.

AWK Interview Questions and Answers

  • Question : In The Text File, Some Lines Are Delimited By Colon And Some Are Delimited By Space. Write A Command To Print The Third Field Of Each Line.?

    Answer :

    awk ‘{ if( $0 ~ /:/ ) { FS=”:”; } else { FS =” “; } print $3 }’ filename

  • Question : Write A Command To Find The Sum Of Bytes (size Of File) Of All Files In A Directory.?

    Answer :

    ls -l | awk ‘BEGIN {sum=0} {sum = sum + $5} END {print sum}’

  • Question : Write A Command To Find The Total Number Of Lines In A File Without Using Nr?

    Answer :

    awk ‘BEGIN {sum=0} {sum=sum+1} END {print sum}’ filename

  • Question : Write A Command To Print The Fields In A Text File In Reverse Order?

    Answer :

    awk ‘BEGIN {ORS=””} { for(i=NF;i>0;i–) print $i,” “; print “n”}’ filename

  • Question : Write A Command To Print The Line Number Before Each Line?

    Answer :

    awk ‘{print NR, $0}’ filename.

  • Question : Write A Command To Print The Second And Third Line Of A File Without Using Nr.?

    Answer :

    awk ‘BEGIN {RS=””;FS=”n”} {print $2,$3}’ filename

  • Question : Write A Command To Print The Squares Of Numbers From 1 To 10 Using Awk Command?

    Answer :

    awk ‘BEGIN { for(i=1;i<=10;i++) {print “square of”,i,”is”,i*i;}}’

  • Question : Write A Command To Print Zero Byte Size Files?

    Answer :

    ls -l | awk ‘/^-/ {if ($5 !=0 ) print $9 }’

  • Question : Write A Command To Rename The Files In A Directory With “_new” As Postfix?

    Answer :

    ls -F | awk ‘{print “mv “$1” “$1″.new”}’ | sh

  • Question : How Add A First Record And Last Record To The Current File In Linux?

    Answer :

    sed -i -e ‘1i Header’ -e ‘$a Trailor’ test_file.txt

  • Question : How To Display Even Number Of Records Into One File And Odd Number Of Records Into Another File?

    Answer :

    awk ‘NR %2 == 0’ test_files.txt

    awk ‘NR %2 !=  0’ test_files.txt

  • Question : How To Get Only Zero Byte Files Which Are Present In The Directory?

    Answer :

    ls -ltr| awk ‘/^-/ { if($5 ==0) print $9 }’

  • Question : How To Print Only Blank Line Of File.?

    Answer :

    sed -n ‘/^$/p’ Test_file.txt

  • Question : How To Run Awk Command Specified In A File?

    Answer :

    awk -f filename

  • Question : Write A Command Delete All Line Except First Line?

    Answer :

    sed –n ‘1!d’ Test_file.txt

  • Question : Write A Command Remove All Empty Lines:

    Answer :

    sed ‘/^$/d’ test_file.txt

    sed ‘/./!d’ test_file.txt

  • Question : Write A Command To Print All Line Except First Line?

    Answer :

    sed –n ‘1!p’ Test_file.txt

  • Question : Write A Command To Print First And Last Line Using Sed Command?

    Answer :

    sed  -n ‘1p’ Test_file.txt

    sed –n ‘$p’ Test_file.txt