awk + Need to print everything (all rest fields) except $1 and $2

后端 未结 7 2167
既然无缘
既然无缘 2020-12-28 19:37

I have the following file and I need to print everything except $1 and $2 by awk

File:

INFORMATION DATA 12 33          


        
相关标签:
7条回答
  • 2020-12-28 19:56

    Well, given your data, cut should be sufficient:

    cut -d\  -f3- infile
    
    0 讨论(0)
  • 2020-12-28 20:00
    $ cat t
    INFORMATION DATA 12 33 55 33 66 43
    INFORMATION DATA 45 76 44 66 77 33
    INFORMATION DATA 77 83 56 77 88 22
    
    $ awk '{for (i = 3; i <= NF; i++) printf $i " "; print ""}' t 
    12 33 55 33 66 43 
    45 76 44 66 77 33 
    77 83 56 77 88 22 
    
    0 讨论(0)
  • 2020-12-28 20:10

    Here's another awk solution, that's more flexible than the cut one and is shorter than the other awk ones. Assuming your separators are single spaces (modify the regex as necessary if they are not):

    awk --posix '{sub(/([^ ]* ){2}/, ""); print}'
    
    0 讨论(0)
  • 2020-12-28 20:11

    Although it adds an extra space at the beginning of each line compared to yael's expected output, here is a shorter and simpler awk based solution than the previously suggested ones:

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

    or even:

    awk '{$1=$2=""}1'
    
    0 讨论(0)
  • 2020-12-28 20:12

    If Perl is an option:

    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 it
    -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

    Variation for csv input files:

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

    This uses the -F field separator option with a comma

    0 讨论(0)
  • 2020-12-28 20:13

    If the first two words don't change, probably the simplest thing would be:

    awk -F 'INFORMATION DATA ' '{print $2}' t
    
    0 讨论(0)
提交回复
热议问题