【发布时间】: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 上,我们鼓励用户付出努力,因为我们都在这里学习