首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将列表中的字符串替换为另一个列表中的下一行

将列表中的字符串替换为另一个列表中的下一行
EN

Stack Overflow用户
提问于 2018-08-22 02:47:31
回答 3查看 81关注 0票数 0

我有3个文件。一个是食物和类别的列表:

代码语言:javascript
复制
                 food="rice"
                 product="cereal"
                 food="beans"
                 product="bean"
                 food="cake"
                 product="bakery"
                 food="lettuce"
                 product="leaves"

第二个是只列出食物的列表:

代码语言:javascript
复制
                 food="rice"
                 food="beans"
                 food="cake"
                 food="lettuce"

在第三个文件中,我有包含大米文件字符串的行(例如,/food=“/food”),我需要将这些字符串替换为第一个文件中列出的相应产品。为了简化:在文件3中找到文件2中的字符串,并替换为文件3中文件1的下一行。我想可能是grep和sed的组合,但我不知道如何...第三个文件如下所示

代码语言:javascript
复制
>[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?

我需要它看起来像..。

代码语言:javascript
复制
 >[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?
EN

回答 3

Stack Overflow用户

发布于 2018-08-22 15:11:37

这是一个使用sed的解决方案

代码语言:javascript
复制
sed -f <(sed 'N;s/\n/\//;s/^/s\//;s/$/\//' one) three

如果第一个文件的每一行都以空格开头,那么就变成了

代码语言:javascript
复制
sed -f <(sed 'N;s/ *//g;s/\n/\//;s/^/s\//;s/$/\//' one ) three
票数 1
EN

Stack Overflow用户

发布于 2018-08-22 03:36:06

下面是一个sed/awk组合

代码语言:javascript
复制
$ sed -f <(awk '     {gsub(/ /,"")} 
             NR==FNR {if(/food/) k=$0; if(/product/) a[k]=$0; next} 
             $0 in a {print "s/" $0 "/" a[$0] "/g"}' f1 f2) f3

>[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?
票数 0
EN

Stack Overflow用户

发布于 2018-08-22 19:53:09

我不明白file2会被用来做什么。

代码语言:javascript
复制
$ cat tst.awk
BEGIN { FS="[][]" }
NR==FNR {
    gsub(/^[[:space:]]+|[[:space:]]+$/,"")
    if (NR%2) { food = $0 }
    else { map[food] = $0 }
    next
}
{
    sub(/\[[^][]+/,"["map[$2])
    print
}

$ awk -f tst.awk file1 file3
>[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?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51954936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档