I had input file with below data,
abcdefghij;20100903040607;1234567891;GLOBAL;
Having values of fields with seperated by semi-colon ( and ended with line feed (\n).
Through shell script, how can I check the field formatting?
Thanks in advance.
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
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 ?
#!/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
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