gpt4 book ai didi

linux - 用另一个列表中的下一行替换列表中的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:50 25 4
gpt4 key购买 nike

我有 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

关于linux - 用另一个列表中的下一行替换列表中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51954936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com