bash脚本在逗号后附加数据 [英] bash script appending data after comma

查看:70
本文介绍了bash脚本在逗号后附加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的测试文件.

I have a test file with following contents.

[groups]

test_read =  apple,orange
write = grapes,mango
[TEST:/]
@test_read= apple,orange
@write= grapes,mango

我需要在[TEST:/]以上的groups部分下的所有字段上添加树莓一词

I need to add the word raspberry on all the fields under groups section which is above [TEST:/]

预期产量

[groups]

    test_read =  apple,orange,raspberry
    write = grapes,mango,raspberry
    [TEST:/]
    @test_read= apple,orange
    @write= grapes,mango

我尝试过使用这种衬垫

awk 'NF > 1 {$NF = $NF ",raspberry"} 1' test

但是它在所有字段上添加了树莓.

But it is adding raspberry on all the fields.

推荐答案

awk '/\[groups/{a=1;print;next} /^\[/{a=0}a && /=/{$0=$0",raspberry"}7' file

无论groups块位于何处,此行均有效. (例如,在TEST块的上方或下方).

This line works no matter where is the groups block located. (E.g. is above or under the TEST block).

这篇关于bash脚本在逗号后附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆