2012-01-26 44 views
2

目錄中的所有文件/usr/bin文件名包含小寫英文字母 字符只,也包含單詞文件爲(連續)子。在目錄列表中的文件用grep

例如,文件型材是這樣的文件,但GIT-LS-文件不是。

這是我的確切問題,我只能使用grep,ls,catwc

ls /usr/bin/ | grep '[^-]*file' 

這就是我到目前爲止,輸出低於。我不知道如何顯示file,因爲* is zero or more occurences。而且不知道如何把小寫的事情在正則表達式以及..

check-binary-files 
clean-binary-files 
desktop-file-install 
desktop-file-validate 
ecryptfs-rewrite-file 
file 
filep 
git-cat-file 
git-diff-files 
git-ls-files 
git-merge-file 
git-merge-one-file 
git-unpack-file 
lockfile 
nsrfile 
pamfile 
pcprofiledump 
pnmfile 
ppufiles 
profiles 
+0

爲什麼選擇投票? OP已將其列爲家庭作業,已經說明了給定預期結果的要求並顯示了他所嘗試的內容。 –

回答

1
ls /usr/bin/ | grep --regex '^[[:lower:]]*file[[:lower:]]*$' 

^和$分別匹配字符串的開頭和結尾。

0
ls -1 /usr/bin | grep '^[a-z]*file[a-z]*$' 

ls -1確保文件在一行中列出。 ^$是啓動和線路,結束標誌是你錯過了什麼(否則它可以匹配文件名中的字符串)

0

使用-P選項,意味着Perl的正則表達式,那麼最後你的命令將看起來像:

ls /usr/bin | grep -P '[a-z]file' 

[email protected] ~/test/bin$ ls 
123file    desktop-file-install file   git-diff-files git-merge-one-file nsrfile  pnmfile testlist 
check-binary-files desktop-file-validate filep   git-ls-files git-unpack-file  pamfile  ppufiles 
clean-binary-files ecryptfs-rewrite-file git-cat-file git-merge-file lockfile   pcprofiledump profiles 
[email protected] ~/test/bin$ ls . | grep -P '[a-z]file' 
lockfile 
nsrfile 
pamfile 
pcprofiledump 
pnmfile 
ppufiles 
profiles 
1

使用ls管道與grep是在那種情況下真是多餘。您可以使用find

$> find /usr/bin -regex "/usr/bin/[a-z]*file[a-z]*" -type f -printf "%f\n" 
profiles 
keditfiletype 
inifile 
dotlockfile 
pamfile 
pnmfile 
file 
konsoleprofile 
+0

問題說「我只能使用grep ls cat wc」,但如果這些約束不存在,這絕對是一種方法。 – nmichaels