Fastest way to print a single line in a file

前端 未结 5 902
礼貌的吻别
礼貌的吻别 2021-02-02 12:44

I have to fetch one specific line out of a big file (1500000 lines), multiple times in a loop over multiple files, I was asking my self what would be the best option

5条回答
  •  天涯浪人
    2021-02-02 13:24

    How about avoiding pipes? Both sed and head support the filename as an argument. In this way you avoid passing by cat. I didn't measure it, but head should be faster on larger files as it stops the computation after N lines (whereas sed goes through all of them, even if it doesn't print them - unless you specify the quit option as suggested above).

    Examples:

    sed -n '1{p;q}' /path/to/file
    head -n 1 /path/to/file
    

    Again, I didn't test the efficiency.

提交回复
热议问题