Tuesday, March 15, 2022

[SOLVED] How can I search for a matching substring in a file with regex and return only the substring?

Issue

I have a file that is 50 MB and only one line. It is corrupt json and I am searching for a substring.

I have a problem with grep that it does not support lazy search in the default syntax. And pearl syntax doesn't seem to be supported on MacOS.

Here is a basic expression that does work but only returns the id, while I need the entire object

grep -o '1234' largefile

Here are some things I tried that did not work

grep -oP '1234.*?globalId' largefile

P not supported

grep -F '1234' largefile | grep -o -E '.{30} 1234.{500}'

invalid repetition count(s)

grep -o '1234.{100}' largefile

doesn't return anything

How can I do this search? It doens't need to be grep. I sometimes read about awk, perl, ripgrep and other stuff that I have never had reason to try.


Solution

Use Perl one-liner if grep -P is not supported. For example, this will print all matches captured in parens, 1 match per line:

perl -lne 'print for /(1234.*?globalId)/g' in_file


Answered By - Timur Shtatland
Answer Checked By - Clifford M. (WPSolving Volunteer)