开发者

how to remove all lines containg less than n number of items

I want to remove all lines containing less than n number of items, space separat开发者_C百科ed.

Say I want to remove lines containing less than 3 items. So the file below:

sdf sdfsdf sdfgsdf sdfsdfsd
sdf sdfsdf 
sdf sdfsdf sdfgsdf 
sdf sdfsdf sdfgsdf  ertert

Should result in:

sdf sdfsdf sdfgsdf sdfsdfsd
sdf sdfsdf sdfgsdf 
sdf sdfsdf sdfgsdf  ertert

Actually both awk and sed solutions are acceptable.


How about this:

awk 'NF >= 3' filename


In vim:

:v/\(\S\+\s\+\)\{3,}/d

Another option is

:g/./exec len(split(getline('.'))) < 3 ? 'd' : ''

You could also do something interesting like

:py vim.current.buffer[:] = [l for l in vim.current.buffer if len(l.split()) >= 3]


Since there is a vim tag:

:v/\(\S\+\s\)\{2,}\S/d

Replace 2 with n-1.


I know you asked for a vi solution, but this is so dead simple in perl:

ethan@rover:~$ perl -ne 'print if split > 3' foo

where "foo" is your file.


NF is the number of fields in the record. Replace 2 with number you want

awk '{if (NF > 2) print $0}' inputFile.txt


~$ cat test.txt | awk '{if(length($3) > 0) print $0;}'

Hope this helps

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜