andschar
andschar

Reputation: 3993

grep pattern not at the begining or the end of sub-string

How can I grep only the ", which are not at the beginning or the end of a "sub-string"?

Hereby, I define a "sub-string" to be characters, separated by a comma ,. Hence, I want the ", which is between blub and didup as well as the one, which is between jo and ha.

The following code greps all ":

echo 'test,"blub"didup","jo"ha"' | grep '"'

Upvotes: 1

Views: 190

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You can use

grep '[^,]"[^,]'

It matches

  • [^,] - any char other than a comma
  • " - a double quote
  • [^,] - any char other than a comma.

See an online grep test:

echo 'test,"blub"didup","jo"ha"' | grep '[^,]"[^,]'
# => test,"blub"didup","jo"ha"

Replacing such quotes is possible with sed:

echo 'test,"blub"didup","jo"ha"' | sed ':a; s/\([^,]\)"\([^,]\)/\1 \2/; ta'
# => test,"blub didup","jo ha"

See an online sed demo.

Upvotes: 2

anubhava
anubhava

Reputation: 786359

How can I grep only the ", which are not at the beginning or the end of a "sub-string"

Using gnu grep you may do this using look arounds to match any double quote that is not at start or end:

grep -oP '(?<!^)"(?!$)' <<< 'test,"blub"didup","jo"ha"'

Or using any awk:

awk -F '[^"]*' '{for (i=2; i<NF; ++i) print $i}' <<< 'test,"blub"didup","jo"ha"'

Upvotes: 1

Related Questions