linux - 用另一个列表中的下一行替换列表中的字符串
问题描述
我有 3 个文件。一个是食物和类别的列表:
food="rice"
product="cereal"
food="beans"
product="bean"
food="cake"
product="bakery"
food="lettuce"
product="leaves"
第二个是仅列出食物的清单:
food="rice"
food="beans"
food="cake"
food="lettuce"
在第三个文件中,我有包含 /food 文件字符串的行(例如 /food="rice"),我需要将这些字符串替换为第一个文件中列出的相应产品。为了简化:在文件 3 上从文件 2 中找到字符串,并用文件 3 上文件 1 的下一行替换。我想可能是 grep 和 sed 的组合,但我不知道如何... 第三个文件看起来像这样的东西
>[food="rice"] [some other sutff] [calories=398]
Here is a recipe with rice
>[food="beans"] [some other sutff] [calories=250]
Here is a recipe with beans
>[food="cake"] [some other sutff] [calories=100]
Here is a recipe for cake
>[food="lettuce"] [some other sutff] [calories=02]
Why would you need a recipe for lettuce?
我需要它看起来像......
>[product="cereal"] [some other sutff] [calories=398]
Here is a recipe with rice
>[product="bean"] [some other sutff] [calories=250]
Here is a recipe with beans
>[product="bakery" [some other sutff] [calories=100]
Here is a recipe for cake
>[product="leaves"] [some other sutff] [calories=02]
Why would you need a recipe for lettuce?
解决方案
这是使用的解决方案sed
:
sed -f <(sed 'N;s/\n/\//;s/^/s\//;s/$/\//' one) three
如果第一个文件的每一行都以空格开头,那就变成
sed -f <(sed 'N;s/ *//g;s/\n/\//;s/^/s\//;s/$/\//' one ) three
推荐阅读
- card - MSE-Set AT - 如何确定公钥的引用
- waf - 无法安装 minindn 项目,面临一些 waf 错误
- python - Visual Studio Code 奇怪的错误,在定义时显示来自另一个文件的未定义错误
- sql - 如何对员工进行工资限制?
- visual-studio-code - 如何防止创建 .dccache 文件?
- php - 错误 Prepared statement需要重新准备
- javascript - 异步函数抛出的异常没有被 catch 块捕获
- php - 我在 Couchbase 中收到此错误:'Pooled socket is dead。继续下一个'
- pine-script - 当价格向前移动了确定数量的点数而没有或在返回之前突出显示区域
- reactjs - 如何让 Nextjs 在保存时显示打字稿类型错误