Bash — Automatiser le grep dans des fichiers

Aujourd’hui, je vous propose de discuter un peu scripting basique sous UNIX. ⌨️

Dans le cadre de mon travail, je me suis souvent retrouvé à devoir faire des recherches (via grep et ses variantes) multi-critères et dans des fichiers (voire de très très nombreux et volumineux fichiers). Assez rapidement, la ligne de commande devient une gageure et, pour tout dire, une perte de temps. Aussi, puisqu’un bon informaticien est un fainéant qui tâchera d’automatiser au maximum les tâches répétitives, j’ai décidé de scripter un petit outil pour faire le job et me fournir un retour graphique très opérationnel, à base de OK verts et de KO rouges.

# SETS VARIABLES
#
#setting the date format
dateheure=$(date +%Y%m%d-%T)
#setting the logsearch root path
baselogfile=/home/logfiles
#setting the lists root path
baseassetslist=/home/assets
#initializing the OK and KO counters
countok=0
countko=0

# PREPARING INPUT AND OUTPUT FOLDERS
#
#if does not exist, creates the path
mkdir -p /home/logfiles
mkdir -p $HOME/output-logs
output=$HOME/output-logs

# CHOOSES THE LOGS TO PARSE
#
#allows easy completion
cd $baselogfile
echo
echo -e "\033[96m# \033[93mWorking directory is \033[7m"$baselogfile"\033[0m\n"
echo -e "\033[96m# \033[93mLatest files:\033[0m "
#lists line by line the five latest modified files
ls -1t | head -5
echo
#prompts the logsearch relative filepath to parse
read -e -p $’\033[93mEnter the path to the logfile(s) to parse:\033[0m ‘ logfile

# CHOOSES THE ASSETS TO CHECK
#
#allows easy completion
cd $baseassetslist
echo
echo -e "\033[96m# \033[93mWorking directory is \033[7m"$baseassetslist"\033[0m\n"
echo -e "\033[96m# \033[93mExisting files:\033[0m "
ls -1
echo
#prompts the list file to grep on
read -e -p $’\033[93mEnter the path to the assets list file:\033[0m ‘ assetslist

# CREATES THE OUTPUT PATH AND LOGFILE
#
#sets the output path/file
outputfile=$output/$assetslist-$dateheure
#creates the output file
touch $outputfile
echo

# DISPLAYS THE RESULTS
#
echo -e "\033[96m# \033[93mStatus:\033[0m "
while read line #loops to work line by line in the list file to display a block output
do
#checks the first character of each line in the list file
substring=$(echo $line | cut -c 1)
#if it is a #, echo the line as it is a comment
if [ $substring == "#" ];
then
echo -e "\033[94m"$line"\033[0m";
#if it is a #, echo the line as it is a comment
elif [ $substring == "<" ];
then
echo -e "\033[95m"$line"\033[0m";
#grep stops at first match and goes next
else fgrep -i -m 1 "devname="$line $baselogfile"/"$logfile >>$outputfile;
if [ $? == 0 ];
then
#displays a green OK if matches
echo -e "[ \033[32mOK\033[0m ]"’\t’$line;
((countok++)) #increments OK counter
else
#displays a red KO if does not match
echo -e "[ \033[31mKO\033[0m ]"’\t’$line;
((countko++)) #increments KO counter
fi
fi
done < $baseassetslist"/"$assetslist
echo

# DISPLAYS THE TOTALS
#
echo -e "\033[96m# \033[93mTotal \033[32mOK\033[0m:"’\t’"\033[0m "$countok
echo -e "\033[96m# \033[93mTotal \033[31mKO\033[0m:"’\t’"\033[0m "$countko
echo

# GIVES NOTICE OF THE OUTPUT FILE LOGPATH
#
echo -e "\033[96mNote: \033[0man output file has been generated there: \033[7m"$outputfile"\033[0m"```

Si vous avez des idées d’amélioration (qualité du code, performance, sécurité, etc.), je suis preneur ! 😉