How to check field formatting of input file?

Hi,

I had input file with below data,
abcdefghij;20100903040607;1234567891;GLOBAL;

Having values of fields with seperated by semi-colon (:wink: and ended with line feed (\n).
Through shell script, how can I check the field formatting?
Thanks in advance.

what do u want to check in formatting ?

only number of fields or you want to verify each fields ?

I want to check below 3 things,
1] number of fields in each line
2] Fields should be seperated by semi-colon
3] Line should be ended with line feed \n

Could you please help me out to write shell script for this? Thanks in advance.

You read awk guide first, understand what's the meaning of :

FS
NF

then you should know the answer.

thanks for reply. but, I need to write a shell script, not awk.

Hi, could you show what you have tried so far and where you are stuck?

I tried below code, but not get expected output.

while read line ; do
  echo "$line"|wc -w
done < $FILE

That is because the field separator is not whitespace, but a semicolon. Try this:

while read line
do
   nrfields=$(IFS=\;; set -- $line; echo $#)
   if [ $nrfields -ne 4 ]; then
     printf "Wrong number of fields: \n%s\n" "$line"
   fi
done <  infile

Yes, Its working.
But my criteria is something different.
My input file data is,

abcdefghij;20100903040607;1234567891;GLOBAL;
04;ABC123;09;XYZ456;PQR6789;VRAI;;;;;;
04;EFG456;09;STU123;WXYZ786;FAUX;;;;;;
04;MNO101;09;LMN489;PQRS789;VRAI;;;;;;

1] First line should have 4 fields and need to check for last field value.
Solution is,

               line=`head -1 $FILE`
		echo "Line is : $line"
		nrfields=$(IFS=\;; set -- $line; echo $#)
		if [ $nrfields -ne 4 ]; then
		   echo "Wrong number of fields: $line"
		else
		FileType=`echo $line | cut -d ";" -f 4`
		echo "File Type is $FileType"
		fi

2] From 2nd line onward fields are 11 and all fields have values/not.
3] Need to check 6th fieldof each line. If 6th field value is VRAI, then values of 2nd and 4th fields from that line should be stored in an arraylist.

Please help me out to solve this 2 problems above.

I would do something like this.
#!/bin/bash
FILE=infile
IFS=\;
{
read -a L
if [ ${#L[@]} -ne 4 ]
then
echo "Wrong number of fields"
else
FileType="${L[3]}"
echo "File Type is $FileType"
while read -a L
do
if [ ${L[5]} = VRAI ]
then
echo "Elem1='${L[1]}' Elem2='${L[3]}'"
fi
done
fi
} <$FILE

Please tell us how the stored array should look like.
2 arrays with 1 record per matching line ?

I am getting error as,

read: 0403-010 A specified flag is not valid for this command.
for line,
if [ ${#L[@]} -ne 4 ]

#!/bin/bash
FILE=infile
L=$(head -1 $FILE)
if [ $(IFS=\;; set -- $L; echo $#) -ne 4 ] # As suggested by Scrutinizer
then
echo "Wrong number of fields : $L"
else
FileType="$(echo $L | cut -d\; -f4)"
echo "File Type is $FileType"
awk -F';' '$6 == "VRAI" { print $2, $4'} infile
fi

But sure a simple awk script woul do the best :wink:

but I need shell script solution for 2 and 3 points mentioned above.

Do you need to check that there are 11 fields ? and what kind of action if not ?
What form of arraylist ? Please, give an example.

error msg (wrong number of fields) should display if 11 fields are not there in a line.
I need to store these 2 field values in array / any variable.
Later I need to insert these values in database table under 2 columns.
Could you please let me know the correct way of storage for these 2 field values of all lines from file, so that I can use it into SQL INSERT INTO........ statement.

Revisited script (without awk) usingf bash arrays.#!/bin/bash
FILE=infile
IFS=\;
{
read L
L=( $L )
if [ ${#L[@]} -ne 4 ]
then
echo "HEADER : Wrong number of fields : $L"
else
echo "File Type is ${L[3]}"
while read L
do
L=( $L )
if [ ${#L[@]} -ne 11 ]
then
echo "DATA : Wrong number of fields : $L"
elif [ ${L[5]} = VRAI ]
then
echo "Perform the insert with MySQL and ${L[1]} ${L[3]}" # first field has number 0
# Perform the insert with MySQL and ${L[1]} ${L[3]}
fi
done
fi
} <$FILE

I need a script in korn shell.
I already told that, I am getting error while executing it.

Does it work if you put in lines 6 and 14 something like set -A L $L

Not working :frowning:

Still error is coming.