How to print third column to last column?

前端 未结 19 1981
面向向阳花
面向向阳花 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 18:51
    awk '{ print substr($0, index($0,$3)) }'
    

    solution found here:
    http://www.linuxquestions.org/questions/linux-newbie-8/awk-print-field-to-end-and-character-count-179078/

    0 讨论(0)
  • 2020-11-28 18:51
    awk '{a=match($0, $3); print substr($0,a)}'
    

    First you find the position of the start of the third column. With substr you will print the whole line ($0) starting at the position(in this case a) to the end of the line.

    0 讨论(0)
  • 2020-11-28 18:52

    In Bash you can use the following syntax with positional parameters:

    while read -a cols; do echo ${cols[@]:2}; done < file.txt
    

    Learn more: Handling positional parameters at Bash Hackers Wiki

    0 讨论(0)
  • 2020-11-28 18:57

    What about following line:

    awk '{$1=$2=$3=""; print}' file

    Based on @ghostdog74 suggestion. Mine should behave better when you filter lines, i.e.:

    awk '/^exim4-config/ {$1=""; print }' file
    0 讨论(0)
  • 2020-11-28 18:58
    awk '{$1=$2=""}1' FILENAME | sed 's/\s\+//g'
    

    First two columns are cleared, sed removes leading spaces.

    0 讨论(0)
  • 2020-11-28 18:59

    The following awk command prints the last N fields of each line and at the end of the line prints a new line character:

    awk '{for( i=6; i<=NF; i++ ){printf( "%s ", $i )}; printf( "\n"); }'
    

    Find below an example that lists the content of the /usr/bin directory and then holds the last 3 lines and then prints the last 4 columns of each line using awk:

    $ ls -ltr /usr/bin/ | tail -3
    -rwxr-xr-x 1 root root       14736 Jan 14  2014 bcomps
    -rwxr-xr-x 1 root root       10480 Jan 14  2014 acyclic
    -rwxr-xr-x 1 root root    35868448 May 22  2014 skype
    
    $ ls -ltr /usr/bin/ | tail -3 | awk '{for( i=6; i<=NF; i++ ){printf( "%s ", $i )}; printf( "\n"); }'
    Jan 14 2014 bcomps 
    Jan 14 2014 acyclic 
    May 22 2014 skype
    
    0 讨论(0)
提交回复
热议问题