0

I'm doing a program that can find a file(s) that match two patterns given by the user (Date and ID), both patterns are located in different lines inside every file. The files are located in different .zip sub folders. My code is not working and I'm trying to use PCRE DOTALL.

File Sample:

    TextTextTextTextText
    TextTextText: [20-MAY-2017]
    TextTextTextTextText
    TextTextTextTextText
    TextTextTextTextText
    TextTextText: [123456]

Code I'm using:

        echo "Set a specific Date [ DD-MM-YYYY ]: "
        read -r Date
        echo -e "Introduce ID: "
        read -r ID
        #Search pattern
        grep -Pzo '(?s)$Date.*\n.*$ID' .
miken32
  • 42,008
  • 16
  • 111
  • 154
Jafet Soto
  • 41
  • 6

1 Answers1

1

You can't use variables in single quoted strings. Try this out:

#!/bin/bash
read -r -p "Set a specific Date [ DD-MMM-YYYY ]: " searchdate
read -r -p "Introduce ID: " searchid
grep -Pzo "(?s)\[$searchdate\].*\[$searchid\]" sample.txt

Provided your input doesn't have a / character in it, you could also use the simpler awk command:

awk "/$searchdate/,/$searchid/" sample.txt 
miken32
  • 42,008
  • 16
  • 111
  • 154