Wanted best way to validate delimited file records


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Wanted best way to validate delimited file records
# 1  
Old 10-18-2012
Error Wanted best way to validate delimited file records

actually i post about this issue before but many folkz miss-understood with my quesion,
We are checking for the delimited file records validation
Delimited file will have data like this:
Code:
Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg|
Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg|
Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg|

So we are checking for where the files of records we got is having validating length or not
NOTE:the structer of the file will be present in the teradata db,we will fetch the structer of the file then we will validate
in the teradata configured table we wil get details about
column name,oder number will be order of column in tht table….it will be 1 2 3….like tht,length of the column
for ex. if we are checking for file of 3 columns then in the table we wil hav 3 columns of size varchar(5),so now every feild in the file should have lenght <=5,

script which i wrote:
Code:
#------------------------------------------
#  Reading through the file and checking for the column length
#----------------------------------------------------
                logNote "Reading through the temp file and and checking for the column length"
 
                while read col_nm col_order_num col_len
                do
                                typeset -i col_len
                                typeset -i col_len_good
 
                                col_len_good=`expr $col_len + 1`
 
                                logNote "col_nm : $col_nm"
                                logNote "col_order_num : $col_order_num"
                                logNote "col_len : $col_len"
                                logNote "col_len_good : $col_len_good"
 
                                awk 'BEGIN{col_ord='$col_order_num';col_l='$col_len'}{FS="|"}{if (length($col_ord) > col_l) print $0;}'  $Src_File >> $Src_File.bad
 
                                awk 'BEGIN{col_ord='$col_order_num';col_l='$col_len_good'}{FS="|"}{if (length($col_ord) < col_l) print $0;}'  $Src_File > $Src_File.temp
 
                                rm -f $Src_File
                                mv $Src_File.temp $Src_File
 
                done <$RPT_FILE
==============================================================


In the script col_nm, col_order_num ,col_len we will fetch from table
col_nm =column name
col_order_num =oder number will be order of column in tht table….it will be 1 2 3….like tht
col_len=length of the column


its working fine bi=ut we had performance issue.
can any come up with some better solution
mostly using with awk,awk array might be easy i guess
thankz in advance

Last edited by Franklin52; 10-23-2012 at 06:33 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-19-2012
If you just want to validate the general form of records, sed gives you regex and demux:
Code:
sed '
  /^[A-Z][a-z]\{3\}|[a-z]\{8\}|[a-z]\{10\}|[a-z]\{7\}|[a-z]\{6\}|$/{
    w good_recs
    d
    }
 ' in_file >bad_recs

You just need to generate the regex from your file specs.

For bulk processing speed, write a simple C program that reads lines, checks the line length, locates all pipes (offsets in a big array of integers), which can be checked against field count and field length command line arguments, and any other field filters you desire can be added integer, decimal, float, text, no white space, upper case, etc.). Of course, you could have standard regex for each field type you care to filter.

Last edited by DGPickett; 10-19-2012 at 01:13 PM..
# 3  
Old 10-23-2012
thanxz for reply but i want in ksh
can any one help me out with this plzs.


i am having this following code fr the same but i dont knw hw it works d i tried this but it show error in last line
Code:
awk '
  NR==FNR{                                 # When the first file is being read (only then are FNR and NR equal)
    W[$2]=$3                               # create an (associative) array element for the column widths with the second
                                           # field as the index using the Field separator (see below)
    next                                   # Proceed to the next record
  }
  {
    for(i in W)                            # for every line in the second file, for every column in array W
      if(length($i)>W[i]){                 # if the length of the corresponding field is more than the max column width then
        print > "file.bad"                 # print that record of the second file to "file.bad"
        next                               # Proceed to the next record
      }
  }
  1                                        # If there are no fields with more characters than the max column width then print the record..
' FS='[^0-9]*' colwidthfile FS=\| file     # Set FS to any sequence of non-digits for the first file. Set it to "|" for the second file.

can can one help in this!!!

Last edited by Franklin52; 10-23-2012 at 06:32 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 10-23-2012
The shells can parse fields if you change to $IFS to include the right field separators, preferable in a subshell so life is not severly bent for the rest of the shell. In place if ths space and tab separators in $IFS, put your simple delimiters, and then they are separated in terms of 'read', 'for myvar in' or arguments on a command line or shell function call. It's pretty simple, really. Something like "while read f1 f2 f3 f4 f5 f6 f7 f8;do . . . done" suggests itself. You can also subdivide fields in shell using ${varname%} or %%, #, ##. Substrings are a bit more work in ksh, but bash has this more gracefully built in: http://www.linuxmisc.com/12-unix-she...1efda506bc.htm
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need help on an old post - How to convert a comma delimited string to records or lines of text?

Hi, Apologies in advance to the moderator if I am posting this the wrong way. I've searched and found the solution to an old post but as it is a very old post, I don't see an option to update it with additional question. The question I have is in relation to the following post: How to... (6 Replies)
Discussion started by: newbie_01
6 Replies

2. Shell Programming and Scripting

Delimited records splitted into different lines

Hi I am using delimited sequence file. Delimter we are using is pipe .But for some of the records for one of the column the values are getting split into different lines as shown below "113"|"0155"|"2016-04-27 07:59:04"|"1930"|"TEST@TEST"|"2016-04-27 11:04:04.357000000"|"BO"|"Hard... (13 Replies)
Discussion started by: ginrkf
13 Replies

3. UNIX for Dummies Questions & Answers

How to convert a comma delimited string to records or lines of text?

Hi, I am not sure if I've posted this question before. Anyway, I previously asked about converting lines of text into a comma delimited string. Now I am needing to do the other way around ... :( :o Can anyone advise how is this possible? Example as below: Converting records/lines to... (2 Replies)
Discussion started by: newbie_01
2 Replies

4. Shell Programming and Scripting

what is the better way to validate records in a file.

hi all, We are checking for the delimited file records validation Delimited file will have data like this: Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg| Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg| Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg| Aaaa|sdfhxfgh|sdgjhxfgjh|sdgjsdg|sgdjsg| So we are... (4 Replies)
Discussion started by: Seshendranath
4 Replies

5. Shell Programming and Scripting

Print records which do not have expected number of fields in a comma delimited file

Hi, I have a comma (,) delimited file, in which few fields are enclosed with in double quotes " ". I have to print the records in the file which donot have expected number of field with the line number. File1 ==== name,desgnation,doj,project #header#... (7 Replies)
Discussion started by: machomaddy
7 Replies

6. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

7. Shell Programming and Scripting

How to convert a space delimited file into a pipe delimited file using shellscript?

Hi All, I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted... AA ATIU2345098809 009697 005374 BB ATIU2345097809 005445 006518 CC ATIU9685098809 003215 003571 DD... (7 Replies)
Discussion started by: nithins007
7 Replies

8. UNIX for Dummies Questions & Answers

Extract records by column value - file non-delimited

the data in my file is has no delimiters. it looks like this: H52082320024740010PH333200612290000930 0.0020080131 D5208232002474000120070306200703060580T1502 TT 1.00 H52082320029180003PH333200702150001 30 100.0020080205 D5208232002918000120070726200707260580T1502 ... (3 Replies)
Discussion started by: jclanc8
3 Replies

9. Shell Programming and Scripting

Delete Duplicate records from a tilde delimited file

Hi All, I want to delete duplicate records from a tilde delimited file. Criteria is considering the first 2 fields, the combination of which has to be unique, below is a sample of records in the input file 1620000010338~2446694087~0~20061130220000~A00BCC1CT... (5 Replies)
Discussion started by: irshadm
5 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question