编写脚本以根据给定条件过滤出文本报告并输出所需值

Writing a script to filter out text reports based on given conditions and outputs desired values

我有一长串要过滤的已生成报告。报告是这样的:

Report Name
Report Date
Blah blah blah
Parameter1: 85.34%
Blah blah
Paramter2 FF_6
Parameter3: 345
Parameter4: value
blah blah
OutputVar1: 98.34%
OutputVar2: 345
blah blah 
Output Var3:
      45, 34, 178
blah blah

现在我正在尝试编写一个 shell 脚本来获取我的条件(根据所需参数)并使用文件名本身打印所需输出变量的行。为此,我使用了 agregex。我正在尝试编写这样的脚本,为我过滤报告并打印所需的行:

#!/bin/sh
# Script Name: FilterResults
# Purpose: Search through report directory (txt files) and put the desired output from all files which have the condition. 
# Usage Example: 
#    FlterReports OutputPattern FilterCondition1 FilterCondition2 FilterCondition3

case "$#" in
    1)
        ag "" 
    ;;
    2)
        ag "" $(ag -l "")
    ;;
    3) 
        #ag "(?s)^(?=.*)(?=.*).*\n\K(?-s).*()"
        ag  $(ag -l "(?s)^(?=.*)(?=.*)")
    ;;
    4)
        ag  $(ag -l "(?s)^(?=.*)(?=.*)(?=.*)")
    ;;
esac

这个脚本能以某种方式工作,但是我在尝试匹配 3 个单词时遇到了案例 4 的问题。另外一个问题是 当过滤条件不返回任何文件时,在这种情况下,脚本打印所有文件的所需输出值(而不是不做任何事情)。

一个复杂的案例是这样的:

> FilterResults "(Accuracy:)|Fold:" "algName:.*RCPB" "Radius:.*8" "approach:.*DD_4"

"Accuracy" 和 "Fold" 是要放在输出上的所需输出。我也喜欢有匹配的文件名,这是 ag 中的默认行为。使用此模式,即 "Val1|Val2" 将导致 ag 在文件名后打印包含行,这正是我想要的。

该脚本应该跨多行正则表达式工作,并且在给定的过滤条件为空时也不会放置任何结果。

空大小写的问题是因为 ag "Pattern" $() 将匹配任何文件,因为 $()​​ 是空的。


#!/bin/zsh
# Purpose: Search through report directory (txt files) and put the desired output from all files which have the condition. 
# Author: SdidS

case "$#" in
    1)
        ag "" 
    ;;
    2)
        LIST=$(ag -l "")
        # Check if the list is empty
        if [[ -z "$LIST" ]]
        then
            echo "Check your conditions, No mathced file were found"
        else
            ag "" $(ag -l "")
        fi
    ;;
    3) 
        LIST=$(ag -l "(?s)^(?=.*)(?=.*)")
        # Check if the list is empty
        if [[ -z "$LIST" ]]
        then
            echo "Check your conditions, No mathced file were found"
        else
            ag  $(ag -l "(?s)^(?=.*)(?=.*)")
        fi
    ;;
    4)
        LIST=$(ag -l "(?s)^(?=.*)(?=.*)(?=.*)")
        # Check if the list is empty
        if [[ -z "$LIST" ]]
        then
            echo "Check your conditions, No mathced file were found"
        else
            ag  $(ag -l "(?s)^(?=.*)(?=.*)(?=.*)")
        fi
    ;;
esac