比较.txt和.csv文件,并使用引用名称替换出现 [英] Compare a .txt and .csv file and replace the occurences with reference names

查看:265
本文介绍了比较.txt和.csv文件,并使用引用名称替换出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

File1.txt

  [fields:WinSpc:defect] 
a = b
hello = hi

[fields:ROCKET PROJECT:ticket]
description = Descrtiption
status = status

[fields:abl_tja1146:autoticket]
= description
status = status

[fields:abl_tja1146:risk]
description = description
assignedToUsername = assigned_to

file2.csv

  WinSpc,projects.winspc 
ROCKET PROJECT,projects.rocket_project
TJA1146,projects.abl_tja1146

以下:



output.txt或原始txt文件可以附加(file1.txt)

  [fields:winspc:defect] 
a = b
hello = hi

[fields:rocket_project:ticket]
description = Descrtiption
status = status

作为字符串(abl_tja1146)之一是完美的,



这是一个扩展

解决方案

Awk 逻辑如下将为您完成这项工作。

  awk'FNR == NR {split($ 2,list,。); replacement [$ 1] = list [2]; next} \ 
$ 0〜/ ^ \ [/ {split($ 0,newlist,:); if(newlist [2]!〜/ ^ [[:lower:]] /)\
{gsub(newlist [2],replacement [newlist [2]],$ 0);打印; \
while(getline&& $ 0!=){print} print}}'FS =,file2.txt file1.txt


$ b :winspc:defect]
a = b
hello = hi

[fields:rocket_project:ticket]
description = Descrtiption
status = status

要替换的值的哈希映射,实际值是从 file2 ,然后在 file1 上,对以<$ c开头的行执行 regex $ c> [,第二个字段被检查为字符串是完美的(在你的例子中的小写)如果这样,后面的行被丢弃,否则他们被打印出来。


File1.txt

[fields:WinSpc:defect]
a=b
hello=hi

[fields:ROCKET PROJECT:ticket]
description=Descrtiption
status=status

[fields:abl_tja1146:autoticket]
description=description
status=status

[fields:abl_tja1146:risk]
description=description
assignedToUsername=assigned_to

file2.csv

WinSpc,projects.winspc
ROCKET PROJECT,projects.rocket_project
TJA1146,projects.abl_tja1146

Desired output is below :

output.txt or the original txt file can be appended (file1.txt)

[fields:winspc:defect]
a=b
hello=hi

[fields:rocket_project:ticket]
description=Descrtiption
status=status

As one of the string(abl_tja1146) is perfect no need to match them and should be ignored.

This is an extension from an earlier question, but to handle the case when the fields should be ignored.

解决方案

An Awk logic as below will do the job for you.

awk 'FNR==NR{split($2,list,"."); replacement[$1]=list[2]; next} \
   $0 ~ /^\[/{split($0,newlist,":"); if(newlist[2] !~ /^[[:lower:]]/) \
      {gsub(newlist[2],replacement[newlist[2]],$0); print; \
          while(getline && $0 != ""){print} print ""} }' FS="," file2.txt file1.txt

produces an output as needed.

[fields:winspc:defect]
a=b
hello=hi

[fields:rocket_project:ticket]
description=Descrtiption
status=status

A hash-map of the value to be replaced and the actual value is created from file2, then on file1, a regex match is performed on lines starting with [ and the second field is checked for string is perfect (lowercase in your example) if so that and the subsequent lines are discarded, else they are printed as they appear.

这篇关于比较.txt和.csv文件,并使用引用名称替换出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆