How to delete first 10 words from file

Hi,

Could you please let me know, how to delete first 10 words from text files using vi?

10dw will delete it from current line, how to do it for all the lines from file?

Thanks

Use Perl:

perl -i~ -0pe's/(\w+\s+)/++$c<11?"":$1/ge' infile

With vi (in visual mode):

d10W

> cat file02
1 2 3 4 5 6 7 8 9 10 11 12
a b c dd eee f g hhh i jjjj kk lllll mm nn oo ppppp
aaaa b ccc d ee ff gg hhh i j kkkk ll mm nnnn o p qq
> cut -d" " -f11- file02
11 12
kk lllll mm nn oo ppppp
kkkk ll mm nnnn o p qq

If your words are separated by arbitrary numbers of white-space characters or the lines can begin with spaces, you could use

perl -n -e 'split; print "@_[10..$#_]\n"' file

although this does not preserve whitespace between the remaining words that get printed.

vi has no way of doing what you want to do using a standard builtin command. However you can call an external utility such as awk from within vi using the ":" command as shown in the following example

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

Note the space between the "$" and "!" and the '\" in front of "%s" in the awk printf statement. Without the slash the word "files" is outputted instead of the expected field text.

Suppose we have the following file:

1 2 3 4 5 6 7 8 9 10 11 12 13 line1
line2

a b c d e f g h i j k l m n line4

the following is the file contents after executing the above command

11 12 13 line1 


k l m n line4 

Just curious: isn't my second solution working for you?

P.S. I've just corrected it: 10 instead of 11.

Reading the vi help pages: perhaps d10E is even more correct.

this will not work in stock vi, but in vim you could say this:

:%s/\(\<\w\+\>\s\+\)\{10}//

to remove first 10 words throughout the whole file