首页 > 解决方案 > Read line by line column exculding header from Processed_files.csv and search each line in Responsed_File_Name.csv

问题描述

I want to read below two csv file line by line (first column) and write it to another report.csv. Take the string from first column without extension (.json) and search the string to another file without extension (_SUCCESS.csv,_ERROR.csv,INPROCESS.csv) and if found update tenter code herehe report.csv with input file name and output file name with status (SUCCESS,ERROR,INPROCESS,Not_Found)

    **Input files**

    **Processed_files.csv**

    **Input File Name,Start Date**
    Out_ch_pc_88882.77.json,2018/10/26  16.38.54  
    Out_pc_fh_42652.48.json,2018/10/26  16.38.54  
    Out_pc_kl_55684.37.json,2018/10/26  17.38.54  
    Out_pc_gl_34454.66.json,2018/10/27  12.38.54  


    **Responsed_File_Name.csv**


    **Output File Name,End Time/Date**

    Out_ch_pc_88882.77_SUCCESS.csv,2018/10/30  14.53.26
    Out_pc_fh_42652.48_ERROR.csv,2018/10/26  17.40.44
    Out_pc_kl_55684.37_INPROCESS.csv,2018/10/26  18.55.14

    **Output files**

    **Input File Name,Start Date,Output File Name,End Time/Date,Status**

    Out_ch_pc_88882.77.json,2018/10/26  16.38.54,Out_ch_pc_88882.77_SUCCESS.csv,2018/10/30  14.53.26,SUCCESS
    Out_pc_fh_42652.48.json,2018/10/26  16.38.54,Out_pc_fh_42652.48_ERROR.csv,2018/10/26  17.40.44.ERROR
    Out_pc_kl_55684.37.json,2018/10/26  17.38.54,Out_pc_kl_55684.37_INPROCESS.csv,2018/10/26  18.55.14,INPROCESS
    Out_pc_gl_34454.66.json,2018/10/27  12.38.54,,Not_Found


    I tried below script unable get thru.
    Request your kind help. 


Processed_File=/test/Processed_Files.csv
ReportFile=Report.csv
LoopCounter=0
while IFS=, read -r f1 f2
do
if [ $LoopCounter != 0 ]
then 
printf '%s' $f1 >> $ReportFile
                printf '%s' , >> $ReportFile
printf '%s' $f2 >> $ReportFile
                printf '%s' , >> $ReportFile
                printf '%s' , >> $ReportFile
                printf '%s\n' , >> $ReportFile
                #printf '\n'  >> $ReportFile
else
                printf '%s' Input File >> $ReportFile
                printf '%s' , >> $ReportFile
                printf '%s' Start Date >> $ReportFile
                printf '%s' , >> $ReportFile
                printf '%s' Response File Name>> $ReportFile
                printf '%s' , >> $ReportFile
                printf '%s' End Date >> $ReportFile
                printf '%s' , >> $ReportFile
                printf '%s\n' Status>> $ReportFile
fi 
let LoopCounter=LoopCounter+1
done <$Processed_File

标签: shell

解决方案


尝试这个:

awk -F',' 'NR==FNR&&NR>1{split($1,b,"."); split(b[2],status,"_"); map[b[1]"."status[1]]=$0","status[2]} NR!=FNR&&FNR>1{split($1,a,"."); search_string=a[1]"."a[2]; if(search_string in map) {map[search_string]=$0","map[search_string]} else {map[search_string]=$0",,Not_Found"}} END{print "Input File Name,Start Date,Output File Name,End Time/Date,Status"; for (i in map) print map[i]}' Responsed_File_Name.csv Processed_files.csv > Report.csv

推荐阅读