【问题标题】:To add a string before and after, if the specific string is matched between patterns using sed in bash要在 bash 中使用 sed 在模式之间匹配特定字符串,则在前后添加字符串
【发布时间】:2019-11-17 02:08:05
【问题描述】:

问题:如果指定的字符串在bash中使用sed在模式之间匹配,要在前后添加一个字符串??

在下面的代码中,我想在 object Host "kali" { 上面添加 /* 一行,并在 } 出现后的下一行添加 */ (不要到最后一次出现 })。

这是我的代码

object Host "linux" {
import "windows"
address = "linux"
groups = ["linux"]
}


object Host "kali" {
import "linux"
address = "linux"
groups = [linux ]
}


object Host "windows" {
import "linux"
address = "linux"
groups = ["windows" ]
}

这是预期的输出:

object Host "linux" {
import "windows"
address = "linux"
groups = ["linux"]
}

/*
object Host "kali" {
import "linux"
address = "linux"
groups = [linux ]
}
*/

object Host "windows" {
import "linux"
address = "linux"
groups = ["windows" ]
}

**This is what I tried**


#! /bin/bash
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[0;32m'
clear
echo -e  "Enter the names to comment in config file"
cat > comment-file.txt
clear
echo -e "#################################################################################"
echo "Please wait. The names will be commented shortly............"
echo -e "#################################################################################"
echo "Dont press any button, Please hold on...."
while read -r names
do
loc=$(grep -il "object.*Host.*\"$names.*\"" /home/jo/folders/test-sc/*.txt)
if [ -z $loc ]
then
        echo -e " $names$RED No Object definition found $NONE "
else
sed -i '/object Host \"$names.*\" {/ { s,^,/*\n,
    : loop
    /}/ {
        s,$,\n*/,
        p
        d
    }
    N
    b loop
}' "$loc"

  echo -e " $names - $loc -   $GREEN Object host defenition commented $NONE "
fi
done < comment-file.txt
echo -e "#################################################################################"
echo -e "\t\t\t\t Script completed \t\t\t\t"
echo -e "#################################################################################"

rm -rf comment-file.txt

错误

没有对输出文件进行任何更改,这意味着 /home/jo/folders/test-sc/*.txt

【问题讨论】:

  • 通过查看您的个人资料得知您从未选择过正确答案,请求您在某个时间给出答案,当您对您的问题有一些答案时,您可以从以下选项中选择任何答案一切都是正确的,在这个伟大的网站上欢呼和快乐学习。
  • 当然会按照建议做...@RavinderSingh13
  • 我刚刚看到您没有添加您为解决您自己的问题而付出的努力,请添加它们,然后我将取消删除我的答案。在 SO 上,我们鼓励用户付出努力,因为我们都在这里学习

标签: linux bash shell awk sed


【解决方案1】:

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

sed -e '/object Host "kali"/{i\/*' -e ':a;n;/}/!ba;a\*/' -e '}' file

查找包含object Host "kali" 的行,在包含/* 的行之前插入一行,读取/打印更多行直到包含} 的行,然后追加*/ 行。

【讨论】:

    【解决方案2】:

    找到object Host "kali" {后:

    1. 在模式空间前添加 /*\n
    2. 如果看到},则将\n*/ 附加到模式空间、print 和delete
    3. Next 行附加到模式空间并循环回到步骤2。
    sed -e '/object Host "kali" {/ {
        s,^,/*\n,
        : loop
        /}/ {
            s,$,\n*/,
            p
            d
        }   
        N
        b loop
    }'
    

    .... 附录... 为了正确传递 "$names" 成为 sed 脚本的一部分,我们需要遵循 sh 的引用规则...我们的想法是嵌入 "$ names" 到 sed 脚本中,sed 行将如下所示:

    sed -i -e "/object Host \"$names\" {/ {
        s,^,/*\n,
        : loop
        /}/ {
            s,$,\n*/,
            p
            d
        }   
        N
        b loop
    }" "$loc"
    

    【讨论】:

    • 我试过了,它成功了,但是当试图在 bash 循环中传递它时它不起作用,我按照上面的方法尝试过,你能对此发表评论吗? @迈克尔
    • @Mohamed - 将其作为一个单独的问题可能会更好......但没问题......在您将 sed 脚本集成到更大的 shell 脚本中之后发生的事情是关于引用规则...'[stuff] $foo [stuff]' 使得shell 认为$foo 是文字而不是变量...我们只需将外部单引号更改为双引号就可以了。干杯!
    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多