well actually
well actually

Reputation: 12370

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

Upvotes: 0

Views: 152

Answers (2)

bobbogo
bobbogo

Reputation: 15483

If your make has pcre compiled in

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

Upvotes: 0

Lev Levitsky
Lev Levitsky

Reputation: 65791

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

Upvotes: 3

Related Questions