I want to validate a text file with a script.
The file to validate is:
FDFHDK JKL
1545665 152
HDKFHDK UHG
YRYRUBH DFG
867HDKE WER
Valid lines must match the regex '[A-Z]{7}+[[:space:]]+[A-Z]{3}'
.
If all the lines are valid, the script shows a message saying that the file is OK.
If at there is at least one line that doesn't match the regex, the script should show a message and display the lines that don't match the regex.
The script is:
#!/usr/bin/env bash
result=""
output=$(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1" |wc -l)
if [[ $output > 0 ]]
then
echo "These lines don't match:"
result="${resultado} $(grep -vE '[A-Z]{7}+[[:space:]]+[A-Z]{3}' "$1") \n"
echo -e $result
else
echo "The text file is valid"
fi
The expected output is
These lines don't match
FDFHDK JKL
1545665 152
867HDKE WER
But I'm getting
These lines don't match:
FDFHDK JKL 1545665 152 867HDKE WER
So the actual script is not taking the line break into account.
echo -e $result
– ilkkachu Nov 09 '21 at 16:021234567abcdefg ABC
be valid, for example? – terdon Nov 09 '21 at 17:581234567abcdefg ABC
is not valid the regex is'[A-Z]{7}+[[:space:]]+[A-Z]{3} '
– Emilio Galarraga Nov 09 '21 at 18:02ABCDEFGABCDEFG ABC
. Would that be valid? – terdon Nov 10 '21 at 09:10