How to grep for a word within two inverted commas. The word may contain any number of characters . e.g "two" , "three"

  • What do "inverted comma" and "word" mean in this context? You can get a list of all "words" in an input with grep -iw '[A-Z_0-9][A-Z_0-9]*' (not all versions of grep support \w, so I avoided it). If you are just looking for the contents of a comma separated list, you could do sed 's/,/\n/g' – Adam Katz Mar 7 '15 at 0:50

grep will show a matching line, so all you have to do is to find the two double-quotes...

grep '".*"'

If you want to extract a word that is within double quotes you can, for example, do...

awk 'match($0,/"[^"]*"/) {print substr($0,RSTART+1,RLENGTH-2)}'
  • 1
    Or use grep -o to only show the matching portion of the line (with a non-greedy regex). – Stephen Kitt Mar 6 '15 at 17:32
  • Stephen, you don't need the non-greedy version if you implement the grep regexp as I've done above with awk. But you will get the quotes as part of the output, and the OP spoke about a match within the quotes. – Janis Mar 6 '15 at 17:37
  • Indeed, it's not quite equivalent; I just thought it might be useful to add that since the -o option to grep seems to be little-known. – Stephen Kitt Mar 6 '15 at 19:13
  • Stephen, along your thought it's probably also worthwhile to note that with option -o multiple matches per line lead to one matched result per output line. So you can just pipe the result into, say, sed s/\"//g to get one match per line. – Janis Mar 6 '15 at 19:52

Extracting word(s) following @StephenKitt comment:

grep -oP '(?<=")[\w\s]+?(?=")'


grep -oP '(?<=")\w+?(?=")'

for single word strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.