Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Grab second string between two single quotes

Tags:

regex

grep

Can I get a little help matching a string in the below text?


The default username and password is 'user' and 'ZWiliWH8E2mV'.


I'm trying to get the string between the second set of single quotes: ZWiliWH8E2mV. This string is randomly generated, and I can only rely on the formatting, and not the ZWiliWH8E2mV. After some googling, I can match it with grep:

cat file_name | grep -oP "(?<=').*?(?=')"

but it's the 3rd match, and I'm not sure how to get to it from there. I'm open to using other tools if they're better for what I'm trying to do, but I'm not very versed in them.

like image 855
robocopgodzilla Avatar asked Jun 23 '26 01:06

robocopgodzilla


1 Answers

I'm trying to get the string between the second set of single quotes

Using awk, you can avoid regex:

s="The default username and password is 'user' and 'ZWiliWH8E2mV'."

awk -F "'" '{print $4}' <<< "$s"
ZWiliWH8E2mV

Here we are using ' as field delimiter and 4th field in awk will give us 2nd value wrapped inside single quotes.

like image 105
anubhava Avatar answered Jun 27 '26 12:06

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!