Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 匹配两行,替换为三行_Linux_Unix_Sed - Fatal编程技术网

Linux 匹配两行,替换为三行

Linux 匹配两行,替换为三行,linux,unix,sed,Linux,Unix,Sed,我需要使用sed将每个匹配的双线模式替换为三线模式。这是我的输入文件(tempfile.txt) 基本上,如果客户机主机名为“hostname” 缺少,则应替换为选项卡,然后替换为换行符 我的尝试: sed'N/硬件.}/d;PD'tempfile.txt 结果是: lease 192.168.6.100 { binding state free; lease 192.168.6.100 { binding state active; hardware ethernet 00:e0:

我需要使用
sed
将每个匹配的双线模式替换为三线模式。这是我的输入文件(
tempfile.txt

基本上,如果客户机主机名为“hostname” 缺少,则应替换为
选项卡
,然后替换为
换行符

我的尝试:
sed'N/硬件.}/d;PD'tempfile.txt

结果是:

lease 192.168.6.100 {
  binding state free;
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
  binding state free;
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
这是我想要的输出

lease 192.168.6.100 {
  binding state free;
  hardware ethernet 00:e0:4c:68:00:96;
  <tab>
}
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
  binding state free;
  hardware ethernet 00:e0:4c:68:00:96;
  <tab>
}
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
租赁192.168.6.100{
无束缚态;
硬件以太网00:e0:4c:68:00:96;
}
租赁192.168.6.100{
结合态活性;
硬件以太网00:e0:4c:68:00:96;
客户端主机名“andrew-H81M-S2PH”;
}
租赁192.168.6.100{
无束缚态;
硬件以太网00:e0:4c:68:00:96;
}
租赁192.168.6.100{
结合态活性;
硬件以太网00:e0:4c:68:00:96;
客户端主机名“andrew-H81M-S2PH”;
}

如你所见,卷发之间始终有三条线。这就是我的目标。

我冒昧又加了一个卷发

sed 'N; s/hardware.*}/\t\n}/; P; D' andy
lease 192.168.6.100 {
  binding state free;

}
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
  binding state free;

}
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}

我冒昧又加了一个卷发

sed 'N; s/hardware.*}/\t\n}/; P; D' andy
lease 192.168.6.100 {
  binding state free;

}
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
  binding state free;

}
lease 192.168.6.100 {
  binding state active;
  hardware ethernet 00:e0:4c:68:00:96;
  client-hostname "andrew-H81M-S2PH";
}
这样做的诀窍是(通过管道输送到cat-A以显示不可打印的字符):

不是删除匹配项,而是捕获应该围绕空行的两行,并替换为换行符和制表符。我还添加了一些锚,以便更安全地匹配

有一点小技巧,因为模式空间在替换后包含两个换行符,但是
p;D
只打印第一行,然后开始新的循环,这会导致在包含
客户端主机名的行之后出现不需要的换行符

详细解释如下:

N       # Append next line to pattern space

# If the hostname is missing, insert a newline and a tab
s/^([[:space:]]*hardware.*)(\n})$/\1\n\t\2/

t      # If we did the substitution, jump to end (prints complete pattern space)
P      # Print first line in pattern space
D      # Delete first line in pattern space - starts new cycle
这样做的诀窍是(通过管道输送到cat-A以显示不可打印的字符):

不是删除匹配项,而是捕获应该围绕空行的两行,并替换为换行符和制表符。我还添加了一些锚,以便更安全地匹配

有一点小技巧,因为模式空间在替换后包含两个换行符,但是
p;D
只打印第一行,然后开始新的循环,这会导致在包含
客户端主机名的行之后出现不需要的换行符

详细解释如下:

N       # Append next line to pattern space

# If the hostname is missing, insert a newline and a tab
s/^([[:space:]]*hardware.*)(\n})$/\1\n\t\2/

t      # If we did the substitution, jump to end (prints complete pattern space)
P      # Print first line in pattern space
D      # Delete first line in pattern space - starts new cycle

我不太明白:“如果它不见了,就应该换掉……”。。。?你能展示所需的输出吗?你能把它添加到问题中吗?我的意思是,这样问题就更清楚了,对未来的访问者来说也是如此,而不必挖掘评论。完成。很抱歉。我已经编辑了这个问题:你实际上用三行替换了两行匹配的行-我相应地修改了措辞。我不太明白:“如果它丢失了,应该替换它”。。。?你能展示所需的输出吗?你能把它添加到问题中吗?我的意思是,这样问题就更清楚了,对未来的访问者来说也是如此,而不必挖掘评论。完成。抱歉。我已经编辑了这个问题:你实际上用三行替换了两行匹配的行-我相应地修改了措辞。对不起,tink,在我意识到我原来的问题问错之前,你的回答是正确的。道歉!对不起,tink,在我意识到我原来的问题问错之前,你的回答是正确的。道歉!