I have the following file and I need to print everything except $1
and $2
by awk
File:
INFORMATION DATA 12 33
Well, given your data, cut should be sufficient:
cut -d\ -f3- infile
$ 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
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}'
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'
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
If the first two words don't change, probably the simplest thing would be:
awk -F 'INFORMATION DATA ' '{print $2}' t