codecrazy46 February 2016

Using variables in sed -n x,yp to print lines from line number x to line number y

I have a file "testfile" with 10 lines. I want to print lines 3 (lower) to 6(upper) out of these lines. So I use cat testfile | sed -n 3,6p to print the lines. Now if I calculate the upper limit to be displayed based on some calculation and the result is saved in a variable say "y". Assume y=6, how can I use the same sed command now??

sed -n 3,$yp doesn't work as yp is considered like a variable here. How do I distinguish between $y and p here.

Answers


user unknown February 2016

With curly braces:

sed -n 3,${y}p FILE

and the useless use of cat can be avoided, too. :)

Post Status

Asked in February 2016
Viewed 2,362 times
Voted 5
Answered 1 times

Search




Leave an answer