我只希望grep对象宿主行( linux.google.com )中的特定单词,并在该对象宿主行下面添加新行。显示名称= "A: Linux.google.com“
object Host "linux.google.com" {
import "windows"
address = "linux.google.com"
groups = ["linux"]
}
object Host "kali.google.com" {
import "linux"
address = "linux.google.com"
groups = [linux ]
}
object Host "windows.google.com" {
import "linux"
address = "linux.google.com"
groups = ["windows" ]
}
object Host "os.google.com" {
import "windows"
address = "linux.google.com"
groups = ["linux"]
}我在搜索linux.google.com
如果在对象主机行中找到该字符串,则我想输入新行(显示名称"A: Linux.google.com“)(显示名称"B: os.google.com”),如下所述。
object Host "linux.google.com" {
Display name "A: Linux.google.com"
import "windows"
address = "linux.google.com"
groups = ["linux"]
}
object Host "os.google.com" {
Display name "B: os.google.com"
import "windows"
address = "linux.google.com"
groups = ["linux"]
}但仅在对象宿主行中搜索字符串,而不在任何其他行中搜索
发布于 2019-04-24 06:24:47
在sed中,只需一行代码即可完成此操作
> sed -i '/object Host "linux.google.com"/a Display name "A: Linux.google.com"' input.txt它的工作原理:
发布于 2019-04-24 06:23:52
我不知道用sed怎么做。但是,我们可以执行以下操作。
让input作为文件名,output作为输出文件。
while IFS= read -r var; do
echo "$var";
if [[ $var == *"Host \"linux.google.com\""* ]]; then
echo "Display name \"A: Linux.google.com\"";
fi;
done < input > output代码非常简单。它每行读取您的文件行,并验证该行是否具有您想要匹配的关键字。如果为true,则在下面的行中打印您想要的内容。
https://stackoverflow.com/questions/55820044
复制相似问题