Sponsored Content
Top Forums Shell Programming and Scripting Shell script for validating fields in a file Post 302390565 by asemota on Thursday 28th of January 2010 09:44:27 AM
Old 01-28-2010
Shell script for validating fields in a file

Hi,

I have not used Unix in a very long time and I am very rusty. I would appreciate any help I can get from the more experienced and experts in Shell script.

I am reading one file at a time from a folder. The file is a flat file with no delimeters or carriage return. Col1 through col6 is the header section and should be read once. Col7 - 14 is the body and can have 1 or multiple records. I read the file check to make sure none of these fields are blank. If there is a blank field, I would like to write a message to a log file that can be viewed later. I haven't gotten to the log file yet. Just trying to read the file with an inner loop just doesn't seem to work. Please help me.

Code:
 
#!/bin/ksh
 
#This script was created on 01/27/2010 to check outbound
# files for missing required fields
###########################################################
 
 
for file in /folder/file.1$.5$
 
do
 
i=1
 
exec< file
 
while read line
do
 
col1=`echo $line | cut -c1-4`
col2= `echo $line | cut -c5-5`
col3= `echo $line | cut -c6-7`
col4= `echo $line | cut -c8-16`
col5= `echo $line | cut -c17-23`
col6= `echo $line | cut -c51-56`
 
if [ -z "$col1" ]
then
echo "Line No. $i -- No String in position 1-4 "
else
echo "Line No. $i -- String in position 1-4 : $col1"
fi
 
if [ -z "$col2" ]
then
echo "Line No. $i -- No String in position 5 "
else
echo "Line No. $i -- String in position 5 : col2"
fi
 
if [ -z "$col3" ]
then
echo "Line No. $i -- No String in position 6-7 "
else
echo "Line No. $i -- String in position 6-7 : $col3"
fi
 
if [ -z "$col4" ]
then
echo "Line No. $i -- No String in position 8-16 "
else
echo "Line No. $i -- String in position 8-16 : $col4"
fi
 
if [ -z "$col5" ]
then
echo "Line No. $i -- No String in position 17-23 "
else
echo "Line No. $i -- String in position 17-23: $col5"
fi
 
if [ -z "$col6" ]
then
echo "Line No. $i -- No String in position 51-56 "
else
echo "Line No. $i -- String in position 51-56 : $col6"
fi
 
i=`expr $i + 1`
 
exec< file
 
 While read line
    do
 
        col7=`echo $line | cut -c66-72`
        col8= `echo $line | cut -c73-74`
        col9= `echo $line | cut -c75-83`
        col10= `echo $line | cut -c84-85`
        col11= `echo $line | cut -c86-88`
        col12= `echo $line | cut -c89-96`
        col13= `echo $line | cut -c97-100`
        col14= `echo $line | cut -c108-115`
 
 
        if [ -z "$col7"] or [ -z "$col8"] or [ -z "$col9"] or [ -z "$col10"]
          or[ -z "$col11" ] or [ -z "$col12" ] or [ -z "$col13"] or [ -z "$col14"]
        then
        echo "Line No. $i -- Missing String "
        else
        echo "Line No. $i -- String is valid"
        fi
 
        i=`expr $i + 1`
        done
done
 
done


Thank you all for your help.

Last edited by jim mcnamara; 01-28-2010 at 11:31 AM.. Reason: corrected code tags
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Validating variables in shells script

All Can you help me to validate a variable only for string and digit. That is variable should either fully alphabets or digits. Please send me result to my mail id also: REMOVED Thanx in advance Regards Deepak Xavier (1 Reply)
Discussion started by: DeepakXavier
1 Replies

2. UNIX for Dummies Questions & Answers

Validating input based on fixed number of fields

Yes, i did... let me state my problem in more detail Inputs: I have one input CSV file And, i have stored no. of comma each line should in a variable. e.g. $ cat cmt.csv this, is a ,comma ,count test1 ,,this, is a ,comma ,count test2 this, is a ,comma ,count test3... (6 Replies)
Discussion started by: Dipali
6 Replies

3. Shell Programming and Scripting

Validating month value in shell script

Hi, the below script is to get & check the correct values for minutes (0-59) that is inputed by user : printf "$FBOLD\nPlease enter the minutes (0-59): $FREG" read MIN case "$MIN" in |) break 2;; *) echo "" echo "Invalid minutes, please... (4 Replies)
Discussion started by: milink
4 Replies

4. UNIX for Dummies Questions & Answers

Validating a file

Pardon my ignorance but I am lost on how to do this I have a file called "Sample.txt", it is pipe delimited, it should have 13 fields. But some of the records do not, I would like to set up a shell script where I can pass in a parameter "Sample.txt" and it would split the file into records... (2 Replies)
Discussion started by: dgeehot
2 Replies

5. Shell Programming and Scripting

validating(pingable or not) remote ip address in shell script

i need to verify whether the ip adress given as input to the shell script is pingable or not... that is whether the ip is alive and responding.. ping $ip_adress the above wont work in script because the execution is continuous... so the shell script keeps will dwell in this pinging process...... (8 Replies)
Discussion started by: vivek d r
8 Replies

6. Shell Programming and Scripting

Aligning output with null fields in shell script

Hello Gurus ! I have what probably amounts to a few simply changes to fix; however for the life of me I cannot seem to get it ti work. I need to align the output of my script (I am writing to a logfile)... here's the lines in my code: if then echo "NODE: $node" >> $logfile... (6 Replies)
Discussion started by: gvolpini
6 Replies

7. Shell Programming and Scripting

How to get fields and get output with awk or shell script.?

I have a flat file A.txt with field seperate by a pipe 2012/11/13 20:06:11 | 284:hawk pid=014268 opened Locations 12, 13, 14, 15 for /home/hawk_t112/t112/macteam/qt/NET12/full_ddr3_2X_FV_4BD_1.qt/dbFiles/t112.proto|2012/11/14 15:19:26 | still running |norway|norway 2012/11/14 12:53:51 | ... (6 Replies)
Discussion started by: sabercats
6 Replies

8. Shell Programming and Scripting

Validating the file

Hello All, I have the following file. The first column is Type. Always the file will have one H and one T type in between all D type reocrds. Need todo some validations. H|ABCD D|TAB N0003107809CD2013-11-14|RYAN|FRY|7 DR|RICHMOND HILL|GA|32431|X|C95|000009999|000000001|TAB||C0001 D|TAB... (3 Replies)
Discussion started by: karumudi7
3 Replies

9. Shell Programming and Scripting

Shell Script to Group by Based on Multiple Fields in a file

Hi, I want to know if there is any simple approach to SUM a field based on group by of different fields for e.g. file1.txt contains below data 20160622|XXX1||50.00||50.00|MONEY|Plan1| 20160622|XXX1||100.00||100.00|MONEY|Plan1| 20160623|XXX1||25.00||25.00|MONEY|Plan1|... (3 Replies)
Discussion started by: cnu_theprince
3 Replies

10. Shell Programming and Scripting

Shell script to pull certain fields

I/m a beginner so be easy. I have text files that live on an AIX server. The files come in and I've been charged with writing a shell script to email out that pulls the first date, and the last date of the file. I need to load these 2 dates into 2 separate variables. I can figure out the variables,... (13 Replies)
Discussion started by: mattadams1983
13 Replies
CPANPLUS::Shell(3perl)					 Perl Programmers Reference Guide				    CPANPLUS::Shell(3perl)

NAME
CPANPLUS::Shell - base class for CPANPLUS shells SYNOPSIS
use CPANPLUS::Shell; # load the shell indicated by your # config -- defaults to # CPANPLUS::Shell::Default use CPANPLUS::Shell qw[Classic] # load CPANPLUS::Shell::Classic; my $ui = CPANPLUS::Shell->new(); my $name = $ui->which; # Find out what shell you loaded $ui->shell; # run the ui shell DESCRIPTION
This module is the generic loading (and base class) for all "CPANPLUS" shells. Through this module you can load any installed "CPANPLUS" shell. Just about all the functionality is provided by the shell that you have loaded, and not by this class (which merely functions as a generic loading class), so please consult the documentation of your shell of choice. BUG REPORTS
Please report bugs or other issues to <bug-cpanplus@rt.cpan.org<gt>. AUTHOR
This module by Jos Boumans <kane@cpan.org>. COPYRIGHT
The CPAN++ interface (of which this module is a part of) is copyright (c) 2001 - 2007, Jos Boumans <kane@cpan.org>. All rights reserved. This library is free software; you may redistribute and/or modify it under the same terms as Perl itself. SEE ALSO
CPANPLUS::Shell::Default, CPANPLUS::Shell::Classic, cpanp perl v5.14.2 2014-09-29 CPANPLUS::Shell(3perl)
All times are GMT -4. The time now is 06:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy