如何在前两行的双引号末尾添加属性

时间:2018-08-13 20:04:08

标签: linux bash perl awk sed

我们要添加以下属性:

-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M

在包含单词- HBASE_MASTER_OPTS

的前两行中

更改前的示例

more ams-hbase-env-template.txt


export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"

更改后的示例(预期结果)

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"
export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"

到目前为止,我所做的是

sed -i '/HBASE_MASTER_OPTS/ s/$/-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/' ams-hbase-env-template.txt

但这不是正确的

  1. 我们希望属性在双引号结束之前

  2. 属性必须仅在变量为HBASE_MASTER_OPTS的前两行

  3. 如果属性已经在行中,那么它将不会在下一次再次添加!

3 个答案:

答案 0 :(得分:2)

这可能对您有用(GNU sed):

sed '/HBASE_MASTER_OPTS/{x;s/^/x/;/^x\{1,2\}$/{x;/-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/b;s/"\s*$/ -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"/;x};x}' file

在包含HBASE_MASTER_OPTS的行上,在保留空间中增加一个计数器,如果该计数器包含1或2,并且不包含字符串-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M,则将最后一个"替换为{ {1}},否则不执行任何操作。

答案 1 :(得分:1)

也许这会给您一些启发:

use strict;
use warnings;

my $i = 0;

while (<>) {
    if ($i < 2 && /HBASE_MASTER_OPTS/ && !/UseGCLogFileRotation/) {
        s/"$/ -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"/;
        $i++;
    }

    print;
}

用法:

$ perl a.pl foo.txt

答案 2 :(得分:1)

awk '/HBASE_MASTER_OPTS/&&/ -XX:\+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/{x++;print;next}/HBASE_MASTER_OPTS/&&x<2{gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M\"");x++}1' ams-hbase-env-template.txt

测试:

 awk '/HBASE_MASTER_OPTS/&&/ -XX:\+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M/{x++;print;next}/HBASE_MASTER_OPTS/&&x<2{gsub(/"$/," -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M\"");x++}1' example.txt



export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=70 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=71 -XX:+UseCMSInitiatingOccupancyOnly -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=2M"

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=72 -XX:+UseCMSInitiatingOccupancyOnly"

export HBASE_MASTER_OPTS=" -XX:CMSInitiatingOccupancyFraction=73 -XX:+UseCMSInitiatingOccupancyOnly"