Checking the format of inputs in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking the format of inputs in a file
# 8  
Old 05-05-2006
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

However its always good to try things out.. spoon feeding never helps..
# 9  
Old 05-08-2006
Hi,

Thanks it worked.
# 10  
Old 05-18-2006
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  
Old 05-18-2006
Hi,
Sorry. It was my mistake. Instead of giving field f4 I was giving only f3.

I corrected it and it worked.
# 12  
Old 05-18-2006
CPU & Memory

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  
Old 05-19-2006
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  
Old 05-19-2006
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

I hope this helps..

Last edited by Ambikesh; 05-19-2006 at 07:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in sending inputs to format command using expect

Hi , I am not able figure out how to use expect tool to send input to this prompt of format Searching for disks...done selecting c0t3d0 FORMAT MENU: disk - select a disk type - select (define) a disk type partition - select (define) a partition... (3 Replies)
Discussion started by: chidori
3 Replies

2. Shell Programming and Scripting

Storing user inputs into a file

Hi, Am trying to store the user inputs into a file, but the below code will store only the first line of the values. I need to store all the user input values which may contain one or more lines. Thanks in advance. echo "please enter file names"; read name; echo $name>/tmp/test (11 Replies)
Discussion started by: rogerben
11 Replies

3. Shell Programming and Scripting

Take 10 user inputs and output to file?

I want a script that will prompt a user to enter 10 numbers and out put them into a file. This what I have so far, but isn't working. I'm guessing it's something easy I'm not seeing. Thanks for any help. #!/usr/bin/ksh echo "Enter 10 numbers" for i in 1 2 3 4 5 6 7 8 9 10 do read .... ... (8 Replies)
Discussion started by: AxlVanDamme
8 Replies

4. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

5. Shell Programming and Scripting

How to read inputs from a file

Hello; Please I need to read inputs from a file change 1 or 2 things the output to another file. (1 Reply)
Discussion started by: jimoney
1 Replies

6. Shell Programming and Scripting

Regarding checking the file format

Hi, This with reference to my earlier thread. I have file called input that has 2 values. Now I have to check whether the 2 fileds are seperated by a "|" or not. I am not able to do this with the following script. while read line do first=`echo $line | cut -f1 -d'|'` ... (8 Replies)
Discussion started by: sendhilmani123
8 Replies

7. Shell Programming and Scripting

Validating inputs from a file

Hi, I have a file called inputs. Now that file has the values like this: 1 2 3 Now In my script called 'get.sh' I do this : exec < inputs read a b c d Now I know that there will not be any value in d. How can I check it. I need the exact condition for checking whether the variable has... (1 Reply)
Discussion started by: sendhilmani123
1 Replies

8. Shell Programming and Scripting

redirecting serial inputs to a file?

i have an external device sending serial messages i want to connect this device to a serial port in my sun blade box and record those messages to a file, how can i read the serial port and write it to file? Thanks (4 Replies)
Discussion started by: guilartec
4 Replies

9. Shell Programming and Scripting

Inputs from a file

Hi, I have a shell script that has to taken inputs from a file say "Inputs". Now I take 2 inputs at a time. Suppose the Inputs file contains numbers like 2 3 4 5 Now I have a written a script for adding 2 numbers. When I run the script for first time 2 and 3 must be the inputs. When i run the... (4 Replies)
Discussion started by: sendhil
4 Replies

10. Shell Programming and Scripting

Reading in two inputs from a file

Hi all, I've been assigned the task of modifying a script which reads in names of tables from a list file, exports a 30 days worth of data from these tables, then deletes the table. The list file will now contain a table name and a number next to it indicating how many days to archive. I need... (1 Reply)
Discussion started by: MadHatter
1 Replies
Login or Register to Ask a Question