提取没有扩展名的文件基名

时间:2014-10-20 02:58:25

标签: linux shell unix

我正在尝试使用pdf命令在我的目录中递归列出所有find个文件:

find -name "*.pdf" > all_pdf.txt

输出正确,如图所示(all_pdf.txt):

./C++/data structures , algorithms and applications in c++ by sartraj sahani.pdf
./C++/C++Complete Reference (3rd Ed.).pdf
./C++/Bjarne Stroustrup - The C++ Programming Language(Third Edition).pdf
./C++/Essential_C++.pdf
./C++/Stawww.free-ebooks-cloud.com --nley B. Lippman - C++ Primer, Fourth Edition-signed.pdf
./C++/c_primer_5th_edition.pdf
./C++/C++ for Dummies 5th Ed.pdf
./C++/C++ Programming for the Absolute Beginner, Second Edition.pdf
./C++/c++_by_balaguruswamy/balagurusami_objectoriented_programming_cPP.pdf
./C++/c++_by_balaguruswamy/alagurusami_objectoriented_programming_cCh8to10__(1).pdf
./C++/c++_by_balaguruswamy/balagurusami_objectoriented_programming_cCh1to5__.pdf
./C++/c++_by_balaguruswamy/balagurusami_objectoriented_programming_cCh5to7__.pdf
./C++/c++_by_balaguruswamy/lagurusami_objectoriented_programming_cCh11to17__.pdf
./JQUERY/jQuery UI.pdf
./JQUERY/prototype-151-api.pdf
./JQUERY/jquery_succinctly.pdf
./JQUERY/learning_jquery_3rd_edition.pdf
./JQUERY/pro_php_and_jquery.pdf
./JQUERY/Learning jQuery, 4th Edition.pdf
./JQUERY/codeschool_try_jquery.pdf
./JQUERY/jQuery Game Development Essentials.pdf
./JQUERY/Manning.jQuery.in.Action.2nd.Edition.Jun.2010.pdf
./JQUERY/cdn.bitbucket.org-JQuery_Documentation.pdf
./JQUERY/Professional jQuery.pdf

但我的要求是仅列出所有文件的basename,而不包含以下内容:

c_primer_5th_edition.pdf
C++Complete Reference (3rd Ed.).pdf
Bjarne Stroustrup - The C++ Programming Language(Third Edition).pdf
...

如何通过提供filename(在本例中为'all_pdf.txt')作为basename命令的参数来实现这一点。

1 个答案:

答案 0 :(得分:2)

使用sed

要从all_pdf.txt中的每一行删除目录名称,请使用:

$ sed -e 's|.*/||' all_pdf.txt 
data structures , algorithms and applications in c++ by sartraj sahani.pdf
C++Complete Reference (3rd Ed.).pdf
Bjarne Stroustrup - The C++ Programming Language(Third Edition).pdf
Essential_C++.pdf
[...snip...]

如果另外要删除文件扩展名,请使用:

$ sed -e 's|.*/||' -e 's|\.pdf$||' all_pdf.txt 
data structures , algorithms and applications in c++ by sartraj sahani
C++Complete Reference (3rd Ed.)
Bjarne Stroustrup - The C++ Programming Language(Third Edition)
Essential_C++
[...snip...]

使用grep

$ grep -o '[^/]*$' all_pdf.txt 
data structures , algorithms and applications in c++ by sartraj sahani.pdf
C++Complete Reference (3rd Ed.).pdf
Bjarne Stroustrup - The C++ Programming Language(Third Edition).pdf
Essential_C++.pdf
[...snip...]

同时删除.pdf扩展程序:

$ grep -Po '[^/]*(?=.pdf$)' all_pdf.txt 
data structures , algorithms and applications in c++ by sartraj sahani
C++Complete Reference (3rd Ed.)
Bjarne Stroustrup - The C++ Programming Language(Third Edition)
Essential_C++
[...snip...]

使用awk

$ awk '{sub(/.*\//, "")} 1' all_pdf.txt 
data structures , algorithms and applications in c++ by sartraj sahani.pdf
C++Complete Reference (3rd Ed.).pdf
Bjarne Stroustrup - The C++ Programming Language(Third Edition).pdf
Essential_C++.pdf
[...snip...]

同时删除.pdf扩展程序:

$ awk '{sub(/.*\//, ""); sub(/\.pdf$/, "")} 1' all_pdf.txt 
data structures , algorithms and applications in c++ by sartraj sahani
C++Complete Reference (3rd Ed.)
Bjarne Stroustrup - The C++ Programming Language(Third Edition)
Essential_C++
[...snip...]