15

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
...
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
yael
  • 2,765
  • 10
  • 40
  • 48
  • possible duplicate of [Using awk to print all columns from the nth to the last](http://stackoverflow.com/questions/2961635/using-awk-to-print-all-columns-from-the-nth-to-the-last) – Andy Nov 30 '13 at 21:31

7 Answers7

22

Well, given your data, cut should be sufficient:

cut -d\  -f3- infile
Dimitre Radoulov
  • 27,252
  • 4
  • 40
  • 48
12

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'
jlliagre
  • 29,783
  • 6
  • 61
  • 72
11
$ 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 
danben
  • 80,905
  • 18
  • 123
  • 145
  • 2
    Even if for the current question there is simpler than awk (see below), if you have to do some calculation on your data then it is very useful to know your loop! One colleague of mine had to perform additions on the two first columns and then print the other ones as is: a simple printf block followed by your block did the trick. Thanks for the answer (which I plused :-) ). – Christophe Muller Sep 16 '11 at 14:10
3

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
7winkie
  • 1,431
  • 1
  • 9
  • 3
2

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

awk -F 'INFORMATION DATA ' '{print $2}' t
Mike
  • 189
  • 1
  • 5
0

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}'
eddi
  • 49,088
  • 6
  • 104
  • 155
0

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

Chris Koknat
  • 3,305
  • 2
  • 29
  • 30