Linux 查找与位于不同行中的模式匹配的文件

Linux 查找与位于不同行中的模式匹配的文件,linux,shell,grep,pcregrep,Linux,Shell,Grep,Pcregrep,我正在做一个程序,可以找到一个(s)文件,匹配用户给出的两种模式(日期和ID),这两种模式都位于每个文件的不同行中。这些文件位于不同的.zip子文件夹中。我的代码不起作用,我正在尝试使用PCRE DOTALL 文件示例: TextTextTextTextText TextTextText: [20-MAY-2017] TextTextTextTextText TextTextTextTextText TextTextTextTextText Tex

我正在做一个程序,可以找到一个(s)文件,匹配用户给出的两种模式(日期和ID),这两种模式都位于每个文件的不同行中。这些文件位于不同的.zip子文件夹中。我的代码不起作用,我正在尝试使用PCRE DOTALL

文件示例:

    TextTextTextTextText
    TextTextText: [20-MAY-2017]
    TextTextTextTextText
    TextTextTextTextText
    TextTextTextTextText
    TextTextText: [123456]
我正在使用的代码:

        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' .

不能在单引号字符串中使用变量。试试这个:

#!/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
如果输入中没有
/
字符,也可以使用更简单的awk命令:

awk "/$searchdate/,/$searchid/" sample.txt