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.

    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)
640/1052 KB   2580/6582 KB
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

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
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

Using standard sed:

$ sed -nE '/\.jar/{ s%.*/(.+\.jar).*%\1%p; }' file

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.