Loading ...
Sorry, an error occurred while loading the content.

Re: [blug-prog] printing lines x through y

Expand Messages
  • hariharan
    Hi, Is there any restrictions on what you can use? I have used awk here. Maybe sed will also help. Not sure though. #!/bin/bash #Pass 3 values: starting
    Message 1 of 11 , Jun 27, 2006
    • 0 Attachment
      Hi,

      Is there any restrictions on what you can use?
      I have used awk here. Maybe sed will also help. Not
      sure though.

      #!/bin/bash
      #Pass 3 values: starting lineno, and ending lineno.
      and the filename, in that order. Add error checking if
      needed.

      awk -v l1=$1 -v l2=$2 '/^.*$/ { if( NR >= l1 && NR <=
      l2 ) print; }' $3

      --hari


      __________________________________________________
      Do You Yahoo!?
      Tired of spam? Yahoo! Mail has the best spam protection around
      http://mail.yahoo.com
    • Rajiv
      ... From: sridhar.iyer To: linux-bangalore-programming@yahoogroups.com Sent: Monday, 26 June, 2006 11:16:52 PM Subject: [blug-prog]
      Message 2 of 11 , Jun 27, 2006
      • 0 Attachment
        ----- Original Message ----
        From: sridhar.iyer <iyer.sridhar.v@...>
        To: linux-bangalore-programming@yahoogroups.com
        Sent: Monday, 26 June, 2006 11:16:52 PM
        Subject: [blug-prog] printing lines x through y

        Is there a simple command to print lines from a file if the line
        numbers are given? I want to print a file from line x to line y. How
        to do it? I have been doing this by using head and tail but I dont
        find it elegant...


        [Rajiv] if you know the pattern of the strings at line x and y then the best way is to use sed


        sed 's/start-patter/end-pattern/' !d

        if not then its a bit more messy. i used a method where i did a grep . of the file to remove all the blank lines, then did a grep -n which added the line nos with the pattern "no:" and then filtered out all the lines that i didnt need.



        Regards,
        Rajiv.












        [Non-text portions of this message have been removed]
      • Philip Tellis
        ... RTFM :P -- Delta: The kids will love our inflatable slides. -- David Letterman
        Message 3 of 11 , Jun 27, 2006
        • 0 Attachment
          Sometime Today, KKI cobbled together some glyphs to say:

          >> right. pipes are just a purer way of KISSing.
          >
          > What is that?

          RTFM :P

          --
          Delta: The kids will love our inflatable slides. -- David Letterman
        • sridhar.iyer
          ... Now That is elegant... thanks. Best, Sridhar
          Message 4 of 11 , Jun 27, 2006
          • 0 Attachment
            --- In linux-bangalore-programming@yahoogroups.com, Philip Tellis
            <philip.tellis@...> wrote:
            > sed:
            > cat file | sed -ne 'x,yp'

            Now That is elegant... thanks.

            Best,
            Sridhar
          • Biju Chacko
            On Wed, 28 Jun 2006 04:03:54 +0530, sridhar.iyer ... umm ... sed -ne x,yp
            Message 5 of 11 , Jun 27, 2006
            • 0 Attachment
              On Wed, 28 Jun 2006 04:03:54 +0530, sridhar.iyer
              <iyer.sridhar.v@...> wrote:

              > --- In linux-bangalore-programming@yahoogroups.com, Philip Tellis
              > <philip.tellis@...> wrote:
              >> sed:
              >> cat file | sed -ne 'x,yp'
              >
              > Now That is elegant... thanks.

              umm ...

              sed -ne 'x,yp' << file

              -- b


              --
              Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
            • Philip Tellis
              ... surely you meant
              Message 6 of 11 , Jun 27, 2006
              • 0 Attachment
                Sometime Today, BC cobbled together some glyphs to say:

                > sed -ne 'x,yp' << file

                surely you meant < file


                Both examples would be slow on large files though, so I'd modify the
                code slightly to be like this:

                sed -ne 'x,yp;yq'

                on a 160MB file, the difference is 2s for the former and an average
                of 1.3s for the latter with a best case of 3ms and worst case the same
                as the former.



                --
                panic("aha1740.c"); /* Goodbye */
                2.2.16 /usr/src/linux/drivers/scsi/aha1740.c
              Your message has been successfully submitted and would be delivered to recipients shortly.