How to print third column to last column?

前端 未结 19 2086
面向向阳花
面向向阳花 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: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.

提交回复
热议问题