pstanton
pstanton

Reputation: 36640

sed regular expression extraction

i have a range of strings which conform to one of the two following patters:

("string with spaces",4) or (string_without_spaces,4)

I need to extract the "string" via a bash command, and so far have found a pattern that works for each, but not for both.

echo "(\"string with spaces\",4)" | sed -n 's/("\(.*\)",.*)/\1/ip'

output:string with spaces

echo "(string_without_spaces,4)" | sed -n 's/(\(.*\),.*)/\1/ip'

output:string_without_spaces

I have tried using "\? however it does not match the " if it is there:

echo "(SIM,0)" | sed -n 's/("\?\(.*\)"\?,.*)/\1/ip'

output: SIM

echo "(\"SIM\",0)" | sed -n 's/("\?\(.*\)"\?,.*)/\1/ip'

output: SIM"

can anyone suggest a pattern that would extract the string in both scenarios? I am not tied to sed but would prefer to not have to install perl in this environment.

Upvotes: 1

Views: 47

Answers (1)

falsetru
falsetru

Reputation: 368894

How about using [^"] instead of . to exclude " to be matched.

$ echo '("string with spaces",4)' | sed -n 's/("\?\([^"]*\)"\?,.*)/\1/p'
string with spaces
$ echo "(string_without_spaces,4)" | sed -n 's/("\?\([^"]*\)"\?,.*)/\1/p'
string_without_spaces

$ echo "(SIM,0)" | sed -n 's/("\?\([^"]*\)"\?,.*)/\1/p'
SIM
$ echo '("SIM",0)' | sed -n 's/("\?\([^"]*\)"\?,.*)/\1/p'
SIM

Upvotes: 1

Related Questions