首页 > 解决方案 > 替换分号后的字符串

问题描述

我有一个filtered_content包含以下内容的文件:

ip-172-31-42324-162.sa-east-1.compute.internal;2021-06-26T05:07:30Z
ip-172-31-42324-162.sa-east-1.compute.internal;2021-10-12T05:07:30Z
ip-172-31-4234-163.sa-east-1.compute.internal;2021-03-02T05:07:30Z
ip-172-31-4234-163.sa-east-1.compute.internal;2021-05-26T05:07:30Z

和另一个converted_data包含内容的文件:

1624684050
1634015250
1614661650
1622005650

我想用这样filtered_content的内容替换分号后的字符串converted_data

ip-172-31-42324-162.sa-east-1.compute.internal;1624684050
ip-172-31-42324-162.sa-east-1.compute.internal;1634015250
ip-172-31-4234-163.sa-east-1.compute.internal;1614661650
ip-172-31-4234-163.sa-east-1.compute.internal;1622005650

我试过这样的东西,但没有用。

data=$(cat /tmp/converted_data)
cat /tmp/filtered_content | sed 's/;.*//' | sed 's/.${data};//'

标签: bashawksed

解决方案


使用pasteandcutProcess Substitution的快速方法。

paste -d';' <(cut -d';' -f1 filtered_content.txt) converted_content.txt

根据@oguzismail 的评论,不需要流程替换。

cut -d ';' -f1 filtered_content.txt | paste -d';' - converted_content.txt

推荐阅读