简体   繁体   中英

How to grep all words with a specific letter in it?

I wanted to find all words in a command output that matches with some letters, so for example, all words that had the letter d or D in it.

So, just to see where the d's were, I ran (sorry, the color thing won't work here):

~$ 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

Now, can anyone explain to me why the command below doesn't work, and which one should I use?

~$ 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

As you can see I used option -o and it still brings me the whole line. I thought the \\b would limit the match until there is a space, or other special character.

When I actually put [\\b\\ ] (border or space) it really messed up.

~$ 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

So, what's going on??

EDIT: The command ls -l | grep -E -io '\\b.+d.+\\b' ls -l | grep -E -io '\\b.+d.+\\b' misses some matches like "Desktop", "Documents", "cd", and all the instances that start with d, like the month Dez (December), and the permissions column, where they are directories (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

Agree with @melpomene, you need,

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

if you would like to limit the match until there is a space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM