Questo sito utilizza i cookie per migliorare l'esperienza di navigazione. Continuando la navigazione acconsenti all'utilizzo dei cookie.

Bash Scripting

Yesterday, at work i had to solve a little problem: how can i verify that a text file A is conteined on another text file B.
IN ather words i have to take every row of file A and check if it is also a row of file B.

In a BASH script.

Easy? Yes it is. And then because it took me two hours? It was not quite so easy. At least for me.

 

That was my algorithm.

These are the files :

#!/bin/bash
RESOURCEFILE=/root/tools/res/hosts.cfg #File A
HOSTS=/etc/hosts #File B



loop on every row of file A :

while read line
do
...
done < $RESOURCEFILE

then loop on every word of the current row of file A

while read line
do
for word in $line
do
...
done
done < $RESOURCEFILE

to build a GREP command on file B

while read line
do
CMD="/bin/grep $(echo $line | awk '{print $1}') $HOSTS " #init the grep command
conta=0 #init the counter
for word in $line
do
conta=$((conta+1)) #a counter to skip the first iteration of the loop
test $conta -eq 1 &amp;&amp; continue #netxt iteration
CMD="${CMD} | /bin/grep $word " #for each iteration after the first one I  add a grep command to the CMD string
done
CMD=$(echo $CMD| sed -e 's/| \/bin\/grep$//g' ) #delete the last grep
done < $RESOURCEFILE

Built the command I just execute it :

while read line
do
CMD="/bin/grep $(echo $line | awk '{print $1}') $HOSTS "
conta=0
for word in $line
do
conta=$((conta+1))
test $conta -eq 1 &amp;&amp; continue
CMD="${CMD} | /bin/grep $word "
done
CMD=$(echo $CMD| sed -e 's/| \/bin\/grep$//g' ) #&amp;&amp; echo $CMD
eval "$CMD" >/dev/null 2>&amp;1 #launch the command
test $? -eq 1 &amp;&amp; echo $line #I evaluate the result : if the current line of the file A is not contained in the file B I print it on the standard output.
done < $RESOURCEFILE

All the work is done. Let's put all togheter

#!/bin/bash
   HOSTS=/etc/hosts #File B
   RESOURCEFILE=/root/tools/res/hosts.cfg #File A
   #loop on the rows of File A
   while read line
   do
      #initialization of the GREP command and the counter that will serve to skip the first iteration
      #of the loop on the words of the current line
      CMD="/bin/grep $(echo $line | awk '{print $1}') $HOSTS "
      conta=0
      #cycle on the words of the current line of file A
      for word in $line
        do
           conta=$((conta+1))
           # I skip the first iteration because i have already done the stuff in the initialization out of the loop
           test $conta -eq 1 &amp;&amp; continue
           #for each iteration after the first one I  add a grep command to the CMD string
           CMD="${CMD} | /bin/grep $word "
        done
     ##At the end of the loop just delete the last grep
     CMD=$(echo $CMD| sed -e 's/| \/bin\/grep$//g' ) #&amp;&amp; echo $CMD 
     ##launch the command just built it and I evaluate the result
     ##if the current line of the file A is not contained in the file B I print it on the standard output.
     eval "$CMD" >/dev/null 2>&amp;1
     test $? -eq 1 &amp;&amp; echo $line
  done < $RESOURCEFILE

Share

Aggiungi commento


Codice di sicurezza
Aggiorna