Q:

How to search for a strings between two tokens in a directory in ubuntu terminal and save the output

I'm looking for everything between the string "gem '" and the string "'" in a directory.
For example:
gem 'mysql2'
In the example above I would be looking for an output such as this:
mysql2
I have worked on couple of rails projects, so from their gem files, i want to make a list of all the gems which i have used so far.
It is important that I can also save the data output in a text file.

A:

You can use awk:
s="gem 'mysql2'"
awk -F"'" '{print $2}' <<< "$s"
mysql2

To save the output use:
var=$(awk -F"'" '{print $2}' <<< "$s")

Explanation:

-F"'" - Uses ' as field separator
print $2 prints 2nd field which is the string inside single quote

Same thing using grep -oP:
grep -oP "'\K[^']+" <<< "$s"
mysql2

