当我运行此命令时ls -l Documents/phone_photo_vids
,将获得以下格式的100个条目。请注意,图像的结尾是PNG或JPG
-rw-r--r-- 1 moi staff 189280 Oct 29 2011 IMG_0041.PNG
-rw-r--r-- 1 moi staff 2481306 Oct 29 2011 IMG_0042.JPG
然后,我决定只想查看jpg结果,所以我运行了这两个命令,但都没有返回结果
ls -l Documents/phone_photo_vids | grep "*.JPG"
ls -l Documents/phone_photo_vids | grep "*.JPG$"
我本来希望这两个grep命令能够过滤出所有以PNG结尾的文件,并返回所有以JPG结尾的文件,但是我什么也没得到。我如何错误使用grep?
我正在使用Mac OSX 10.9.3
某种形式的答案是错误的,尽管它在大多数情况下都像声称的那样工作。
grep ".jpg" #match string "jpg" anywhere in the filename with any character in front of it.
# jpg -- not match
# .jpg -- match
# mjpgsfdfd -- match
grep ".*.jpg" #basically the same thing as above
grep ".jpg$" #match anything that have at least 4 chars and end with "jpg"
# i_am_not_a_.dummy_jpg -- match
grep ".*.jpg$" #the same as above (basically)
因此,要获得最佳结果,请尝试以下方法:
grep "[.]jpg$" #anything that end with ".jpg"
grep "\\.jpg$" #the same as above, use escape sequence instead
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句