The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #8  
Old 05-05-2006
Registered User
 

Join Date: Feb 2006
Location: California
Posts: 45
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..
Reply With Quote
Forum Sponsor
  #9  
Old 05-07-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
Hi,

Thanks it worked.
Reply With Quote
  #10  
Old 05-17-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
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
Reply With Quote
  #11  
Old 05-17-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
Hi,
Sorry. It was my mistake. Instead of giving field f4 I was giving only f3.

I corrected it and it worked.
Reply With Quote
  #12  
Old 05-18-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
Arrow

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
Reply With Quote
  #13  
Old 05-18-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
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.
Reply With Quote
  #14  
Old 05-19-2006
Registered User
 

Join Date: Feb 2006
Location: California
Posts: 45
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 03:26 PM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0