We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Linux Shell
  3. Text Processing
  4. Middle of a Text File
  5. Discussions

Middle of a Text File

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 81 Discussions, By:

votes

Please Login in order to post a comment

  • sirekanyan
    6 years ago+ 3 comments

    Or use + prefix to start counting from the begining:

    head -n 22 | tail -n +12
    
    37|
    Permalink
  • adarsh1001
    4 years ago+ 1 comment

    I could come up with 3 solutions for this. Since most of us are in a learning phase, this may help in broadening the understanding of the language-

    1. Remember "cut" for previous questions? You can change the delimiter to '\n', meaning that each field would now correspond to a line.
    cut -d$'\n' -f12-22
    
    1. Just like head, tail command exists which can print the last n lines of a file. So just pass the first 22 lines from head to tail via a pipe.
    head -n 22 | tail -n 11
    
    1. I actually came up with this solution first since I didn't know about tail. Basically, take the first 22 lines (from head) and pass then to a counter which will ignore the first 11 lines and start printing from the 12th.
    count=1
    head -n 22 | while read line; do if [ $count -ge 12 ]; then echo $line; fi; count=$(($count+1)); done
    

    Hope it helps!

    24|
    Permalink
  • thatgeeman
    6 years ago+ 2 comments
    head -22 | tail -11
    

    I think this is the simplest way to do it.

    23|
    Permalink
  • senooken
    5 years ago+ 1 comment
    sed -n '12,22p'
    
    19|
    Permalink
  • rotiroti
    7 years ago+ 2 comments

    I found a simple solution using the sed command but I don't know if I can use it to solve this problem. Other approaches to find a solution consist in use both commands, head and tail, passing the output of the first command as input of the second command.

    3|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature