Get specific line from text file using just shell script Get specific line from text file using just shell script unix unix

Get specific line from text file using just shell script


sed:

sed '5!d' file

awk:

awk 'NR==5' file


Assuming line is a variable which holds your required line number, if you can use head and tail, then it is quite simple:

head -n $line file | tail -1

If not, this should work:

x=0want=5cat lines | while read line; do  x=$(( x+1 ))  if [ $x -eq "$want" ]; then    echo $line    break  fidone


You could use sed -n 5p file.

You can also get a range, e.g., sed -n 5,10p file.