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.
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 "|".
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.
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 "|".
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.
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