Regarding checking the file format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regarding checking the file format
# 1  
Old 05-30-2006
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'|'`
second=`echo $line | cut -f2 -d'|'`
echo $first $second
if [ "$first" = "" -o "$second" = "" ]
then
print "\nERROR: Line \#$cnt \"$line\" is missing a | field.\n"
print "\n The fields in the input file must be seperated by |"
exit 1
fi
done < input

This script is not working. Please help me out.
Thanks in advance
# 2  
Old 05-30-2006
Code:
if [ $(echo $line|grep -c '|') -ne 1 ]
then
   print "\nERROR: Line \#$cnt \"$line\" is missing a | field.\n"
   print "\n The fields in the input file must be seperated by |"
   exit 1
fi

# 3  
Old 05-30-2006
Hi,
Thanks. It worked. How can I do it if my input file has 4 fields and they are seperated by "|". Will the same logic work. I tried it. It didnt work.

Thanks in advance
# 4  
Old 05-30-2006
did you change the condition,

to hold no of "|" delimiters

assuming 3 "|" for separating 4 fields

if [ $(echo $line|grep -c '|') -ne 3 ]
# 5  
Old 05-30-2006
Other way:
Code:
if [ $(echo "$line"|grep -c "^.\{1,\}|.\{1,\}|.\{1,\}$" ) -ne 1 ]
then
.....

Note that in \{1,\} ,1 is the min length of the param in that postion.
Cheers
# 6  
Old 05-30-2006
Hi,
The real problem is that the user may give 3 fields or 4 fields in the input file. I shoud perform a check based on that. Thats where I am stuck. Since the number of fields may vary(3 or 4). How can i place a check for that.


Thanks in advance
# 7  
Old 05-30-2006
Code:
awk -F\| '{if ( NF != 3  &&  NF != 4 ){print "Bad param number"}else{for (i=1;i<=NF;i++){print "Param"i": "$i}}}'<input

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to generate Excel file or to SQL output data to Excel format/tabular format

Hi , i am generating some data by firing sql query with connecting to the database by my solaris box. The below one should be the header line of my excel ,here its coming in separate row. TO_CHAR(C. CURR_EMP_NO ---------- --------------- LST_NM... (6 Replies)
Discussion started by: dani1234
6 Replies

2. Shell Programming and Scripting

Need help to format one txt file to required format

Hello Everyone, I have one source file which is genarated by SAP in different format(Which I've never seen). I need to convert that file to required format and I need to read this target file from Datastage to use this in my Jobs. So I do not have any other options except to use Unix script to... (4 Replies)
Discussion started by: Prathyu
4 Replies

3. Shell Programming and Scripting

Converting windows format file to unix format using script

Hi, I am having couple of files which i used to copy from windows to Linux, so now in case of text files (CTRL^M) appears at end of line. I know i can convert this windows format file to unix format file by running dos2unix. My requirement here is that i want to do it automatically using a... (5 Replies)
Discussion started by: sarbjit
5 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

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (2 Replies)
Discussion started by: Samtel
2 Replies

6. UNIX for Dummies Questions & Answers

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (1 Reply)
Discussion started by: Samtel
1 Replies

7. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like ..ݡ.ݡ ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies

8. UNIX for Dummies Questions & Answers

Convert UTF8 Format file to ANSI format

:confused: Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on... (9 Replies)
Discussion started by: rajreddy
9 Replies

9. UNIX for Advanced & Expert Users

Convert UTF8 Format file to ANSI format

:) Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on this.........Let me... (1 Reply)
Discussion started by: rajreddy
1 Replies

10. Shell Programming and Scripting

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... (13 Replies)
Discussion started by: sendhilmani123
13 Replies
Login or Register to Ask a Question