Skip to content
Advertisement

Match unknown substring with RegEx

How can I get an unknown substring with an regular expression? I know what’s before and after the wanted string but I don’t want the known part with in the result.

Example text:

jhgjgjgvocher_SOMETHINGHERE.dbhjjkghjkg
vocher_SOMETHINGELSE.db

I’m looking for ‘SOMETHINGHERE’ and ‘SOMETHINGELSE’ only.

vocher_ and .db are always before and after the relevant part but should not be in the result.

A working solution is:

cat test | egrep -o "vocher_.*.db" | cut -d "_" -f2 | cut -d "." -f1

… but you know it’s ugly.

Is it possible to search exactly for an unknown part with regex (in this case only the .* part), or do I need to use something like sed? Is there a better solution?

Advertisement

Answer

A simple solution using perl is the following:

perl -ne 'if (/vocher_(.*).db/){ print "$1n";}' test_file.txt

This iterates line-by-line over the file and only prints the desired portion.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement