How to print third column to last column?

前端 未结 19 1984
面向向阳花
面向向阳花 2020-11-28 18:44

I\'m trying to remove the first two columns (of which I\'m not interested in) from a DbgView log file. I can\'t seem to find an example that prints from column 3 onwards unt

相关标签:
19条回答
  • 2020-11-28 19:10

    A bit late here, but none of the above seemed to work. Try this, using printf, inserts spaces between each. I chose to not have newline at the end.

    awk '{for(i=3;i<=NF;++i) printf("%s ",  $i) }'
    
    0 讨论(0)
  • 2020-11-28 19:13

    In AWK columns are called fields, hence NF is the key

    all rows:

    awk -F '<column separator>' '{print $(NF-2)}' <filename>
    

    first row only:

    awk -F '<column separator>' 'NR<=1{print $(NF-2)}' <filename>
    
    0 讨论(0)
  • 2020-11-28 19:14
    awk '{print ""}{for(i=3;i<=NF;++i)printf $i" "}'
    
    0 讨论(0)
  • 2020-11-28 19:14
    awk '{for (i=4; i<=NF; i++)printf("%c", $i); printf("\n");}'
    

    prints records starting from the 4th field to the last field in the same order they were in the original file

    0 讨论(0)
  • 2020-11-28 19:17
    awk '{for(i=3;i<=NF;++i)print $i}' 
    
    0 讨论(0)
  • 2020-11-28 19:17
    awk -v m="\x0a" -v N="3" '{$N=m$N ;print substr($0, index($0,m)+1)}'
    

    This chops what is before the given field nr., N, and prints all the rest of the line, including field nr.N and maintaining the original spacing (it does not reformat). It doesn't mater if the string of the field appears also somewhere else in the line, which is the problem with daisaa's answer.

    Define a function:

    fromField () { 
    awk -v m="\x0a" -v N="$1" '{$N=m$N; print substr($0,index($0,m)+1)}'
    }
    

    And use it like this:

    $ echo "  bat   bi       iru   lau bost   " | fromField 3
    iru   lau bost   
    $ echo "  bat   bi       iru   lau bost   " | fromField 2
    bi       iru   lau bost 
    

    Output maintains everything, including trailing spaces

    Works well for files where '/n' is the record separator so you don't have that new-line char inside the lines. If you want to use it with other record separators then use:

    awk -v m="\x01" -v N="3" '{$N=m$N ;print substr($0, index($0,m)+1)}'
    

    for example. Works well with almost all files as long as they don't use hexadecimal char nr. 1 inside the lines.

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