LeetCode195 solution

Printing out Tenth Line

The original problem is https://leetcode.com/problems/tenth-line/.

This is a basic application of bash: just get the 10th line from a file. Many methods can solve this simple question.

Solution: using bash control structures, conditional tests

 #!/bin/bash

 let count=0

 while read line || [[ -n "$line" ]]
 do
     name=$line
     ((count++))
     # echo $count, $line, $name
     if [ "$count" -eq "10" ]
     then
         echo $name
     fi
 done<file.txt

Solution: using sed

  sed -n '10p' file.txt

Solution: using awk

  awk 'NR==10' file.txt