如何从Linux文件(myfile_I.out:application/octet stream;charset=binary)中的分隔文件中消除重复记录

如何从Linux文件(myfile_I.out:application/octet stream;charset=binary)中的分隔文件中消除重复记录,linux,sorting,ksh,Linux,Sorting,Ksh,我试图将数据从linux文件(其中包含重复项,数据从源表中卸载)加载到一个表中 mylinux file properties: $ file -bi myfile_I.out application/octet-stream; charset=binary 在将数据加载到表之前,我应该从linux文件中删除重复项 我删除重复项的方法: 将数据从源表卸载到临时文件(TempeEX.out) 从TempEX.out文件执行sort-u函数并删除 重复项和加载到my

我试图将数据从linux文件(其中包含重复项,数据从源表中卸载)加载到一个表中

     mylinux file properties: 
     $ file -bi myfile_I.out
     application/octet-stream; charset=binary
在将数据加载到表之前,我应该从linux文件中删除重复项

我删除重复项的方法:

  • 将数据从源表卸载到临时文件(TempeEX.out)
  • 从TempEX.out文件执行sort-u函数并删除 重复项和加载到myfile_I.out的最终唯一数据记录
  • 最后将myfile_I.out数据加载到目标_表
  • 我在步骤2中面临问题{无法从TempEX.out文件中删除完整的副本}

        #------------------------------------------------------------------#
        #- Delete the duplicates from TempEX.out write the unique data-----# 
        #------------------to myfile_I.out----------------------------------#
    
        echo -e "Eliminate the duplicates from the ${FILE_PATH}/TempEX.out 
        file" >> ${LOG}
    
        sort -u  ${FILE_PATH}/TempEX.out > ${DEST_PATH}/myfile_I.out
    
        echo -e "Unique records successfully written into 
        ${DEST_PATH}/myfile_I.out" >> ${LOG}
    
        count=0
        while read
        do
        ((count=$count+1))
        done <${DEST_PATH}/myfile_I.out
        echo -e "Total No of unique records in ${DEST_PATH}/myfile_I.out:" 
        ${count} "\n" >> $LOG
         #-----------------------------------------------------------------#
    
    我做了一些排序函数,以了解myfile_I out中存在的重复项 TempEX.out文件中存在重复记录计数

        $ cut -d'^X' -f1,6,10 TempEX.out|sort|uniq -d|wc -l
        5
    
        $ cut -d'^X' -f1,6,10 TempEX.out|sort|uniq -d|cat
        701234567      412345678        19
        701234568      412345677        18
        709875641      412345859        17
        701234569      425984031        21
        701234570      409845216        20
    
    myfile_I.out文件中的重复记录计数

        $ cut -d'^X' -f1,6,10 myfile_I.out|sort|uniq -d|wc -l
        1
    
        $ cut -d'^X' -f1,6,10 myfile_I.out|sort|uniq -d|cat
        709875641      412345859        17
    
    在TempEX.out文件中获取了哪些记录(在主键上)具有重复项

        $ cut -d'^X' -f1,6,10 TempEX.out|sort|uniq -d|wc -l
        5
    
        $ cut -d'^X' -f1,6,10 TempEX.out|sort|uniq -d|cat
        701234567      412345678        19
        701234568      412345677        18
        709875641      412345859        17
        701234569      425984031        21
        701234570      409845216        20
    
    已获取哪些记录(在主密钥上)在myfile\u I.out文件中具有重复项

        $ cut -d'^X' -f1,6,10 myfile_I.out|sort|uniq -d|wc -l
        1
    
        $ cut -d'^X' -f1,6,10 myfile_I.out|sort|uniq -d|cat
        709875641      412345859        17
    
    预期成果: 要消除TempEX.out文件中的重复项,请将唯一数据加载到myfile_I.out

        sort -u TempEX.out > myfile_I.out /*cant resolving the issue*/
    
    我们可以这样做吗?(在主键上执行)


    下面是一个可能会有所帮助的小脚本。它不会用新数据修改原始文件,而是创建一个要加载的新文件(我总是希望保留原始文件以防出错)。它在主键上进行验证,但将确保在主键重复的情况下,其他列也相同。合理的做法是,即使您没有提及,也可能会修改现有数据或输入系统中的错误。无论如何,脚本会将这些行发送到另一个文件中供用户查看

    它写在注释中,但可以肯定的是,任何列中的字段都不应该有带空格的值

    #!/bin/ksh
    
    TIMESTAMP=$(date +"%Y%m%d%H%M")
    
    #No sense to do anything if the files are not readable.
    if [[ ! -r $1 || ! -r $2 ]]; then
        print "ERROR - You must provide 2 parameters : 1 = path/filename of DB content 2 = path/filename of New Data"
        exit
    fi
    
    #Declaring 2 associative matrix
    typeset -A TableDB
    typeset -A DataToAdd
    
    #Opening the different files. 3 and 4 for reading and 5 and 6 for writting.
    #File handlers : 
    # 3 for the data from the DB, 
    # 4 for the new data to add, 
    # 5 to write the new data to load (unique and new), 
    # 6 to write the data in problem (same primary key but with different values)
    exec 3<$1
    exec 4<$2
    exec 5>Data2Load_${TIMESTAMP}.txt
    exec 6>Data2Verify_${TIMESTAMP}.txt
    
    #Loading the 2 matrix with their data. 
    #Here it is assumed that no field in any column contain blank spaces.
    #Working with only 3 columns as in the example
    while read -u3 a b c && read -u4 d e f; do
            TableDB[$a]=( $a $b $c )
            DataToAdd[$d]=( $d $e $f )
    done
    
    #Checking for duplicate and writting only the new one to load without the lines in possible errors
    for i in ${!DataToAdd[@]}; do
            if [[ -z ${TableDB[$i]} ]]; then
                    print -u5 "${DataToAdd[$i][0]} ${DataToAdd[$i][1]} ${DataToAdd[$i][2]}"
            elif [[ ${DataToAdd[$i][1]} != ${TableDB[$i][1]} || ${DataToAdd[$i][2]} != ${TableDB[$i][2]} ]]; then
                    print -u6 "${DataToAdd[$i][0]} ${DataToAdd[$i][1]} ${DataToAdd[$i][2]}"
            fi
    done
    
    #closing the different files
    exec 3>&-
    exec 4>&-
    exec 5>&-
    exec 6>&-
    
    #/bin/ksh
    时间戳=$(日期+%Y%m%d%H%m)
    #如果文件不可读,那么做任何事情都没有意义。
    如果[!-r$1 |!-r$2]];然后
    打印“错误-必须提供2个参数:1=数据库内容的路径/文件名2=新数据的路径/文件名”
    出口
    fi
    #2-结合矩阵
    排版-表格数据库
    排版-数据添加
    #打开不同的文件。3和4用于阅读,5和6用于书写。
    #文件处理程序:
    #3对于来自数据库的数据,
    #4对于要添加的新数据,
    #5写入要加载的新数据(唯一和新),
    #6写入问题中的数据(相同的主键,但值不同)
    exec 3Data2Verify_${TIMESTAMP}.txt
    #用数据加载2矩阵。
    #这里假设任何列中的字段都不包含空格。
    #仅使用示例中的3列
    读取-u3 a b c和读取-u4 d e f;做
    TableDB[$a]=($a$b$c)
    数据添加[$d]=($d$e$f)
    完成
    #检查是否存在重复项,并仅写入要加载的新项,而不会导致可能的错误
    对于${!DataToAdd[@]}中的i;做
    如果[[-z${TableDB[$i]}]];然后
    打印-u5“${DataToAdd[$i][0]}${DataToAdd[$i][1]}${DataToAdd[$i][2]}”
    elif[${DataToAdd[$i][1]}!=${TableDB[$i][1]}| |${DataToAdd[$i][2]}!=${TableDB[$i][2]];然后
    打印-u6“${DataToAdd[$i][0]}${DataToAdd[$i][1]}${DataToAdd[$i][2]}”
    fi
    完成
    #关闭不同的文件
    执行官3>&-
    执行官4>&-
    执行官5>&-
    执行官6>&-
    

    希望有帮助

    这里有一个小脚本可能会有所帮助。它不会用新数据修改原始文件,而是创建一个要加载的新文件(我总是希望保留原始文件以防出错)。它在主键上进行验证,但将确保在主键重复的情况下,其他列也相同。合理的做法是,即使您没有提及,也可能会修改现有数据或输入系统中的错误。无论如何,脚本会将这些行发送到另一个文件中供用户查看

    它写在注释中,但可以肯定的是,任何列中的字段都不应该有带空格的值

    #!/bin/ksh
    
    TIMESTAMP=$(date +"%Y%m%d%H%M")
    
    #No sense to do anything if the files are not readable.
    if [[ ! -r $1 || ! -r $2 ]]; then
        print "ERROR - You must provide 2 parameters : 1 = path/filename of DB content 2 = path/filename of New Data"
        exit
    fi
    
    #Declaring 2 associative matrix
    typeset -A TableDB
    typeset -A DataToAdd
    
    #Opening the different files. 3 and 4 for reading and 5 and 6 for writting.
    #File handlers : 
    # 3 for the data from the DB, 
    # 4 for the new data to add, 
    # 5 to write the new data to load (unique and new), 
    # 6 to write the data in problem (same primary key but with different values)
    exec 3<$1
    exec 4<$2
    exec 5>Data2Load_${TIMESTAMP}.txt
    exec 6>Data2Verify_${TIMESTAMP}.txt
    
    #Loading the 2 matrix with their data. 
    #Here it is assumed that no field in any column contain blank spaces.
    #Working with only 3 columns as in the example
    while read -u3 a b c && read -u4 d e f; do
            TableDB[$a]=( $a $b $c )
            DataToAdd[$d]=( $d $e $f )
    done
    
    #Checking for duplicate and writting only the new one to load without the lines in possible errors
    for i in ${!DataToAdd[@]}; do
            if [[ -z ${TableDB[$i]} ]]; then
                    print -u5 "${DataToAdd[$i][0]} ${DataToAdd[$i][1]} ${DataToAdd[$i][2]}"
            elif [[ ${DataToAdd[$i][1]} != ${TableDB[$i][1]} || ${DataToAdd[$i][2]} != ${TableDB[$i][2]} ]]; then
                    print -u6 "${DataToAdd[$i][0]} ${DataToAdd[$i][1]} ${DataToAdd[$i][2]}"
            fi
    done
    
    #closing the different files
    exec 3>&-
    exec 4>&-
    exec 5>&-
    exec 6>&-
    
    #/bin/ksh
    时间戳=$(日期+%Y%m%d%H%m)
    #如果文件不可读,那么做任何事情都没有意义。
    如果[!-r$1 |!-r$2]];然后
    打印“错误-必须提供2个参数:1=数据库内容的路径/文件名2=新数据的路径/文件名”
    出口
    fi
    #2-结合矩阵
    排版-表格数据库
    排版-数据添加
    #打开不同的文件。3和4用于阅读,5和6用于书写。
    #文件处理程序:
    #3对于来自数据库的数据,
    #4对于要添加的新数据,
    #5写入要加载的新数据(唯一和新),
    #6写入问题中的数据(相同的主键,但值不同)
    exec 3Data2Verify_${TIMESTAMP}.txt
    #用数据加载2矩阵。
    #这里假设任何列中的字段都不包含空格。
    #仅使用示例中的3列
    读取-u3 a b c和读取-u4 d e f;做
    TableDB[$a]=($a$b$c)
    数据添加[$d]=($d$e$f)
    完成
    #检查是否存在重复项,并仅写入要加载的新项,而不会导致可能的错误
    对于${!DataToAdd[@]}中的i;做
    如果[[-z${TableDB[$i]}]];然后
    打印-u5“${DataToAdd[$i][0]}${DataToAdd[$i][1]}${DataToAdd[$i][2]}”
    elif[${DataToAdd[$i][1]}!=${TableDB[$i][1]}| |${DataToAdd[$i][2]}!=${TableDB[$i][2]];然后
    打印-u6“${DataToAdd[$i][0]}${DataToAdd[$i][1]}${DataToAdd[$i][2]}”
    fi
    完成
    #关闭不同的文件
    执行官3>&-
    执行官4>&-
    执行官5>&-
    执行官6>&-
    

    希望有帮助

    太多的信息,但投票,因为你真的试图解决你的问题,你已经包括代码!是的,您可以在对文件进行排序时使用
    ,将其减少为唯一值,但要对其进行疯狂的测试,因为结果通常不会像您预期/希望的那样工作。正确的语法是
    sort-t{yourFieldChar}-k1+k1-k6