FILE1.TXT
[fields:WinSpc:defect]
a=b
b=c
hello=hi
[fields:ROCKET PROJECT:ticket]
description=Descrtiption
status=status
[fields:PROJECT_Nexus:defect]
title=summary
priority=Priority_hello
file2.csv
WinSpc,projects.winspc
ROCKET PROJECT,projects.rocket_project
PROJECT_Nexus,projects.project-nexus
我需要匹配这两个文件,所需的输出将是:
output.txt的
[fields:winspc:defect]
a=b
b=c
hello=hi
[fields:rocket_project:ticket]
description=Descrtiption
status=status
[fields:project-nexus:defect]
title=summary
priority=Priority_hello
只需更改名称,
我尝试过使用
grep -Fwf, diff --breif,
和awk选项但没有获得所需的输出。还在学习这些东西。任何建议都会非常有帮助。提前谢谢。
答案 0 :(得分:2)
可以执行更具伸缩性的Awk
逻辑,如下所示。
重新确认对未来读者的要求,
.csv
文件有多行field,replacement-of-field
1 对。对于field
中的所有.csv
.txt
},replacement-of-field
文件中的相应条目应替换为awk 'FNR==NR{split($2,list,"."); replacement[$1]=list[2]; next} \ {for (i in replacement){ if (match($0,i)) {gsub(i,replacement[i],$0); break} }}1 ' \ FS="," file2.csv file1.txt
<子> 1. replcement-of-field 实际上只涉及点之后的部分 子>
以下命令按预期完成工作。
OP
生成所需的[fields:winspc:defect]
a=b
b=c
hello=hi
[fields:rocket_project:ticket]
description=Descrtiption
status=status
[fields:project-nexus:defect]
title=summary
priority=Priority_hello
输出,
FNR==NR
提出一些解释,
{}
逻辑确保.csv
内.csv
之后的命令首先针对,
文件运行。请注意,使用字段分隔符split($2,list,".");replacement[$1]=list[2]; next
.
文件
.csv
确保文件的第二列被.txt
拆分,并创建一个散列映射,索引设置为要替换的值,并将值作为要替换的实际值。这是针对parseInt
文件Number
文件中,检查每一行是否存在要替换的值,如果存在,则替换为替换值。答案 1 :(得分:1)
S
单行:
any
工作原理:
将 file2.csv 转换为sed
s ubstitute 命令。所以初始代码
sed 's#,projects.#/#;s#.*#/fields/s/&/\;#' file2.csv | sed -f - file1.txt
输出:
sed
在 file1.txt 上运行生成的替换命令。
输出:
sed 's#,projects.#/#;s#.*#/fields/s/&/\;#' file2.csv
答案 2 :(得分:0)
考虑到你的评论,这看起来是一个练习来替换txt文件中fields:
后面的值,使用来自另一个文件的替换值,其中&#34;字段&#34;价值是一种关键。
看看这种方法:
$ readarray -t a < <(grep -e "\[fields:" a.txt |cut -d: -f2)
$ for ((i=0;i<${#a[@]};i++));do a[i]=s/${a[i]}/$(grep -e "${a[i]}" b.txt |cut -d, -f2 |cut -d. -f2)/g\;;done
$ sed -f <(echo "${a[@]}") a.txt
输出:
[fields:winspc:defect]
a=b
b=c
hello=hi
[fields:rocket_project:ticket]
description=Descrtiption
status=status
[fields:project-nexus:defect]
title=summary
priority=Priority_hello
说明:
# grep the first file a.txt for all the fields: and keep the second part, i.e WinScp . Store all those findings in an array
$ readarray -t a < <(grep -e "\[fields:" a.txt |cut -d: -f2)
$ declare -p a #print the array to see what is inside
#Bash Output: declare -a a=([0]="WinSpc" [1]="ROCKET PROJECT" [2]="PROJECT_Nexus")
# Iterate through the array and with the stored value (i.e WinSpc) grep the second file
# (b.txt in my test) and get the second field after comma. Store changes in the same array.
$ for ((i=0;i<${#a[@]};i++));do a[i]=s/${a[i]}/$(grep -e "${a[i]}" b.txt |cut -d, -f2 |cut -d. -f2)/g\;;done
$ declare -p a #print the array again. Now array looks like a sed pattern.
# Bash Output: declare -a a=([0]="s/WinSpc/winspc/g;" [1]="s/ROCKET PROJECT/rocket_project/g;" [2]="s/PROJECT_Nexus/project-nexus/g;")
# We can then apply all the sed patterns stored in array to replace values of text file (a.txt)
$ sed -f <(echo "${a[@]}") a.txt
使用AWK等的其他解决方案可以提供更高效的代码。