0

I've got strings "s=c=g++", where s, c ... (can be multiple values) mean extensions and g++ or any other value means a compiler. I've written 2 regex to parse compiler and extensions in given string:

compiler_pattern=".+(\w.+)$"
extensions_pattern="(\w+)?(?=\=)"

they work fine in python, but now I'm trying to use them in bash and I get this error

sed: -e expression #1, char 1: unknown command: `.'

How can it be fixed?

#!/bin/bash

archive=''
source=''
declare -a array=()
declare -A dictionary=()

while [ $# -gt 0 ]; do
  case "$1" in
    -a | --archive)
        archive=$2
    shift 2
        ;;
    -s | --source)
        source=$2
    shift 2
        ;;
    -c | --compiler)
        array+=($2)
        shift 2
        ;;
    *)
        shift 1 
    ;;
  esac
done

compiler_pattern=".+(\w.+)$"
extensions_pattern="(\w+)?(?=\=)"

for item in "${array[@]}"
do
  compiler=$(echo $item | sed $compiler_pattern)
  echo $compiler
done


about regex:

given "s=c=g++" -> compiler = g++, extensions = [s, c]

given "ipynb=py" -> compiler = py, extensions = ipynb

Cyrus
  • 84,225
  • 14
  • 89
  • 153
John Doe
  • 37
  • 4
  • `sed` don't know `PCRE` like regex: `\d \w \s ...` – Gilles Quénot Feb 28 '23 at 21:37
  • And the input is not clear, please clarify by editing your original post – Gilles Quénot Feb 28 '23 at 21:38
  • @GillesQuénot, I've added my full script – John Doe Feb 28 '23 at 21:40
  • 1
    You're using PCRE regex syntax. You need to stick to BRE and/or ERE. There's no `\w` in POSIX regular expressions, there's no `(?...)`, etc. – Charles Duffy Feb 28 '23 at 21:41
  • Better, but we still don't have the sample input – Gilles Quénot Feb 28 '23 at 21:42
  • 1
    A good starting point is https://en.wikibooks.org/wiki/Regular_Expressions/POSIX_Basic_Regular_Expressions – Charles Duffy Feb 28 '23 at 21:42
  • Also, `echo $item` needs to be `echo "$item"` if you want to ensure your items don't get munged before `sed` even sees them. This is a category of problem that http://shellcheck.net/ will catch. – Charles Duffy Feb 28 '23 at 21:43
  • 1
    (See the "character classes" section of the BRE reference above with respect to finding alternatives to things like `\w`; in that case, I assume you might want `[[:word:]]`, though I haven't used PCRE in a very long time so my memory may be missing things). – Charles Duffy Feb 28 '23 at 21:45
  • 1
    BTW, you also want `array+=( "$2" )` _with the quotes_ instead of `array+=($2)`, and similarly, it should be `sed "$compiler_pattern"`, not just `sed $compiler_pattern`, after you've changed that `sed` expression to be something that'll actually work. – Charles Duffy Feb 28 '23 at 21:46
  • @CharlesDuffy, thanks! I'm new to bash and I'd take a long time to find it out without your help – John Doe Feb 28 '23 at 21:51
  • BTW, depending on whether you have GNU or BSD `sed`, either `-r` or `-E` should put it into a mode where it honors ERE syntax instead of needing BRE. Still won't take PCRE, but ERE is a fair bit less ugly in some places. – Charles Duffy Feb 28 '23 at 22:00
  • @CharlesDuffy, spending some time in google I thought that the dot operator is the same in each regex "version" – John Doe Feb 28 '23 at 22:06
  • Yes, the dot operator itself is the same across all of them, but `+` (for example) isn't, so even though `.` means the same thing across all regex forms, `.+` doesn't. (It's only BRE where `+` isn't special, which is one of the reasons I generally use ERE when it's an option; granted, you can get the same effect with `.\{1,\}` in BRE, but that's painful and not portable to ERE or PCRE since in either of those the backslashes aren't called for). – Charles Duffy Feb 28 '23 at 22:20
  • 1
    Also, `sed` doesn't just take expressions, it takes *commands* (which may involve expressions) -- like `s/pattern/replacement/` to replace the first match of `pattern` with `replacement` on each line, or `/pattern/ d` to delete lines that contain matches to `pattern`, etc. You need to tell `sed` *what to do* with that pattern. – Gordon Davisson Feb 28 '23 at 22:24

0 Answers0