2017-06-27 10 views
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 

は、今私は(必要なパラメータで)私の条件を取得するシェルスクリプトを作成し、ファイル名自体に所望の出力変数の行を印刷しようとしている。報告書は、このようなものです。これを達成するために私は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 "$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」を使用すると、ファイル名の後ろに両方の行を含む印字が必要になります。

スクリプトは、複数行の正規表現全体で動作する必要があります。また、指定したフィルタ条件が空の場合は結果を出力しません。

答えて

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 

関連する問題