Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: printing certain lines from a file

  1. #1
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    printing certain lines from a file

    Say I have a text file: story.txt.

    Is there a command that allows me to print on the screen only lines 30 to 35 from this file? The only solution I can think of would be this:

    Code:
    $ cat story.txt | head -n 35 | tail -n 5
    But that isn't really pretty. Is there a better way?
    Nunca te acostarás, sin saber una cosa más.

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: printing certain lines from a file

    Code:
    cat story.txt | sed -n '30,35 p'
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: printing certain lines from a file

    Thanks!
    Nunca te acostarás, sin saber una cosa más.

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: printing certain lines from a file

    Check out BashFAQ 011 (link in my signature) for some examples. In a script I'd use ed:
    Code:
    x=30 y=35
    ed -s file << EOF
    ${x},${y}p
    EOF

  5. #5
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: printing certain lines from a file

    Thank you also. Making scripts is something I'm still learning, but I'll definitely keep that in mind too.
    Nunca te acostarás, sin saber una cosa más.

  6. #6
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: printing certain lines from a file

    Update to my own topic, lol. I did some more studying and I found that you can also do it like this:

    Code:
    cat story.txt | sed '30,35!d'
    or even shorter:

    Code:
    sed '30,35!d' story.txt
    Basically it tells sed to delete everything, except line 30 to 35.
    Nunca te acostarás, sin saber una cosa más.

  7. #7
    Join Date
    Dec 2007
    Beans
    12,521

    Re: printing certain lines from a file

    Quote Originally Posted by sha1sum View Post
    Update to my own topic, lol. I did some more studying and I found that you can also do it like this:

    Code:
    cat story.txt | sed '30,35!d'
    ...
    I had done something like that and got this. Ever since then, I've been cautious with cats

  8. #8
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: printing certain lines from a file

    Did they give you the "Useless Use of Cat Award"? lol. Tell them you gratefully accept and that you've given it an honorable place in /dev/null

    Interesting page though. Funny how those kinds of habits sneak in. Thanks for that!
    Last edited by sha1sum; December 27th, 2013 at 01:42 PM.
    Nunca te acostarás, sin saber una cosa más.

  9. #9
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: printing certain lines from a file

    New update to my own topic Now I'm studying the use of AWK, and I discovered that in awk you can do it like this:

    Code:
    awk '30<=NR && NR<=35' file.txt
    Nunca te acostarás, sin saber una cosa más.

  10. #10
    Join Date
    Oct 2013
    Beans
    97
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: printing certain lines from a file

    Every time I find a new way to do it, I'm going to update this topic. I found that with awk you can also do it like this:
    Code:
    awk 'NR==30,NR==35' file.txt
    So many possibilities
    Nunca te acostarás, sin saber una cosa más.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •