bash - 替换分号后的字符串
问题描述
我有一个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};//'
解决方案
使用paste
andcut
和Process Substitution的快速方法。
paste -d';' <(cut -d';' -f1 filtered_content.txt) converted_content.txt
根据@oguzismail 的评论,不需要流程替换。
cut -d ';' -f1 filtered_content.txt | paste -d';' - converted_content.txt
推荐阅读
- python - 使用 lxml Etree 在 python 中更新 xml 标签
- javascript - 如何使用地图坐标和比例绘制多边形画布
- java - 使用 getrealmetrics 阅读时屏幕尺寸错误
- javascript - 如何在更改 DropDownMenu 时通过 Jquery 使 jqGrid 动态化?
- java - 如何在 Spring Boot Rest API 中以 XML 形式返回对象列表
- sql - 如何找到两个日期之间的条目?
- javascript - Html画布不适用于简单网页中的电子签名
- python - 将字典的每个键值存储为一个单独的 numpy 数组,以键名作为其名称,并将其连接成一个大数组
- html - 带间隙的 HTML 背靠背图像
- twig - 自定义页面错误不起作用。(在生产中总是返回代码 500)