簡體   English   中英

查找與位於不同行中的模式匹配的文件

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

我正在做一個程序,該程序可以找到與用戶指定的兩種模式(日期和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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM