Home > OS >  Splitting strings for appname and version (follow up)
Splitting strings for appname and version (follow up)

Time:12-30

Because in my previous post it was indicated that I better open a new question with more information, I post this question.

I started asking how I could split the list below so that I have the name and version as a CSV.

appname-2021.12.24
appname2020-2021.12.23
app_name-2021.12.01
app_name2020-2021.12.02
app-name-2021.11.10
appname-2021.12.24-SNAPSHOT

With the help of the answers I have come out on the command: sed 's/-\([^-]*\)$/,\1/' or sed 's/\(.*\)-\([0-9].*\)/\1,\2/'

However, it turned out that there were other possibilities in the list:

app_name-10.10.0-beta-1-10
appname-1.10.10-r2021-12

both resulting in the last number after the comma

app_name-10.10.0-beta-1,10
appname-1.10.10-r2021,12

Could I adjust the command that it would work for all possibilities? Maybe to start at the end of the file and select everything (starting at the first number) after the last - (seen from the end of the string). So:

appname,1.10.10-r2021-12

I’ve tried it with adding a extra ^ in the command like: sed 's/\(.*\)-\([^0-9].*\)/\1,\2/' that changed the output a bit, but not to what I want

CodePudding user response:

To change the first hyphen (which is followed by a digit) into a comma, you can use

sed -E 's/-([[:digit:]])/,\1/'

which transforms your inputs into

appname,2021.12.24
appname2020,2021.12.23
app_name,2021.12.01
app_name2020,2021.12.02
app-name,2021.11.10
appname,2021.12.24-SNAPSHOT
app_name,10.10.0-beta-1-10
appname,1.10.10-r2021-12
  • Related