简体   繁体   English

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

[英]Find file that match patterns located in different lines

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. 我正在做一个程序,该程序可以找到与用户指定的两种模式(日期和ID)匹配的文件,两种模式都位于每个文件的不同行中。 The files are located in different .zip sub folders. 这些文件位于不同的.zip子文件夹中。 My code is not working and I'm trying to use PCRE DOTALL. 我的代码不起作用,我正在尝试使用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' .

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命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM