Bash tool to get nth line from a file

前端 未结 19 2381
刺人心
刺人心 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 09:00

    Wow, all the possibilities!

    Try this:

    sed -n "${lineNum}p" $file
    

    or one of these depending upon your version of Awk:

    awk  -vlineNum=$lineNum 'NR == lineNum {print $0}' $file
    awk -v lineNum=4 '{if (NR == lineNum) {print $0}}' $file
    awk '{if (NR == lineNum) {print $0}}' lineNum=$lineNum $file
    

    (You may have to try the nawk or gawk command).

    Is there a tool that only does the print that particular line? Not one of the standard tools. However, sed is probably the closest and simplest to use.

提交回复
热议问题