displaying nth line of a file dynamically

Hi,

I have a file split.txt with the following contents

one
two
three
four
five

Suppose if i want to display contents of line 3, I know this could be achieved using the command

sed -n '3p' split.txt

But I need the line number to be decided dynamically like

a=3
sed -n '$ap' split.txt

But the above command doesnt work as sed -n '3p' split.txt. So how could I display the contents of a line whose line number is decided dynamically (or) how could I display the contents of a file using line numbers

Hi.

You need to separate the variable a from the sed modifier p.

Either of these would do it:

sed -n "$a p" split.txt
sed -n "${a}p" split.txt
1 Like

or by awk :

awk 'NR=="'$a'"' infile
awk -v s=$a 'NR==s' infile