I'm creating a little script and I have to list all my Tomcat binaries.
So far I was able to do this:ls -1 | grep '\-tomcat\-' | cut -f3 -d'-'
This basically lists all the versions, but it adds the .zip or .tar.gz
5.5.17.zip
5.5.26.tar.gz
5.5.27.tar.gz
5.5.28.tar.gz
5.5.31.tar.gz
5.5.32.tar.gz
I would like to know how to remove the .zip and .tar.gz from the extracted strings.
Using the cut Command Specifying the character index isn't the only way to extract a substring. You can also use the -d and -f flags to extract a string by specifying characters to split on. The -d flag lets you specify the delimiter to split on while -f lets you choose which substring of the split to choose.
If "name=foo" is always in the same position in the line you could simply use: awk '{print($3)}' , that will extract the third word in your line which is name=foo in your case. He told "also other parameters may or may not be there" hence you may not use a positional logic.
Or simplify the whole approach:
ls apache-tomcat*|sed -r 's/^.*-([0-9.]+)\..*/\1/'
Less tools, and it gives you the version numbers.
P.S.: Following up on @Nemo's suggestion: we let shell globbing and prior knowledge take care of half the job (just list things that actually look like apache-tomcat). When piping ls' output to another tool the -1 is moot, so we get rid of that. sed takes the values coming from ls, matches beginning of line to the first - followed by a digit, the parenthesis remember all digits & literal periods, and then we match the rest of the string till end of line (implicit). And then the whole match gets replaced with the remembered digits & periods.
Pipe it through another cut
:
ls -1 | grep '-tomcat-' | cut -f3 -d'-' | cut -f1-3 -d'.'
This will work as long as the versions all have three components. If the version is just 5.5, it won't work.
Another option would be just to use sed
:
ls -1 | grep '-tomcat-' | cut -f3 -d'-' | sed 's/.tar.gz\|.zip//'
This will remove .tar.gz
or .zip
from the strings.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With