0

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

zeel
  • 27
  • 5
  • 1
    Thanks for showing your efforts, could you please do mention(by editing your question) samples of input and expected output more clearly so that we can get clear picture of question, thank you. – RavinderSingh13 Dec 29 '21 at 13:40
  • 1
    Please just post 1 sample input file and the 1 associated expected output, don't spread it across multiple files so we don't have to do multiple copy/paste to test with it. – Ed Morton Dec 29 '21 at 13:40
  • 1
    Where does the input come from? – KamilCuk Dec 29 '21 at 14:42

1 Answers1

3

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
glenn jackman
  • 238,783
  • 38
  • 220
  • 352