Get specific line from text file using just shell script

前端 未结 10 2159
清酒与你
清酒与你 2020-12-22 16:59

I am trying to get a specific line from a text file.

So far, online I have only seen stuff like sed, (I can only use the sh -not bash or sed or anything like that).

10条回答
  •  情歌与酒
    2020-12-22 17:37

    Assuming line is a variable which holds your required line number, if you can use head and tail, then it is quite simple:

    head -n $line file | tail -1
    

    If not, this should work:

    x=0
    want=5
    cat lines | while read line; do
      x=$(( x+1 ))
      if [ $x -eq "$want" ]; then
        echo $line
        break
      fi
    done
    

提交回复
热议问题