Bash tool to get nth line from a file

前端 未结 19 2363
刺人心
刺人心 2020-11-22 08:07

Is there a \"canonical\" way of doing that? I\'ve been using head -n | tail -1 which does the trick, but I\'ve been wondering if there\'s a Bash tool that speci

19条回答
  •  自闭症患者
    2020-11-22 08:54

    The fastest solution for big files is always tail|head, provided that the two distances:

    • from the start of the file to the starting line. Lets call it S
    • the distance from the last line to the end of the file. Be it E

    are known. Then, we could use this:

    mycount="$E"; (( E > S )) && mycount="+$S"
    howmany="$(( endline - startline + 1 ))"
    tail -n "$mycount"| head -n "$howmany"
    

    howmany is just the count of lines required.

    Some more detail in https://unix.stackexchange.com/a/216614/79743

提交回复
热议问题