简体   繁体   中英

Grep out all file extensions from input

My input is a large list of files. They could have any characters in the name (including periods, because there are some package names as well). Here's some small sample input:

com.test.impl.servlets.Test.xml
TestClass.class
TestClass1.class
Test2.java
Test3.java

I want to know all of the different file extensions in my list, so essentially, I want egrep -o everything after the last period. Something like this:

input | xargs <unknown command but probably egrep> | sort -u

Would return:

.xml
.class
.java

You can try grep -o '\\.[^.]*$' :

$ echo 'com.test.impl.servlets.Test.xml
TestClass.class
TestClass1.class
Test2.java
Test3.java' | grep -o '\.[^.]*$' | sort -u
.class
.java
.xml

or sed 's/.*\\././' :

$ echo 'com.test.impl.servlets.Test.xml
TestClass.class
TestClass1.class
Test2.java
Test3.java' | sed 's/.*\././' | sort -u
.class
.java
.xml

如果您的制造商已经编译了pcre

$ grep -P -o '.*\.\K.*'

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