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

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • 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

    You are viewing a single comment's thread. Return to all comments →

  • adarsh1001
    6 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
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy