sed取指定字串中的特定子字串 - Linux

Table of Contents

test="[ro.serialno]: [D6GQHIOZTKMBOZJR]"

echo $test | sed 's/.*\[\(.*\)\].*\[\(.*\)\].*/\2/g'

後面的‘\2’表示取第二個括號的值
D6GQHIOZTKMBOZJR

如果一個字串是
test="(ro.serialno): (D6GQHIOZTKMBOZJR)"

要如何用上面sed方式取出()內的字串呢?謝謝

--

All Comments

Candice avatarCandice2019-05-31
就... 把 \[ \] 換成 ( )
Andrew avatarAndrew2019-06-01
s/.*(\([^()]*\)): (\([^()]*\)).*/\2/
George avatarGeorge2019-06-06
grep -o '([^()]*)'