Awk Interview Questions with Answers
Question: 1. How To Print Only Blank Line Of File.?
Answer:
sed -n ‘/^$/p’ Test_file.Txt
Question: 2. 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
Sed (Stream Editor) Interview Questions
Question: three. Write A Command To Print All Line Except First Line?
Answer:
sed –n ‘1!P’ Test_file.Txt
Question: four. Write A Command Delete All Line Except First Line?
Answer:
sed –n ‘1!D’ Test_file.Txt
AWK Tutorial
Question: five awk ‘/^-/ if($5 ==0) print $9 ‘
Question: 6. 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: 7. 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
Sed (Stream Editor) Tutorial
Question: eight. Write A Command Remove All Empty Lines:
Answer:
sed ‘/^$/d’ test_file.Txt
sed ‘/./!D’ test_file.Txt
Question: 9. How To Run Awk Command Specified In A File?
Answer:
awk -f filename
Question: 10. 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: 11. Write A Command To Find The Sum Of Bytes (size Of File) Of All Files In A Directory.?
Answer:
ls -l sum=0 sum = sum + $5 END print sum’
Question: 12. 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
Sed (Stream Editor) Interview Questions
Question: 13. Write A Command To Print The Line Number Before Each Line?
Answer:
awk ‘print NR, $0’ filename.
Question: 14. 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: 15. Write A Command To Print Zero Byte Size Files?
Answer:
ls -l if ($5 !=0 ) print $9 ‘
Question: 16. Write A Command To Rename The Files In A Directory With “_new” As Postfix?
Answer:
ls -F print “mv “$1” “$1″.New”‘ ORS=”” for(i=NF;i>0;i–) print $i,” “; print “n”‘ filename
Question: 18. Write A Command To Find The Total Number Of Lines In A File Without Using Nr?
Answer:
awk ‘BEGIN sum=zero sum=sum+1 END print sum’ filename
