I've long list of generated reports which I want to filter. The report is something like this:
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
Now I'm trying to write a shell script which get my conditions (on desired parameters) and print the lines of desired output variables with the filename itself. To achieve this I'm using ag
and regex
. I'm trying writing a script like this which filter the reports for me and prints the desired lines:
#!/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
This script works somehow but I've problem with case 4 when I'm trying to match 3 words. Also another problem is when the filter conditions doesn't give back any file and in this case the script prints the desired output value of all files (instead of not do anything).
One complicated case example is this:
> FilterResults "(Accuracy:)|Fold:" "algName:.*RCPB" "Radius:.*8" "approach:.*DD_4"
"Accuracy" and "Fold" are the desired output to be put on the output. I also like to have the matched file name as well which is the default behavior in ag. Using this pattern i.e. "Val1|Val2" will cause ag to print both containing line after filename which is what I want.
The script should work with across multi-line regex and also put no results when the given filtering condition is empty.
The problem with empty case is because ag "Pattern" $()
will match any file since the $() is empty.
#!/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