找回密码
 注册

QQ登录

只需一步,快速开始

查看: 510|回复: 0

Sample code for DecisionPlus - Read in csv data

[复制链接]
发表于 2011-8-23 14:42:06 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2011-8-23 14:47 编辑

Usually we can just use DecisionPlus to do it. just as example here:

  1. !file 1 $MTDAT/certs

  2. #define certs 1

  3. !temp txtline,,a160,t
  4. !temp filename,,a100
  5. !temp txtfile,,i2
  6. !temp charct,,i2
  7. !temp charstr,,a80,t
  8. !temp onechar,,a1
  9. !temp threechar,,a3
  10. !temp startpos,,i2
  11. !temp endpos,,i2
  12. !temp hostname,,a3

  13. !init scroll 4 : filename = arg : gosub INIT

  14. END\
  15.         end
  16. INIT\
  17.         fopen txtfile filename "r"
  18. ...     fopen txtfile "/tmp/transfers_certs_rcp.out" "r"
  19.         hostname=$HOSTNAME

  20. NEWLINE\
  21.         fgets txtfile txtline eof  = RETURN
  22.         charct = 10
  23. NEXTCHAR\
  24.         onechar = getstr(txtline,charct,1)
  25.         if onechar = " " then goto CHK_HOST
  26.         charct = charct + 1: goto NEXTCHAR
  27. CHK_HOST\
  28.         threechar = getstr(txtline,charct+8,3)
  29.         if threechar = hostname then goto GET_HEAT
  30.         goto NEWLINE

  31. GET_HEAT\
  32.         charct = charct + 27
  33.         cert_num = getstr(txtline,charct,24)
  34.         read certs nsr=CONT
  35.         goto NEWLINE
  36. CONT\
  37.         cert_num = getstr(txtline,charct,24)
  38.         insert certs
  39.         print cert_num
  40.         goto NEWLINE
  41. RETURN\
  42.         return
复制代码
But Linux already has very strong capability on text processing, so I tried to use script to get data and only pass the data been read out to the DecisionPlus program.

like we have a csv file for grouping id with just customer id and master id, like.
MasterID        Cust_ID
V 1375        S  829
        K 2121
        V 1375
        R 1561
        R  551
        E 1693
        S  348
        R  430
        E 4926
S  884        R  388
        S  884
V 5162        K 3628
        K 4009
        V 5162
        E 4204
        S 2430
        R 1768
V  486        E 6613
        V  486
        K 1050
csv file like:
  1. dev]$ cat supplier_list_2.csv
  2. V 1375,S  829
  3. ,K 2121
  4. ,V 1375
  5. ,R 1561
  6. ,R  551
  7. ,E 1693
  8. ,S  348
  9. ,R  430
  10. ,E 4926
  11. S  884,R  388
  12. ,S  884
  13. V 5162,K 3628
  14. ,K 4009
  15. ,V 5162
  16. ,E 4204
  17. ,S 2430
  18. ,R 1768
  19. V  486,E 6613
  20. ,V  486
  21. ,K 1050
复制代码
we have bash script file getcsv.jcl as:
  1. #!/bin/bash
  2. #INPUT=supplier_list.csv
  3. function pause(){
  4.    print '...'
  5.    read -p "$*"
  6. }

  7. INPUT=$1
  8. if [ "$1" == "" ]; then
  9.         INPUT=supplier_list_1.csv
  10. fi
  11. echo "Input file: $1"
  12. OLDIFS=$IFS
  13. IFS=,
  14. masterid=""
  15. [ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
  16. while read master acct
  17. do
  18.         #echo "Name : " $master "Acct :" $acct
  19.         if [ "$masterid" == "" ]; then masterid="$master"; fi
  20.         if [ "$master" == "" ]; then master="$masterid"; else masterid="$master"; fi
  21.         ./showp.jcl "$master" "$acct"
  22.         #/appl/sagexec/sagerep input_supp_csv nullp "$acct" "$master"
  23.         /appl/sagexec/sagerep input_supp_csv nullp UPDATE "$acct" "$master"
  24.         #echo 'Next to go ...'
  25.         #echo "Name :  ${master}| Acct : ${acct}|"
  26. done < $INPUT
  27. IFS=$OLDIFS
复制代码
and then, the DecisionPlus only used to update data by the number provided by arguments.
  1. ... input_supp_csv.r

  2. ... Append new items to supplier_master
  3. ... 08-23-11 Johnz: use parameters to update supplier grouping table inside Metaltraq

  4. !file 1 $MTDAT/supplier_master


  5. !temp master,,a6,u+
  6. !temp supplier,,a6,u+
  7. !temp tmp_key,,a6
  8. !temp tmpstr,,a60,t
  9. !temp tmp_count,,i2

  10. !init scroll 4 : tmp_key           = arg
  11. !init scroll 5 : supplier          = arg
  12. !init scroll 6 : master            = arg
  13. !init gosub INIT

  14. ...!heading gosub HEAD


  15. !final gosub TAIL
  16. ...    print "Show Data ..."
  17.     if tmp_key = "UPDATE" then \
  18.        rewind 1 : suppm_master_id = master : suppm_num = supplier : \
  19.        print "Insert "; supplier; " -> ";master;" ..." : \
  20.        insert 1 re = RERROR : \
  21.        print "                           --- succeed." \
  22.     else \
  23.        print "Supplier:";supplier;" ---> ";master
  24.     print "..."
  25.     rewind 1
  26.     tmp_count = tmp_count + 1
  27. END\
  28.     exit 0

  29. RERROR\
  30.     print "Supplier ";master;" existed! insert failed."
  31.     goto END

  32. ._________________________________________________________________________________
  33. INIT\
  34.     tmp_count = 0
  35. .    print "Starting ..."
  36.     if tmp_key <> "UPDATE" then \
  37.        master = supplier : supplier = tmp_key
  38. .    print "Supplier:";supplier;" - Master:";master
  39.     return
  40. .________________________________________________________________

  41. TAIL\
  42.     return
复制代码

and this code can be easily used to update other data tables.



您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-4-10 21:01 , Processed in 0.033515 second(s), 16 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表