Q:

BASH: grep text in a long string

Can anyone explain how to write a regex to get a value in a very long txt file full of meta. The whole file is without any newline separators, just a very long string, which is hard to read or analyze
I need to grep values after key username. Can anyone help? Seem to be stuck writing out a proper regex exression for this case
    .."somevalue\";s:7:\"text1\";s:8:\"username\";s:9:\"USER1\";s:7:\"company\";s:3:\"text2\";s:5:\  "somevalue\";s:11:\"text11\";s:8:\"username\";s:15:\"USER2\";s:7:\"company\";s:17:\"XXXX\";s:5:\...   "somevalue\";s:15:\"text110000\";s:8:\"username\";s:12:\"USER3_HERE\";s:7:\"company\";s:18:\"yyyyy\";s:

In the above example I need the following output
    USER1
    USER2
    USER3_HERE

A:

With Perl it is
perl -wn -le 'print for /\\"username\\";.*?\\"([^\\"]+)/g' filename

-n - process file line by line, but don't print anything
-l - handle line endings
-e - run the following code
print for /\\"username\\";.*?\\"([^\\"]+)/g

Print the captured output whenever you see \"username\"; followed by something followed by \" .
Output
$ perl -wn -le 'print for /\\"username\\";.*?\\"([^\\"]+)/g'
    .."somevalue\";s:7:\"text1\";s:8:\"username\";s:9:\"USER1\";s:7:\"company\";s:3:\"text2\";s:5:\  "somevalue\";s:11:\"text11\";s:8:\"username\";s:15:\"USER2\";s:7:\"company\";s:17:\"XXXX\";s:5:\...   "somevalue\";s:15:\"text110000\";s:8:\"username\";s:12:\"USER3_HERE\";s:7:\"company\";s:18:\"yyyyy\";s:

USER1
USER2
USER3_HERE

See also
perlrun for the command line switches
perlre for the regular expression used

