File validations in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File validations in KSH
# 1  
Old 11-26-2011
File validations in KSH

Please help develop script for below requirement
-------Sample file-------------------------------
Code:
HJUPITER000502
D0928053001
D0928053002
D0928053003
D0928053003
D0928053005
D0928053006
D0928053007
D0928053008
T000008

-----------------------------------------------
Here is the List of Validation. First Char is Type of Record Say Header/Detail/Trailer.

1. There must be only one Header in the file and it should always be first line of the file. Also it should start with 'H'.
2. HJUPITER000502. The number indicates any sequence. Here validation required is that the number in Header must be of 6 digits only.3. There must be only one Trailer in the file and it should always be last line of the file. Also it should start with 'T'.
4. T000008. The number indicates the count of Details in the file. In this case it is 8. Here validation required is that the number in Trailer must be of 6 digits and this count must match with the count of Details in the file.
5. D0928053002. The details must satrt with 'D'.

Thanks in advance.

Last edited by vbe; 11-26-2011 at 05:19 AM.. Reason: code tags
# 2  
Old 11-26-2011
What have you done so far?
# 3  
Old 11-28-2011
Here are some tips for the OP:

Code:
# cat test.txt
HJUPITER000502
D0928053001
D0928053002
D0928053003
D0928053003
D0928053005
D0928053006
D0928053007
D0928053008
T000008

# You can use the below to count records: H, D and T
# egrep '^H' test.txt
HJUPITER000502
# egrep -c '^H' test.txt
1

# And below to get the last 6 positions in H records (can also be used in T)
# egrep '^H' test.txt | awk '{print substr($0, length($0)-5)}'
000502

# 4  
Old 11-29-2011
Here are some checks using bash:

Code:
#!/bin/bash
FILE=./infile
FIRST_LINE=$(head -1 $FILE)
LAST_LINE=$(tail -1 $FILE)
if [ ${FIRST_LINE:0:1} != "H" ]
then
    echo "First line must be Header record"
    exit 1
fi
 
SEQ=${FIRST_LINE:${#FIRST_LINE}-6}
case $SEQ in
   *[!0-9]*) echo "Invalid header sequence: $SEQ" ; exit 2 ;;
esac
 
if [ ${LAST_LINE:0:1} != "T" ]
then
    echo "Last line must be a T record"
    exit 3
fi
 
LINES=${LAST_LINE:2}
case $LINES in
   [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) echo "Invalid line count: $LINES" ; exit 4 ;;
esac
 
if (( 10#$LINES+2 != $(wc -l < $FILE ) ))
then
    echo "Invalid line count in T record: $LINES"
    exit 5
fi
 
if (( 10#$LINES != $(grep -c "^D" $FILE ) ))
then
    echo "Invalid number of D records"
    exit 6
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. Shell Programming and Scripting

Ksh: How to write the log file simultaneously when .sql file executes by UNIX process

Hello Team- we would like to implement an approach which has to write the log file simultaneously when .sql file is executing by Unix process. At present,it is writing the log file once the process is completed. I've tested the current process with the below approaches and none of them... (1 Reply)
Discussion started by: Hima_B
1 Replies

3. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

4. Shell Programming and Scripting

Errors in if condition validations-Shell Script

Hello All, i am facing difficulty in validating the values, kindly help me in resolving the issue. Thanks a lot in advance. -Chandra Script:Test.sh #! /bin/sh # *************************************************************************** # Function to display help function usage()... (1 Reply)
Discussion started by: duddukuri
1 Replies

5. Shell Programming and Scripting

Field validations in multiple files CSV

Hi, I am regular reader of this forum. My advanced thanks to everyone. Below given are the sample files INDATA (Main data) Fild1Çfld2Çfld3….. Fild1Çfld2Çfld3….. Fild1Çfld2Çfld3….. Fild1Çfld2Çfld3….. Fild1Çfld2Çfld3….. . . N records (140000) eg GRPDATA (Reference file) (2 Replies)
Discussion started by: hyperion.krish
2 Replies

6. Shell Programming and Scripting

ksh- redirect stderr to file and then modify the file

I have the following: remsh $host -n 2>>syslog_issue_list.txt grep -i -e "EMS" -e "error" -e "warning" -e "excessive" /var/adm/syslog/syslog.log | awk /"$DATE1"/ | awk -vhost="$host" '!/remsh|telnetd/{print host "\n", $0 >> "syslog_issue_list.txt"}' I am creating a health script that has... (4 Replies)
Discussion started by: chipblah84
4 Replies

7. Shell Programming and Scripting

KSH - help needed for creating a script to generate xml file from text file

Dear Members, I have a table in Oracle DB and one of its column name is INFO which has data in text format which we need to fetch in a script and create an xml file of a new table from the input. The contents of a single cell of INFO column is like: Area:app - aam Clean Up Criteria:... (0 Replies)
Discussion started by: Yoodit
0 Replies

8. Shell Programming and Scripting

KSH script to run other ksh scripts and output it to a file and/or email

Hi I am new to this Scripting process and would like to know How can i write a ksh script that will call other ksh scripts and write the output to a file and/or email. For example ------- Script ABC ------- a.ksh b.ksh c.ksh I need to call all three scripts execute them and... (2 Replies)
Discussion started by: pacifican
2 Replies

9. Shell Programming and Scripting

KSH to group records in a file and compare it with another file

Hi, I've a file like below: DeptFile.csv DeptID EmpID ------- ------ Dep01 Emp01 Dep01 Emp02 Dep01 Emp03 Dep02 Emp04 Dep02 Emp05 I've another file which has EmpFile.csv EmpID Salary ------ ------ (3 Replies)
Discussion started by: Matrix2682
3 Replies

10. Shell Programming and Scripting

"|" separated file validations

Hi, I have a file with "|" separated fields. If the line doesn't contain n(say 9) "|" then put this line in a file called invalid_file.txt. If it does put this row in a file called valid_file.txt. Any help is highly appreciated. Thanks, SK (1 Reply)
Discussion started by: kolesunil
1 Replies
Login or Register to Ask a Question