![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to set the File Paths for Inputs and Outputs | Amruta Pitkar | UNIX for Dummies Questions & Answers | 1 | 10-31-2006 02:19 PM |
| Regarding checking the file format | sendhilmani123 | Shell Programming and Scripting | 8 | 05-30-2006 06:07 AM |
| Validating inputs from a file | sendhilmani123 | Shell Programming and Scripting | 1 | 05-10-2006 02:49 AM |
| Inputs from a file | sendhil | Shell Programming and Scripting | 4 | 02-01-2006 02:48 AM |
| Reading in two inputs from a file | MadHatter | Shell Programming and Scripting | 1 | 06-29-2005 07:17 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
|||
|
|||
|
Try the following :
Code:
#!/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
|
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
Hi,
Thanks it worked. |
|
#10
|
|||
|
|||
|
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 |
|
#11
|
|||
|
|||
|
Hi,
Sorry. It was my mistake. Instead of giving field f4 I was giving only f3. I corrected it and it worked. |
|
#12
|
|||
|
|||
|
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 |
|
#13
|
|||
|
|||
|
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. |
|
#14
|
|||
|
|||
|
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..
Code:
#!/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
Last edited by Ambikesh; 05-19-2006 at 03:26 PM. |
|||
| Google The UNIX and Linux Forums |