Thread: how to program in shell to get a part of a file by line number

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    how to program in shell to get a part of a file by line number

    Hello everyone,


    I am wondering how to program in shell to get a part of a file by indicated (input) line number, for example, get from line 100 to line 200 in a specific file.

    What command(s) should I use?


    thanks in advance,
    George

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    $ # first 10 lines
    $ head foo.c
    void f1 ( void **p ) {
        *p = 0;
    }
    
    void f2 ( ) {
        int *p1;
        void *p2;
        f1( &p1 );
        f1( &p2 );
    }
    
    $ # a subset of lines
    $ sed -n '7,9p' foo.c
        void *p2;
        f1( &p1 );
        f1( &p2 );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Cool, thank you Salem!


    Quote Originally Posted by Salem
    Code:
    $ # first 10 lines
    $ head foo.c
    void f1 ( void **p ) {
        *p = 0;
    }
    
    void f2 ( ) {
        int *p1;
        void *p2;
        f1( &p1 );
        f1( &p2 );
    }
    
    $ # a subset of lines
    $ sed -n '7,9p' foo.c
        void *p2;
        f1( &p1 );
        f1( &p2 );

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM