开发者

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

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

File:

INFORMATION DATA 12 33 55 33 66 43 
INFORMATION DATA 45 76 44 66 77 33 
INFORMATION DATA 77 83 56 77 88 22
...

the desirable output:

 12 33 55 33 66 43 
 45 76 44 66开发者_运维技巧 77 33 
 77 83 56 77 88 22
...


Well, given your data, cut should be sufficient:

cut -d\  -f3- infile


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'


$ 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 


danbens answer leaves a whitespace at the end of the resulting string. so the correct way to do it would be:

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


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

awk -F 'INFORMATION DATA ' '{print $2}' t


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}'


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜