bash 在linux脚本中有条件地添加或附加到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13007672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:36:16  来源:igfitidea点击:

Conditionally add or append to a file in linux script

linuxbashunixscripting

提问by Jim

I need to modify a file via a script.
I need to do the following:
IF a specific string does NOT exist then append it.

我需要通过脚本修改文件。
我需要执行以下操作:
如果特定字符串不存在,则附加它。

So I created the following script:

所以我创建了以下脚本:

#!/bin/bash  
if grep -q "SomeParameter A" "./theFile"; then  
echo exist  
else  
   echo doesNOTexist  
   echo "# Adding parameter" >> ./theFile    
   echo "SomeParameter A" >> ./theFile    
fi

This works but I need to make some improvements.
I think it would be better if I checked if "SomeParameter" exists and then see if it is followed by "A" or "B". If it is "B" then make it "A".
Otherwise append the string (like I do) BUT BEFORE the start of the last block of comments.
How could I do this?
I am not good in scripting.
Thanks!

这有效,但我需要进行一些改进。
我认为如果我检查“SomeParameter”是否存在,然后查看它后面是“A”还是“B”会更好。如果是“B”,则将其设为“A”。
否则在最后一个注释块开始之前附加字符串(像我一样)。
我怎么能这样做?
我不擅长脚本。
谢谢!

回答by l0b0

First, change any SomeParameterlines if they already exist. This should work with lines like SomeParameteror SomeParameter B, with any number of extra spaces:

首先,更改任何SomeParameter已存在的行。这应该适用于像SomeParameter或这样的行SomeParameter B,带有任意数量的额外空格:

sed -i -e 's/^ *SomeParameter\( \+B\)\? *$/SomeParameter A/' "./theFile"

Then add the line if it doesn't exist:

如果它不存在,则添加该行:

if ! grep -qe "^SomeParameter A$" "./theFile"; then
    echo "# Adding parameter" >> ./theFile    
    echo "SomeParameter A" >> ./theFile    
fi

回答by Vijay

awk 'BEGIN{FLAG=0}
     /parameter a/{FLAG=1}
     END{if(flag==0){for(i=1;i<=NR;i++){print}print "adding parameter#\nparameter A#"}}' your_file

BEGIN{FLAG=0}-initializing a flag variable before the start of the file processing.

BEGIN{FLAG=0}- 在文件处理开始之前初始化标志变量。

/parameter a/{FLAG=1}-setting the flag if the parameter is found in the file.

/parameter a/{FLAG=1}- 如果在文件中找到参数,则设置标志。

END{if(flag==0){for(i=1;i<=NR;i++){print}print "adding parameter#\nparameter A#"}}-finally adding the lines at the end of the file

END{if(flag==0){for(i=1;i<=NR;i++){print}print "adding parameter#\nparameter A#"}}- 最后在文件末尾添加行

回答by Nahuel Fouilleul

A perl one-liner

一个 perl 单行

perl -i.BAK -pe 'if(/^SomeParameter/){s/B$/A/;$done=1}END{if(!$done){print"SomeParameter A\n"}} theFile

a backup theFile.BAK will be created (-i option). A more verbose version, which takes into account the last comments, to be tested. Should be saved in a text file and executed perl my_script.plor chmod u+x my_script.pl./my_script.pl

将创建备份 theFile.BAK(-i 选项)。一个更详细的版本,它考虑到了最后的评论,有待测试。应保存在文本文件中并执行perl my_script.plchmod u+x my_script.pl./my_script.pl

#!/usr/bin/perl

use strict;
use warnings;

my $done = 0;
my $lastBeforeComment;
my @content = ();
open my $f, "<", "theFile" or die "can't open for reading\n$!";
while (<$f>) {
  my $line = $_;
  if ($line =~ /^SomeParameter/) {
    $line =~ s/B$/A/;
    $done = 1;
  }
  if ($line !~ /^#/) {
    $lastBeforeComment = $.
  }
  push @content, $line;
}
close $f;
open $f, ">", "theFile.tmp" or die "can't open for writting\n$!";
if (!$done) {
  print $f @content[0..$lastBeforeComment-1],"SomeParameter A\n",@content[$lastBeforeComment..$#content];
} else {
  print $f @content;
}
close $f;

once it is ok then add the following:

一旦确定,然后添加以下内容:

rename "theFile.tmp", "theFile"