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
Question 3. Write A Command To Print All Line Except First Line?

Answer :

sed –n ‘1!p’ Test_file.txt
Question 4. Write A Command Delete All Line Except First Line?

Answer :

sed –n ‘1!d’ Test_file.txt

Question 5. How To Get Only Zero Byte Files Which Are Present In The Directory?

Answer :

ls -ltr| 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
Question 8. 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 | awk 'BEGIN {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
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 | awk '/^-/ {if ($5 !=0 ) print $9 }'

Question 16. 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 17. 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 18. 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


Post a Comment

If you have any doubts, Please let me know
Thanks!

Previous Post Next Post