Checking the format of inputs in a file

Hi,

I have a script that takes the contents of another file as inputs. Its assumed that there are 3 values in the input file that are seperated by '|'. I have to check in my script, whether the filed seperator used in the input file is '|' or not. If its not a '|' I have to print a error message. How can I do it.

Thanks in advnace.

You could use awk.

awk -F "\|" '{ if (NF != 3) printf ("Not good\n");  }' input.file

If you want to do it within the script, you can take this approach

#! /bin/ksh

while read line
do
        last="${line##*\|}"

        if [ "$last" = "$line" ] ; then
                echo "Not good"
                break;
        fi ;
done < input.file

Unlike the awk, the script will not go ahead if it encounters a non-conforming input.

Hi,
The script works fine when there are no "|" symbols in the file. But even if one of them is present then it still says that the format is ok, which is not the exact way.
ie....
I have inputs like this.

Mani|123 prof

This kind of input also is accepted by the script. But it should not accept. It should accept only if all the three field are seperated by "|".

Thanks in advance

echo "$record_read" | egrep -q '^.*([|]).*([|]).*$'
if [ $? -eq 0 ] ; then
  echo "good"
else
  echo "bad"
fi

Does the awk statement work well ?

Hi,
The script is working fine for the following inputs:

Mani|123|proff
Mani 123|proff
Mani|123 proff

But when I just give '|||' in the input file it still tells me that its 'good'. Even when I give the input as Mani 123 proff|||, it tells that its good.

Thanks in advance

Okay.

Why don't you explain EXACTLY what is allowed/not allowed. We are trying to hit a moving target.

Try the following :

#!/bin/ksh
#

cnt=0
while read line
 do
  let cnt=$cnt+1
  first=`echo $line | cut -f1 -d'|'`
  second=`echo $line | cut -f2 -d'|'`
  third=`echo $line | cut -f3 -d'|'`
   if [ "$first" = "" -o "$second" = "" -o "$third" = "" ]; then
        print "\nERROR: Line \#$cnt \"$line\" is missing a field.\n"
   fi
 done < delimit.txt

However its always good to try things out.. spoon feeding never helps..

Hi,

Thanks it worked.

Hi,

Now I want to do the same for , when I have four fields in my input file. When I tried to introduse a variable fourth and do the same thing it didnt work.

This is the script that worked for 3 fields in the input file

cnt=0
while read line
do
cnt=`expr $cnt + 1`
first=`echo $line | cut -f1 -d'|'`
second=`echo $line | cut -f2 -d'|'`
third=`echo $line | cut -f3 -d'|'`
if [ "$first" = "" -o "$second" = "" -o "$third" = "" ]; then
print "\nERROR: Line \#$cnt \"$line\" is missing a | field.\n"
print "\n The fields in the input file must be seperated by |"
fi
done < sdh_input_dl

I want to do the same, if there are four fields in my input file and I must check whether they are seperated by a "|".

Thanks in advance

Hi,
Sorry. It was my mistake. Instead of giving field f4 I was giving only f3.

I corrected it and it worked.

Hi,
I modified the code like this, when my input file has four fields.

cnt=0
while read line
do
cnt=`expr $cnt + 1`
first=`echo $line | cut -f1 -d'|'`
second=`echo $line | cut -f2 -d'|'`
third=`echo $line | cut -f3 -d'|'`
fourth=`echo $line | cut -f4 -d'|'`
if [ "$first" = "" -o "$second" = "" -o "$third" = "" -o "$fourth" = "" ]; then
print "\nERROR: Line \#$cnt \"$line\" is missing a | field.\n"
print "\n The fields in the input file must be seperated by |"
fi
done < sdh_input_dl

My input file "sdh_input_dl" has got fields like this.

76H/1_DL16S_16S-LOAD-G5-R2/S2|16S-LOAD-G5-R2/S2|TP2.1.1 N

If you see that last 2 fields i.e TP2.1.1 and N are not seperated by a "|". But this code doesnt give me the correct output.Please help me out.

Thanks in advance

Hi,

With the earlier script that I had given, its not working when I introduce a fourth field and seperator between third and fourth field is a space and not a "|". Where could I be going wrong.

The thing is that I have to check whether all the four fileds in the input file are seperated by a "|" or not. If not I have to display a message saying that the input file format is not correct.

The code goes awry when I seperate the third and fourth fields by a space.

i.e
MANI|123|26 REGISTER

Please help me out.
Thanks in advance.

Buddy.. I told you.. you need to work.. dont rely on spoon feeding.. Anyways.. here is the code.. this is flexible code.. you can have n number of fields.. separated by any delimiter..

#!/bin/ksh
#
# usage $0 <file> <delimiter> <Number of fields>

file=$1
delimiter=$2
num=$3
cnt=0
i=0

while read line
 do 
  let cnt=$cnt+1
  while [  $i -lt $num ]
  do
   let i=$i+1
   field=`echo $line | cut -f$i -d"$delimiter"`;
 
  if [ "$field" = "" ]; then
        print "\nERROR: Line #$cnt \"$line\" is missing field #$i.\n"
        break
  fi
  done
  i=0
 done < $file

I hope this helps..