如何用特定的字母grep所有单词?

时间:2017-02-13 01:10:26

标签: linux terminal grep match

我想在命令输出中找到与某些字母匹配的所有单词,例如,所有包含字母d或D的单词。

所以,只是为了看看d的位置,我跑了(对不起,颜色在这里不起作用):

~$ ls -l | grep -i 'd'
drwxrwxr-x 2 user user   4096 Fev  8 02:50 %%
drwxrwxr-x 2 user user   4096 Fev  8 02:53 cd
drwxr-xr-x 3 user user   4096 Fev  4 16:10 Desktop
drwxr-xr-x 4 user user   4096 Fev  9 23:26 Documents
drwxr-xr-x 3 user user   4096 Fev  4 23:48 Downloads
-rw-r--r-- 1 user user   8980 Dez 14 01:07 examples.desktop
-rw-rw-r-- 1 user user      5 Dez 17 01:46 hud-cli
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Music
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Pictures
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Public
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Templates
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Videos
drwxrwxr-x 2 user user   4096 Jan 28 00:18 VirtualBox VMs

现在,任何人都可以向我解释为什么下面的命令不起作用,我应该使用哪一个?

~$ ls -l | grep -E -io '\b.+d.+\b'
drwxr-xr-x 3 user user   4096 Fev  4 16:10 Desktop
drwxr-xr-x 4 user user   4096 Fev  9 23:26 Documents
drwxr-xr-x 3 user user   4096 Fev  4 23:48 Downloads
rw-r--r-- 1 user user   8980 Dez 14 01:07 examples.desktop
rw-rw-r-- 1 user user      5 Dez 17 01:46 hud-cli
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Music
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Pictures
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Public
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Templates
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Videos

正如你所看到的,我使用了选项-o,它仍然带给我整行。我认为\ b会限制匹配,直到有空格或其他特殊字符。

当我实际放置[\ b \](边框或空格)时,它真的搞砸了。

~$ ls -l | grep -E -io '[\b\ ].+d.+[\b\ ]'
 1 user user   8980 Dez 14 01:07
 1 user user      5 Dez 17 01:46
 2 user user   4096 Dez 14 07:26
 2 user user   4096 Dez 14 07:26
 2 user user   4096 Dez 14 07:26 Pub
 2 user user   4096 Dez 14 07:26
 2 user user   4096 Dez 14 07:26
 1 user user 131072 Fev  3 01:18 volume header b

那么,发生了什么?

编辑: 命令ls -l | grep -E -io '\b.+d.+\b'错过了一些匹配,如"桌面","文档"," cd"以及所有以d开头的实例,如月份Dez(十二月)和权限列,它们是目录(drwx ...)

~$ ls -l
total 204
drwxrwxr-x 2 user user   4096 Fev  8 02:50 %%
-rw-rw-r-- 1 user user   1043 Fev  9 23:06 allfiles.txt
drwxrwxr-x 2 user user   4096 Fev  8 02:53 cd
drwxr-xr-x 3 user user   4096 Fev  4 16:10 Desktop
drwxr-xr-x 4 user user   4096 Fev  9 23:26 Documents
drwxr-xr-x 3 user user   4096 Fev  4 23:48 Downloads
-rw-r--r-- 1 user user   8980 Dez 14 01:07 examples.desktop
-rw-rw-r-- 1 user user      0 Fev  9 00:22 grep
-rw-rw-r-- 1 user user      5 Dez 17 01:46 hud-cli
-rw-rw-r-- 1 user user      2 Jan 27 23:43 log.txt
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Music
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Pictures
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Public
-rw-rw-r-- 1 user user      0 Fev  3 01:02 tail
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Templates
drwxr-xr-x 2 user user   4096 Dez 14 07:26 Videos
drwxrwxr-x 2 user user   4096 Jan 28 00:18 VirtualBox VMs
-rw-rw-r-- 1 user user      6 Fev 10 00:51 xargs

~$ ls -l | grep -E -io '\b[^ ]+d[^ ]+\b'
Downloads
examples.desktop
hud-cli
Videos

1 个答案:

答案 0 :(得分:0)

同意@melpomene,你需要,

ls -l | grep -E -io '\b[^ ]+d[^ ]+\b'

如果您想限制匹配,直到有空格。