Shell 将较短的表单转换为完整表单
输入文件1:Shell 将较短的表单转换为完整表单,shell,Shell,输入文件1: IT : Information Technology B.Tech : Graduation CS : computer science 输入文件2: B.Tech has the different groups. One of several groups is IT and it has CS. Most of the students are selecting these two groups. oputput应该类似于: 将输入文件1的完整格式替换为输入文件2
IT : Information Technology
B.Tech : Graduation
CS : computer science
输入文件2:
B.Tech has the different groups.
One of several groups is IT and it has CS.
Most of the students are selecting these two groups.
oputput应该类似于:
将输入文件1的完整格式替换为输入文件2
任何人都可以在此基础上使用awk/sed帮助创建shell脚本
awk -F' : ' 'NR==FNR{a[$1]=$2;next}{for(x in a)gsub(x,a[x])}1' file1 file2
产出将是:
kent$ awk -F' : ' 'NR==FNR{a[$1]=$2;next}{for(x in a)gsub(x,a[x])}1' file1 file2
Graduation has the different groups.one of several groups is Information Technologyand it has computer science.Most of the students selecting these two groups.
但是有一个问题需要注意:
递归替换问题
例如:
CS : computer science (CS)
这会发生吗?应该怎么做?使用sed听起来是个不错的计划——正如@Fredrik所说,您应该尝试一下,并向我们展示您所拥有的。输入和输出文件真的只有一行吗?我认为您应该插入一些换行符,使其可读性和可理解性。awk'FNR==NR{array[$1]=$2;next}{for(数组中的I)gsub(I,数组[I]))1'master.txt file.txti尝试了上面的代码…它不起作用…输入文件不是一行…3行…它不会包含第二行中第一列的较短形式…正如您所说,我需要在您的示例中尝试…感谢这实际上是另一个潜在问题。替换顺序。比如说,
fb:Foo-Bar
和'Foo:whatever`你会如何处理Foo
?不确定…我正在学习阶段…尝试不同的选项…你能不能建议相同的…@siddi我不能建议,这是你的要求,你应该确切知道。