0

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

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 脚本来获取我的条件(在所需参数上)并使用文件名本身打印所需输出变量的行。为了实现这一点,我正在使用agand regex。我正在尝试编写这样的脚本,它为我过滤报告并打印所需的行:

#!/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 "$1" 
    ;;
    2)
        ag "$1" $(ag -l "$2")
    ;;
    3) 
        #ag "(?s)^(?=.*$2)(?=.*$3).*\n\K(?-s).*($1)"
        ag $1 $(ag -l "(?s)^(?=.*$2)(?=.*$3)")
    ;;
    4)
        ag $1 $(ag -l "(?s)^(?=.*$2)(?=.*$3)(?=.*$4)")
    ;;
esac

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

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

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

“准确度”和“折叠”是要放在输出上的所需输出。我也喜欢匹配的文件名,这是 ag 中的默认行为。使用这种模式,即“Val1|Val2”将导致 ag 在文件名之后打印两个包含行,这是我想要的。

该脚本应该在多行正则表达式中使用,并且当给定的过滤条件为空时也不会产生任何结果。

4

1 回答 1

0

空大小写的问题是因为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 "$1" 
    ;;
    2)
        LIST=$(ag -l "$2")
        # Check if the list is empty
        if [[ -z "$LIST" ]]
        then
            echo "Check your conditions, No mathced file were found"
        else
            ag "$1" $(ag -l "$2")
        fi
    ;;
    3) 
        LIST=$(ag -l "(?s)^(?=.*$2)(?=.*$3)")
        # Check if the list is empty
        if [[ -z "$LIST" ]]
        then
            echo "Check your conditions, No mathced file were found"
        else
            ag $1 $(ag -l "(?s)^(?=.*$2)(?=.*$3)")
        fi
    ;;
    4)
        LIST=$(ag -l "(?s)^(?=.*$2)(?=.*$3)(?=.*$4)")
        # Check if the list is empty
        if [[ -z "$LIST" ]]
        then
            echo "Check your conditions, No mathced file were found"
        else
            ag $1 $(ag -l "(?s)^(?=.*$2)(?=.*$3)(?=.*$4)")
        fi
    ;;
esac

于 2017-06-27T14:04:46.173 回答