我怎么能基于匹配重点领域用从另一个文件行的文本文件行? [英] How can I replace lines in a text file with lines from another file based on matching key fields?

查看:84
本文介绍了我怎么能基于匹配重点领域用从另一个文件行的文本文件行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

input.txt的

1,Ram,Fail
2,John,Fail
3,Ron,Success

param.txt(新输入)

1,Sam,Success
2,John,Sucess

现在我想在param.txt那些present替换input.txt的整行。
第一列会像一个主键。

Now i want to replace the whole line in input.txt with those present in param.txt . 1st column will act like a primary key.

Output.txt的

1,Sam,Success
2,John,Sucess
3,Ron,Success

我想为

awk 'FNR==NR{a[$1]=$2 FS $3;next}{ print $0, a[$1]}' input.txt param.txt > Output.txt 

但它合并该文件的内容。

But it is merging the file contents.

推荐答案

这可能会为你工作(GNU SED):

This might work for you (GNU sed):

 sed 's|^\([^,]*,\).*|/^\1/c\\&|' param.txt | sed -f - input.txt

说明:


  • 使用的第一个字段作为地址来更改线路转换 param.txt SED 脚本 input.txt的 s ^ | ^ \\([^,] * \\)* | / ^ \\ 1 / C \\\\&放大器; |

  • 运行input.txt的对的脚本。 SED -f - input.txt的

  • Convert param.txt into a sed script using the first field as an address to change the line in the input.txt. s|^\([^,]*,\).*|/^\1/c\\&|
  • Run the script against the input.txt. sed -f - input.txt

这篇关于我怎么能基于匹配重点领域用从另一个文件行的文本文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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