Linux 要在行中grep一个特定单词,并在文件中该行下添加一个新行吗
我只想在对象主机行(linux.google.com)中grep特定单词,并在该对象主机行下方添加新行。 Display name=“A:Linux.google.com” 我正在搜索linux.google.comLinux 要在行中grep一个特定单词,并在文件中该行下添加一个新行吗,linux,bash,Linux,Bash,我只想在对象主机行(linux.google.com)中grep特定单词,并在该对象主机行下方添加新行。 Display name=“A:Linux.google.com” 我正在搜索linux.google.com object Host "linux.google.com" { import "windows" address = "linux.google.com" groups = ["linux"] } object Host "kali.google.com" { import "
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"]
}
如果在对象主机行中找到字符串,那么我想输入新行(显示名称“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"]
}
但是只搜索对象主机行中的字符串,而不搜索任何其他行我不知道如何使用sed。但是,我们可以执行以下操作。
将
input
作为文件名,output
作为输出文件
而IFS=read-r var;做
回声“$var”;
如果[[$var==*“Host\”linux.google.com\”*];然后
echo“Display name”A:Linux.google.com\;
fi;
完成<输入>输出
代码非常简单。它每行读取您的文件行,并验证该行是否具有要匹配的关键字。如果为真,那么它会在下面的一行中打印您想要的内容。我不知道如何使用sed。但是,我们可以执行以下操作。
将
input
作为文件名,output
作为输出文件
而IFS=read-r var;做
回声“$var”;
如果[[$var==*“Host\”linux.google.com\”*];然后
echo“Display name”A:Linux.google.com\;
fi;
完成<输入>输出
代码非常简单。它每行读取您的文件行,并验证该行是否具有要匹配的关键字。如果为true,则在下面的行中打印您想要的内容。您可以在
sed
中的一行中执行此操作:
> sed -i '/object Host "linux.google.com"/a Display name "A: Linux.google.com"' input.txt
工作原理:
- -i选项告诉
就地修改文件(例如,不必制作临时副本,然后用临时副本覆盖原始文件)sed
是一个/objecthost“linux.google.com”/
。它告诉sed要在哪一行上操作。在本例中,它是一个正则表达式。但是sed
有多种形式的地址,包括行号、行号范围等sed
- 地址正则表达式后面的
是a
命令。无论您在sed
后面放什么,都会附加在与地址匹配的那一行下面a
sed中的一行中执行此操作:
> sed -i '/object Host "linux.google.com"/a Display name "A: Linux.google.com"' input.txt
工作原理:
- -i选项告诉
sed
就地修改文件(例如,不必制作临时副本,然后用临时副本覆盖原始文件)
/objecthost“linux.google.com”/
是一个sed
。它告诉sed要在哪一行上操作。在本例中,它是一个正则表达式。但是sed
有多种形式的地址,包括行号、行号范围等
- 地址正则表达式后面的
a
是sed
命令。无论您在a
后面放什么,都会附加在与地址匹配的那一行下面
请查看我编辑的上述代码,并为我们提供需求建议@Mike Holt请查看我编辑的上述代码,并为我们提供需求建议@Mike Holt