HOW TO PRINT Nth LINE FROM A FILE IN SHELL SCRIPT
I was asked this question in an interview for testing pattern matching skills in shell script. Though, the question looked easy but still it was really difficult to think of logic immediately and it took some time. Also the difficult question was “How to filter the 10 row without using head or tail”
Create a sample file, populate with numbers and cat the contents
[oracle@hydrupgrd ~]$ touch abc.txt
[oracle@hydrupgrd ~]$ echo -ne "1\n2\n3\n4\n5\n6\n" > abc.txt
[oracle@hydrupgrd ~]$ cat abc.txt
1
2
3
4
5
6
head and tail are the easiest combination to filter the Nth row in the file
[oracle@hydrupgrd ~]$ cat abc.txt|head -3|tail -1
3
Another method is to use AWK to filter the exact line
[oracle@hydrupgrd ~]$ cat abc.txt |awk 'NR==3'
3
SED can also be used to filter complex logics.
[oracle@hydrupgrd ~]$ cat abc.txt |sed -n '3p'
3