sed regular expression extraction

pstanton

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.

falsetru

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular expression with conditional extraction

From Dev

the use of "+" in sed regular expression

From Dev

sed regular expression failure

From Dev

sed with regular expression

From Dev

the use of "+" in sed regular expression

From Dev

Regular expression with sed

From Dev

sed command with regular expression

From Dev

Negation of sed regular expression

From Dev

Sed Regular Expression with /P

From Dev

+ Regular Expression not working in sed

From Dev

Regular expression extraction from JSON string in PHP

From Java

Escaping plus in sed regular expression

From Dev

sed positional replacement with regular expression

From Dev

Sed multiline regular expression issue

From Dev

understand Regular expression in a sed command

From Dev

sed regular expression failed on solaris

From Dev

sed: regular expression,how to substitute?

From Dev

sed replace regular expression match

From Dev

Using Sed in a for loop with a regular expression

From Dev

sed regular expression for escaped urls

From Dev

How to match everything but a regular expression with sed?

From Dev

Converting a sed regular expression to python code

From Java

regular expression in sed replace question mark

From Dev

Is there another regular-expression "flavor" in GNU sed?

From Dev

sed to edit only part of a file with regular expression

From Dev

Converting a sed regular expression to python code

From Dev

Using Sed with regular expression to save results into a variable

From Dev

How to use [\w]+ in regular expression in sed?

From Dev

sed regular expression matching more than intended