How to print third column to last column?

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

    Jonathan Feinberg's answer prints each field on a separate line. You could use printf to rebuild the record for output on the same line, but you can also just move the fields a jump to the left.

    awk '{for (i=1; i<=NF-2; i++) $i = $(i+2); NF-=2; print}' logfile
    
    0 讨论(0)
  • 2020-11-28 19:01

    If you want to print the columns after the 3rd for example in the same line, you can use:

    awk '{for(i=3; i<=NF; ++i) printf "%s ", $i; print ""}'
    

    For example:

    Mar 09:39 20180301_123131.jpg
    Mar 13:28 20180301_124304.jpg
    Mar 13:35 20180301_124358.jpg
    Feb 09:45 Cisco_WebEx_Add-On.dmg
    Feb 12:49 Docker.dmg
    Feb 09:04 Grammarly.dmg
    Feb 09:20 Payslip 10459 %2828-02-2018%29.pdf
    

    It will print:

    20180301_123131.jpg
    20180301_124304.jpg
    20180301_124358.jpg
    Cisco_WebEx_Add-On.dmg
    Docker.dmg
    Grammarly.dmg
    Payslip 10459 %2828-02-2018%29.pdf
    

    As we can see, the payslip even with space, shows in the correct line.

    0 讨论(0)
  • 2020-11-28 19:03

    Perl solution:

    perl -lane 'splice @F,0,2; print join " ",@F' file
    

    These command-line options are used:

    • -n loop around every line of the input file, do not automatically print every line

    • -l removes newlines before processing, and adds them back in afterwards

    • -a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace

    • -e execute the perl code

    splice @F,0,2 cleanly removes columns 0 and 1 from the @F array

    join " ",@F joins the elements of the @F array, using a space in-between each element

    If your input file is comma-delimited, rather than space-delimited, use -F, -lane


    Python solution:

    python -c "import sys;[sys.stdout.write(' '.join(line.split()[2:]) + '\n') for line in sys.stdin]" < file

    0 讨论(0)
  • 2020-11-28 19:06

    ...or a simpler solution: cut -f 3- INPUTFILE just add the correct delimiter (-d) and you got the same effect.

    0 讨论(0)
  • 2020-11-28 19:09

    Well, you can easily accomplish the same effect using a regular expression. Assuming the separator is a space, it would look like:

    awk '{ sub(/[^ ]+ +[^ ]+ +/, ""); print }'
    
    0 讨论(0)
  • 2020-11-28 19:10
    awk '{$1=$2=$3=""}1' file
    

    NB: this method will leave "blanks" in 1,2,3 fields but not a problem if you just want to look at output.

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