bash command to print column at specific range of line numbers

前端 未结 3 1609
独厮守ぢ
独厮守ぢ 2020-12-22 02:20

I\'m trying to get the values in column X at lines 5 to 5 + Y. I\'m guessing there\'s a quick way to do this with awk. How is this done?

3条回答
  •  伪装坚强ぢ
    2020-12-22 03:11

    Use awk to print column 2 of lines 5 to 10:

    awk 'NR==5,NR==10 {print $2}' 

    The optimization is that it exits after the last line of the desired range has been printed.

    A range pattern was used:

    A range pattern is made of two patterns separated by a comma, in the form ‘begpat, endpat’. It is used to match ranges of consecutive input records.
    https://www.gnu.org/software/gawk/manual/html_node/Ranges.html

    A pattern can be either a regexp pattern or an expression pattern. Above uses expression patterns to do comparisons with NR.

    I assumed white space delimited columns, but provided an example of specifying a different delimiter with the -F option.

提交回复
热议问题