Read n lines at a time using Bash

前端 未结 15 2870
难免孤独
难免孤独 2020-11-30 00:25

I read the help read page, but still don\'t quite make sense. Don\'t know which option to use.

How can I read N lines at a time using Bash?

15条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 00:56

    With Bash≥4 you can use mapfile like so:

    while mapfile -t -n 10 ary && ((${#ary[@]})); do
        printf '%s\n' "${ary[@]}"
        printf -- '--- SNIP ---\n'
    done < file
    

    That's to read 10 lines at a time.

提交回复
热议问题