option 1 is correct. Each file will comply with one template only and Variables will always precede the data.Please give your idea to write a script that will
read the text and match these templates.
As a test, I created 3 sample files. The first two have the proper layout, but the third is missing a field. The program is quite simple in that it counts each successful element. If three, message saying ok but if not then message saying bad.
> cat file1
City: Brockton
Zip: 02330
State: MA
> cat file2
City: Boston
Zip: 02109
State: MA
> cat file3
City: Boston
Zip: 02109
>
> cat ck_format
#! /bin/bash
xf="file"
cnt=1
max=5
while [ $cnt -le $max ]
do
zf="$xf""$cnt"
# echo $zf
#verify file integrity
if [ -s $zf ]
then
flag=0
testf=$(cat $zf | grep "^City:")
if [ -n "$testf" ]
then
flag=$((flag+1))
fi
testf=$(cat $zf | grep "^Zip:")
if [ -n "$testf" ]
then
flag=$((flag+1))
fi
testf=$(cat $zf | grep "^State:")
if [ -n "$testf" ]
then
flag=$((flag+1))
fi
if [ $flag -eq 3 ]
then
echo "The file "$zf" is a good file"
else
echo "The file "$zf" is a bad file"
fi
fi
cnt=$((cnt+1))
done
program execution is:
> ck_format
The file file1 is a good file
The file file2 is a good file
The file file3 is a bad file
thank you ! I am new to Unix scripting, your logic helped me a lot to build my script. I have few more questions
How can include " case Ignore " while checking for the template? &
Is there a way to check the order of the Words "City", "Zip" & "state" can be verified ie.. can We should the file is bad in case the file containts text like
Zip:
City:
State:
something like awk '/City/' ---> only in first line and then update template1 to template1+1??