如何将包含括号的字符串拆分为数组,并在shell脚本的括号中保留子字符串

时间:2013-06-04 09:38:33

标签: bash shell sed

我有一个这样的字符串:

a1="a,b,c,(d,e),(f,g)";

如何获取数组

arr=["a","b","c","d,e","f,g"];

我想将括号中的逗号替换为其他字符,并在转换为数组后将其还原

但我不知道如何只替换括号之间的逗号; 怎么办呢?

3 个答案:

答案 0 :(得分:1)

GNU sed解析器

sed 's/,/\",\"/g;s/(\(.\)\"/\1/g;s/\"\(.\))/\1/g;s/^\w\+=\"/arr=[\"/;s/;/];/'

答案 1 :(得分:0)

写一个解析器! :d

我不知道如何在bash中执行此操作,但我可以向您展示如何在PHP中执行此操作(应该可以转换为其他语言)。

$str = "a,b,c,(d,e),(f,g)";
$out = array();
$current_token = "";
$open_brackets = 0;
$length = strlen($str)
for ($i = 0; $i < $length; $i += 1) {
    $chr = $str[$i];
    if ($chr === "(") {
        $open_brackets += 1;
    } else if ($chr === ")") {
        $open_brackets -= 1;
    } else if ($open_brackets === 0 && $chr === ",") {
        $out[] = $current_token; // push token value to out
        $current_token = "";
    } else {
        $current_token .= $chr;
    }
}
if (strlen($current_token) > 0) {
    $out[] = $current_token; // dont forget the last one
}
var_dump($out); // ["a","b","c","d,e","f,g"]

未经测试,但这是大纲。跟踪括号的数量,并且仅当括号匹配时才应将,解释为分隔符。

答案 2 :(得分:0)

尝试使用脚本,我使用正则表达式解析字符串。这对我来说很尴尬,但似乎有效:

#!/usr/bin/env bash

unset arr
a1="a,b,c,xxx(d,e),sdf(f,g)"

## The regular expression does an alternation between
## a pair of parens followed by an optional comma "\([^\)]+\)(,?)"
## or any characters followed by a comma or end of line "[^,]+(,|$)"
## After that I save all the rest of the string to match it in
## following iterations.
while [[ $a1 =~ ([^\(,]*\([^\)]+\)(,?)|[^,]+(,|$))(.*) ]]; do

    ## BASH_REMATCH keeps grouped expressions. The first one
    ## has the data extracted between commas. This removes the
    ## trailing one.
    elem="${BASH_REMATCH[1]%,}"

    ## Remove opening paren, if exists one.
    elem="${elem/\(/}"

    ## Remove trailing paren, if exists one.
    elem="${elem%)}"

    ## Add element to an array.
    arr+=("$elem")

    ## Use the string left (fourth grouped expression in
    ## the regex) to continue matching elements.
    a1="${BASH_REMATCH[4]}"
done

printf "%s\n" "${arr[@]}"

像以下一样运行:

bash script.sh

它产生:

a
b
c
xxxd,e
sdff,g