How to print all the columns after a particular number using awk?

后端 未结 11 644
一生所求
一生所求 2020-12-22 20:23

On shell, I pipe to awk when I need a particular column.

This prints column 9, for example:

... | awk \'{print $9}\'

How can I tell

相关标签:
11条回答
  • 2020-12-22 20:57
    sed -re 's,\s+, ,g' | cut -d ' ' -f 9-
    

    Instead of dealing with variable width whitespace, replace all whitespace as single space. Then use simple cut with the fields of interest.

    It doesn't use awk so isn't germane but seemed appropriate given the other answers/comments.

    0 讨论(0)
  • 2020-12-22 20:58
    awk '{print substr($0, index($0,$9))}'
    

    Edit: Note, this doesn't work if any field before the ninth contains the same value as the ninth.

    0 讨论(0)
  • 2020-12-22 20:58

    To display the first 3 fields and print the remaining fields you can use:

    awk '{s = ""; for (i=4; i<= NF; i++) s= s $i : "; print $1 $2 $3 s}' filename
    

    where $1 $2 $3 are the first 3 fields.

    0 讨论(0)
  • 2020-12-22 21:03
    function print_fields(field_num1, field_num2){
        input_line = $0
    
        j = 1;
        for (i=field_num1; i <= field_num2; i++){
            $(j++) = $(i);
    
        }
        NF = field_num2 - field_num1 + 1;
        print $0
    
        $0 = input_line
    }
    
    0 讨论(0)
  • 2020-12-22 21:11
    awk '{ s = ""; for (i = 9; i <= NF; i++) s = s $i " "; print s }'
    
    0 讨论(0)
  • 2020-12-22 21:15

    Generally perl replaces awk/sed/grep et. al., and is much more portable (as well as just being a better penknife).

    perl -lane 'print "@F[8..$#F]"'
    

    Timtowtdi applies of course.

    0 讨论(0)
提交回复
热议问题