0

I am working in linux bash. I have a filename which is captured by a script into this var ($filename). The filename is something like this: release-10.20.30.tar.xz

I want to be able to capture the numeric part, into a VAR. This is: 10.20.30

How can I do that?

fr0zt
  • 733
  • 4
  • 12
  • 30

1 Answers1

-1

You can use awk. Here it will be:

filename=$(echo "$filename" | awk -F "-" '{print $2}' | awk -F ".tar" '{print $1}')

here you tkae the second iteration of the char "-" and the first of ".tar". Second awk could be replace by awk -F "." '{print $1,.,$2,.,$3}'

or use it directly when you set the value of $filename

kurtrsb
  • 1
  • 1
  • Unfortunately, this has [broken quoting](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) and an inefficient and inelegant double Awk. Probably try http://shellcheck.net/ for basic diagnostics. – tripleee May 27 '22 at 13:48
  • just update for the double quote – kurtrsb May 27 '22 at 13:51
  • The double Awk is still a bit of a wart. Avoiding an external process with a parameter expansion is generally the most efficient solution, though slightly more verbose than e.g. `grep -Eo '[0-9]+(\.[0-9]+)*'`. But this has been answered many, many times before. – tripleee May 27 '22 at 13:58