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 01: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 01: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 Display Modes
  #1 (permalink)  
Old 05-04-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-04-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,662
You could use awk.

Code:
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

Code:
#! /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.
Reply With Quote
  #3 (permalink)  
Old 05-05-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
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
Reply With Quote
  #4 (permalink)  
Old 05-05-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,859
Code:
echo "$record_read" | egrep -q '^.*([|]).*([|]).*$'
if [ $? -eq 0 ] ; then
  echo "good"
else
  echo "bad"
fi

Last edited by jim mcnamara; 05-05-2006 at 03:31 AM.
Reply With Quote
  #5 (permalink)  
Old 05-05-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,662
Quote:
Originally Posted by sendhilmani123
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
Does the awk statement work well ?
Reply With Quote
  #6 (permalink)  
Old 05-05-2006
Registered User
 

Join Date: Apr 2006
Posts: 33
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
Reply With Quote
  #7 (permalink)  
Old 05-05-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,859
Okay.

Why don't you explain EXACTLY what is allowed/not allowed. We are trying to hit a moving target.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:31 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 Global Fact Book

Content Relevant URLs by vBSEO 3.2.0