
Me pregunto cómo puedo hacer esto de manera eficiente. Dados dos archivos, digamos a.txt y b.txt, quiero escribir un script bash para hacer lo siguiente:
Cada línea en a.txt que contenga '*' que no exista en b.txt se agregará al final de b.txt con una marca de tiempo.
Puedo hacer la primera y la última parte
grep "*" a.txt echo "$(date)" >> b.txt
pero no sé qué tal el resto.
Respuesta1
Le sugiero que utilice un tercer archivo para obtener resultados para no estropear el archivo b.txt. Podrías probar esto:
cp b.txt c.txt
for line in $(grep '*' a.txt); do
# for each line found in a.txt
echo "Found: $line"
grep -q $line b.txt # check its presence in b.txt
if [ $? -ne 0 ]; then
# if the result of the grep is not equal to 0
# it means that the line has not been found in b.txt
# then print the line in a third file with the leading timestamp
echo "$(date): $line" >> c.txt
fi
done
Está claro que tal vez deberías mejorar el grep ya que no sé cómo está compuesta la fila donde estás buscando el '*'.