shell - 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 here
he 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
解决方案
尝试这个:
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
推荐阅读
- spring-mvc - `mock.Mvc.perform()` 导致 `NullPointerException`
- html - 单击覆盖 div 内的内容 div 也会关闭弹出窗口
- c++ - 在 after 变量中应该在哪里使用 ULL?
- kubernetes - Docker Desktop 上的 Kubernetes - 使用两个 pod 之间的服务的本地连接问题
- typescript - 如果在打字稿中传递可选参数,则更改函数返回类型
- sql - 在 SQLite 中查询嵌套列
- parameters - 我可以让 NVCC 在 PTX 中使用实际参数名称吗?
- vba - 需要说明:vba find方法故障排除
- django - 如何在 django-ckeditor 中使用引导程序?
- excel - Excel VBA反复删除左上角特定字符串的范围