Reputation: 35
I have many text files and need to locate certain words that may exist in the context of the file but need only those that are in quotation marks.
Example: Find in the text below the word "search" only if in quotes (the word "search" may vary).
1. text text text text text text search text
2. text "search text text text text" text
3. text "SEARCH text text text text" text
For this precise example, I would expect only the words of line 2 and 3.
Thanks to anyone who can help me.
Upvotes: 2
Views: 3832
Reputation: 30273
If you can guarantee that there'll be only one set of quotes, then
/".*search.*"/i
should do. But if there can be more than one pair of quotes, then you have to ensure that an even number of quotes have been passed, lest you mistake a closing quote for an opening quote:
/^[^"]*("[^"]*"[^"]*)*"[^"]*search[^"]*"/i
Here's a demo. (Note that the demo contains \n
s purely for presentation purposes.) If you see two #
s in the demo regex, please replace them with parentheses (
)
—it is a limitation of the way RegexPal encodes its data in the URL.
Upvotes: 3
Reputation: 4384
I you want all waords between double quotes, I would simply use grep
:
grep -E -o '".*"' inputfile
I f you want only the first word:
sed -E 's/.+"([[:alpha:]]+) .*/\1/' inputfile
Upvotes: 0