Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Match substring with regex inside file with bash

I have a following text file that contains content:

┌─ This is folder description
│
├─ ##### ─────── Magenta 
│
├─ Size ──────── 245.7 GiB
│
├─ URL ───────── https://www.google.com
│
│
├─ Video ─────── 23.976fps
│
├─ Audio ─────┬─ French 
│             │         
│             │
│             └─ German
│
├─ Subtitles ─── French, German, Italian, C#

I want to verify if there is French between Audio and Subtitles, I don’t care rest.

I tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

grep -E 'Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))' test.txt

But no match. I’m pretty sure that is something wrong in using grep. Can lead me to correct way to do that ?

>Solution :

grep reads 1 line at a time by default so you can’t do a multi-line match on a single line. Using GNU grep for -z (to read the whole file at one time) and -o (as I’m guessing at what output you want):

$ grep -Ezo 'Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))' test.txt
Audio ─────┬─ French
│             │
│             │
│             └─ German
│
├─ Subtitles

Alternatively, using any awk:

$ awk '
    { rec = rec $0 ORS }
    END {
        if ( match(rec,/Audio(.*)(French|Franz?)(.*)Sub(s|(titles?)|(titulos))/) ) {
            print substr(rec,RSTART,RLENGTH)
        }
    }
' test.txt
Audio ─────┬─ French
│             │
│             │
│             └─ German
│
├─ Subtitles
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading