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?

  • 3
    sed '/Page # 2/,/Page # 3/!d' < FileName.txt – Stéphane Chazelas Aug 13 '13 at 7:22
  • @StephaneChazelas Please add this as an answer...It worked perfectly – S.M.09 Aug 13 '13 at 8:42
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
  • 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 6: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.