validate each field in txt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validate each field in txt
# 8  
Old 07-27-2007
Hope you have fixed you field 6 and 18 to be like MM:SS .
For the rest of the validation you can use the below script.

Might be a round about way, but this is what i have developed.

NOTE: I have to strip of spaces in the input file to print the entire line :-(

Code:
#!/bin/ksh

### Functions ###

is_digit()
{
        strLen=${#1}
        check_no=$(echo $1 | tr -d '[0-9]')
        if [[ $strLen -eq 5 && $check_no = "" ]]
        then
                return $SUCCESS
        else
                return $FAIL
        fi
}
is_string()
{
        strLen=${#1}
        check_no=$(echo $1 | tr -d '[A-Za-z]')
        if [[ $strLen -eq 5 && $check_no = "" ]]
        then
                return $SUCCESS
        else
                return $FAIL
        fi
}
validate()
{
        vRecord=$1
        vField=$2
        if [[ $vField -eq 1 || $vField -eq 4 ]]  # FOR FIELDS 1 AND 4
        then
                is_digit $vRecord
                if [[ $? -eq $SUCCESS ]]
                then
                        return $SUCCESS
                else
                        return $FAIL
                fi
        fi
        if [[ $vField -eq 2 ]]                  # FOR FIELDS 2
        then
                is_string $vRecord
                if [[ $? -eq $SUCCESS ]]
                then
                        return $SUCCESS
                else
                        return $FAIL
                fi
        fi
        if [[ $vField -eq 5 ]]                 # FOR FIELDS 5
        then
                if [[ $vRecord = "N" || $vRecord = "Y" ]]
                then
                        return $SUCCESS
                else
                        return $FAIL
                fi
        fi
        if [[ $vField -eq 24 || $vField -eq 25 ]]  # FOR FIELDS 24 AND 25
        then
                tmpStr=$(echo $vRecord | sed 's/[A-Z]\{3\}//')
                if [[ $tmpStr = "" ]]
                then
                        return $SUCCESS
                else
                        return $FAIL
                fi
        fi
        if [[ $vField -ge 7 && $vField -le 17 ]] || [[ $vField -ge 19 && $vField -le 23 ]] # FOR 7-17 AND 19-23
        then
                tmpStr=$(echo $vRecord | sed 's/[0-9]//')
                if [[ $tmpStr = "" ]]
                then
                        return $SUCCESS
                else
                        return $FAIL
                fi
        fi


}

### Main ###

export FAIL=1
export SUCCESS=0
InpFile=$1
NO_OF_COLUMNS=25
for line in `cat $InpFile | tr -d ' '`
do
        PRINT_FLAG=0
        field_no=1
        while [[ $field_no -le $NO_OF_COLUMNS ]]
        do
                fRecord=$(echo $line | cut -d',' -f$field_no)
                validate $fRecord $field_no
                if [[ $? -eq $FAIL ]]
                then
                        PRINT_FLAG=1
                        break;
                fi
                field_no=$((field_no+1));
        done
        if [[ $PRINT_FLAG -eq $SUCCESS ]]
        then
                echo $line
        fi
done


Last edited by lorcan; 07-27-2007 at 05:04 AM..
# 9  
Old 07-27-2007
thank you very much for your help.

I have some idea for this.....SmilieSmilie

can i ask you one more question.
if the filed 6 should incorrect time format like "hh:mm:ss:00", can I write a simple script to delete the last two digit and showing correct one like "hh:mm:ss"

Thx again!!
# 10  
Old 07-27-2007
Yes you can

Code:
echo 12:30:00:00 | sed 's/\([0-9]\{1,2\}:[0-9]\{1,2\}:[0-9]\{1,2\}\).*/\1/'
12:30:00

or

Code:
Time=12:30:00:00
New_time=${Time%:*}
echo $New_time

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk, sed, shell all words in INPUT.txt find in column1 of TABLE.txt and replce with column2 in

Hi dears i have text file like this: INPUT.txt 001_1_173 j nuh ]az 001_1_174 j ]esma. nuh ]/.xori . . . and have another text like this TABLE.txt j j nuh word1... (6 Replies)
Discussion started by: alii
6 Replies

2. Shell Programming and Scripting

Desired output.txt for reading txt file using awk?

Dear all, I have a huge txt file (DATA.txt) with the following content . From this txt file, I want the following output using some shell script. Any help is greatly appreciated. Greetings, emily DATA.txt (snippet of the huge text file) 407202849... (2 Replies)
Discussion started by: emily
2 Replies

3. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

4. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

5. UNIX for Dummies Questions & Answers

find lines in file1.txt not found in file2.txt memory problem

I have a diff command that does what I want but when comparing large text/log files, it uses up all the memory I have (sometimes over 8gig of memory) diff file1.txt file2.txt | grep '^<'| awk '{$1="";print $0}' | sed 's/^ *//' Is there a better more efficient way to find the lines in one file... (5 Replies)
Discussion started by: raptor25
5 Replies

6. Shell Programming and Scripting

how to add extra a field in a flat txt file ?

Hi all, I did not use UNIX for a long time, now i need to make a flat file with extra field, can you help me with the code ? 1. I create a last line of each log from each system and make it in a flat text file (seperate by a pipe |) mv current.log old tail -1 sanfrancisco.log > current.log... (5 Replies)
Discussion started by: britney
5 Replies

7. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

8. Shell Programming and Scripting

how to validate a field when it is blank using awk prog

Hi, I tried the below piece of code for my script to check whether it has a blank space for a particular field. if(f10==/]/){ print "Field 10 is Correct";} else{ print "Field 10 is Wrong"; } Please help me to know whether the "if" condition applied here is correct or do i... (14 Replies)
Discussion started by: meva
14 Replies

9. UNIX for Dummies Questions & Answers

echo "ABC" > file1.txt file2.txt file3.txt

Hi Guru's, I need to create 3 files with the contents "ABC" using single command. Iam using: echo "ABC" > file1.txt file2.txt file3.txt the above command is not working. pls help me... With Regards / Ganapati (4 Replies)
Discussion started by: ganapati
4 Replies

10. Shell Programming and Scripting

Reindex or re-increment last field of txt file.

I am using a database text file with a field that increments +1 with each new entry, occasionally if a entry is deleted the unique sequence is disrupted. I am looking for a small script/function in sh and/or perl that would re index this. Example of db file: Name | Address | misc |number... (2 Replies)
Discussion started by: silenthands
2 Replies
Login or Register to Ask a Question