Read n lines at a time using Bash

前端 未结 15 2804
难免孤独
难免孤独 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条回答
  •  执念已碎
    2020-11-30 01:03

    Simplest method - pretty self-explanatory. It is similar to the method provided by @Fmstrat, except the second read statement is before the do.

    while read first_line; read second_line
    do
        echo "$first_line" "$second_line"
    done
    

    You can use this by piping multiline input to it:

    seq 1 10 | while read first_line; read second_line 
    do
        echo "$first_line" "$second_line"
    done
    

    output:

    1 2
    3 4
    5 6
    7 8
    9 10
    

提交回复
热议问题