5

I want to print a text file from X line number to Y line number

The value of X & Y is determined by searching for a specific line in the text file(in this case the page number)

I search for the line by the following command

echo -n |grep -nr "Page # 2" FileName.txt |cut -f1 -d: ; echo -n |grep -nr "Page # 3" FileName.txt |cut -f1 -d:

for which I get an out put

67

128

I want to feed this output to the command below

sed -n 'X,Yp' FileName.txt

But I get them as two different lines how can i feed it to the sed command

There after I want to feed the result of the above command to the lp command something like this...

sed -n 'X,Yp' FileName.txt | lp -dmyprinter

Can this be done without creating a file?

S.M.09
  • 153
  • 1
  • 5

2 Answers2

5

No need for the 2 phases to find out the lines beforehand. Just do the whole thing with sed:

sed '/Page # 2/,/Page # 3/!d' < FileName.txt
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • Just a slight modification...What if I want start it three lines before the point where i find `Page # 2` string in the file...is it possible to get the three lines above the `Page # 2` as well....Unfortunately the string in the line above can be fixed so cannot ad as a search criteria... – S.M.09 Sep 20 '13 at 06:47
0

Try to keep the values with variables, something like this:

FROM_LINE=`echo -n |grep -nr "Page # 2" FileName.txt |cut -f1 -d: `
TO_LINE=`echo -n |grep -nr "Page # 3" FileName.txt |cut -f1 -d:`

and then use these variables with sed:

sed -en "${FROM_LINE},${TO_LINE}p" FILE
dsmsk80
  • 3,038
  • 1
  • 15
  • 20