1

I have a file with a couple of strings in it. Like :

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

I want the appname and the version from these strings in single variables, so I can output an csv file from my script In this case :

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

The version is always the substring with the dots in it, with a - before it.

My idea was to get the name and version in a separate variable, and ultimately merge it into a new variable with an extra comma

string = $appname","$appversion

I’ve tried to use cut with the - delimiter, but that doesn’t work with example 5. Using grep -Eo “[a-zA-Z_]+” doesn’t work either, missing the 2020 in example 2.

Would it be a possibility to find all songs and points from the end of the line to the first -?

zeel
  • 27
  • 5

3 Answers3

2

With bash:

while read -r line; do
  if [[ "$line" =~ (.*)-([^-]+)$ ]]; then
    string="${BASH_REMATCH[1]},${BASH_REMATCH[2]}"
    echo "$string"
  fi
done < file

Output:

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

See: The Stack Overflow Regular Expressions FAQ

Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

In plain bash:

#!/bin/bash

while read -r line; do
    printf '%s,%s\n' "${line%-*}" "${line##*-}"
done < file

Or, using a sed one-liner:

sed 's/\(.*\)-/\1,/' file
M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
  • I’m trying the sed one-liner in a script, but it doesn’t work with the last line because of the - in ‘app-name’. So I’ve changed it to ```sed 's/\(.*\)-[0-9]/\1,/'``` But then I’m missing the first ‘2’ in the version. Any ideas how to change the sed command? – zeel Dec 28 '21 at 19:38
  • @zeel The version in the answer should work with the last line in the sample input. `sed 's/\(.*\)-/\1,/' <<< 'app-name-2021.11.10'` should print out `app-name,2021.11.10`. – M. Nejat Aydin Dec 28 '21 at 19:52
  • I’m sorry, you’re right. Messed up some things. Do you have any idea how the sed command has to be changed if the line is ```appname-2021.12.24-SNAPSHOT``` – zeel Dec 28 '21 at 20:41
  • 1
    @zeel `sed 's/\(.*\)-\([0-9].*\)/\1,\2/'` – M. Nejat Aydin Dec 28 '21 at 20:48
  • It seems that someone can always come up with an exception. It appears that there are now applications that have the following name: ```app_name-22.20.0-beta-1-10```. Unfortunately, this does not work with the command, ```app_name-22.20.0-beta-1,10``` is the result. Maybe I have to look for something that checks from the end of the line up to and including the last figure for '-' as the version. – zeel Dec 29 '21 at 12:36
  • @zeel `sed 's/\(.*\)-\([0-9][0-9]*\.[0-9].*\)/\1,\2/' file`. This assumes the version begins with `M.m`, where `M` and `m` consist of decimal digits. – M. Nejat Aydin Dec 29 '21 at 14:15
1
$ sed 's/-\([^-]*\)$/,\1/' file
appname,2021.12.24
appname2020,2021.12.23
app_name,2021.12.01
app_name2020,2021.12.02
app-name,2021.11.10
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • That does not work, see the last line. – zeel Dec 28 '21 at 18:37
  • 1
    Fixed to handle that case. – Ed Morton Dec 28 '21 at 20:47
  • Thank you, it works indeed. Any idea if I can use a sed command to fix such a case? ```app_name-22.20.0-beta-1-10```. I think it’s best to start at the end of the line and grep all numbers/characters to the last ```-``` as the version? – zeel Dec 29 '21 at 12:54
  • You're welcome. [Chameleon Questions](https://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) are strongly discouraged. You should ask a followup question that includes that and any other input formats you didn't include in this question and describe how to identify the separate parts you want and show the expected output. – Ed Morton Dec 29 '21 at 13:08
  • Excuse me, I came across it when performing the script on other systems than my development environment. – zeel Dec 29 '21 at 13:19
  • It's not a problem, the way to handle such a situation is just ask a new question about it. – Ed Morton Dec 29 '21 at 13:20
  • 1
    Opened a follow up question : [link] https://stackoverflow.com/questions/70520201/splitting-strings-for-appname-and-version-follow-up Thanks for the tip. – zeel Dec 29 '21 at 13:37