Skip to content
Advertisement

bash extract version string & convert to version dot

I want to extract version string (1_4_5) from my-app-1_4_5.img and then convert into dot version (1.4.5) without filename. Version string will have three (1_4_5) or four (1_4_5_7) segments.

Have this one liner working ls my-app-1_4_5.img | cut -d'-' -f 3 | cut -d'.' -f 1 | tr _ .

Would like to know if there is any better way rather than piping output from cut.

Advertisement

Answer

Remove everything but 0 to 9, _ and newline and then replace all _ with .:

echo "my-app-1_4_5.img" | tr -cd '0-9_n' | tr '_' '.'

Output:

1.4.5
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement