0

From below 3lines I need to grep and cut jar file name only. How to grep jar name from the line and cut from there.

Downloading:https://repo.maven.apache.org/maven2/org/springframework/spring-aop/5.0.6.RELEASE/spring-aop-5.0.6.RELEASE.jar   
    Downloaded:https://repo.maven.apache.org/maven2/org/springframework/spring-aspects/5.0.6.RELEASE/spring-aspects-5.0.6.RELEASE.jar (46 KB at 12.6 KB/sec)
    Downloading:https://repo.maven.apache.org/maven2/org/springframework/security/spring-security-config/5.0.5.RELEASE/spring-security-config-5.0.5.RELEASE.jar
640/1052 KB   2580/6582 KB
2
awk -F'.jar' '/.jar/{print $1".jar"}' file |awk '{print $NF}' FS=/

The first awk display only the lines that contain ".jar", and display them until the name of the jar file

The second awk remove everything from the beginning of the line until the las occurrence of "/", and only left the name of the jar file

  • This works but cat isn't needed. The filename can simply be added at the end of the first awk statement. – Nasir Riley Dec 26 '18 at 1:49
  • ok, I will edit the answer – Emilio Galarraga Dec 26 '18 at 1:51
3

If your grep supports the -o (--only-matching) command line option, and you want to output a sequence of non-/ characters terminating in .jar, then you can use

grep -o '[^/]*\.jar\b' file
1
perl -ne 'print $1 if /([^\/]*\.jar\s)/' file

If you want your results in one line ( useful when trying to create an array of the names matching the pattern )

Or, print it in different lines

perl -ne 'print "$1\n" if /([^\/]*\.jar)/' file
0

Using standard sed:

$ sed -nE '/\.jar/{ s%.*/(.+\.jar).*%\1%p; }' file
spring-aop-5.0.6.RELEASE.jar
spring-aspects-5.0.6.RELEASE.jar
spring-security-config-5.0.5.RELEASE.jar

This finds lines that contain the string .jar and performs a substitution on them. The substitution replaces the whole line with only the filename portion of the line. It is assumed that the filename is the only thing that contains the string .jar on the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.