我有这个ssh配置,需要编辑。
Host vps6
HostName 123.456.789.00
User dylan
Port 123
Host vps4
HostName 123.456.789.00
User dylan
Port 123
# old server
Host vps3-old
HostName 123.456.789.00
User dylan
Port 123我希望将vps6的配置移到文件的末尾,并将-old附加到它的配置别名。生成的文件将是。
Host vps4
HostName 123.456.789.00
User dylan
Port 123
# old server
Host vps3-old
HostName 123.456.789.00
User dylan
Port 123
Host vps6-old
HostName 123.456.789.00
User dylan
Port 123我使用sed命令sed '/'"vps4"'/{N;N;N;N;H;$!d}; ${p;x;s/'"vps4"'/'"vps4"'-old/}'成功地做到了这一点,不幸的是,这在文件末尾给了我不想要的换行符。
[tmp]$ sed '/'"vps6"'/{N;N;N;N;H;$!d}; ${p;x;s/'"vps6"'/'"vps6"'-old/}' config
Host vps4
HostName 123.456.789.00
User dylan
Port 123
# old server
Host vps3-old
HostName 123.456.789.00
User dylan
Port 123
Host vps6-old
HostName 123.456.789.00
User dylan
Port 123
[tmp]$ # See above me此外,我希望能够指定要移动的下一个n行(例如,上面将标记Host vps4,接下来的3行将移动到文件末尾)。我搜索了这个网络,发现这类任务推荐的工具是ed,但是我还没有找到示例命令来做我想做的事情。
发布于 2022-09-14 02:49:47
在显示的示例中,请尝试遵循awk代码。
awk '
!NF && found{
found=""
next
}
/^Host vps6/{
found=1
line=$0"-old"
next
}
found{
val=(val?val ORS:"")$0
next
}
!found
END{
print ORS line ORS val
}
' Input_file注意:如果您想将输出保存到Input_file本身,然后运行上面的程序,它将在终端上打印输出,一旦您对上述程序的结果感到满意,就可以将> temp && mv temp Input_file附加到上面的程序中,以执行内部保存到Input_file中。
说明:添加了对上述用过的代码的详细说明。
awk ' ##Starting awk program from here.
!NF && found{ ##Checking if line is empty AND found is SET then do following.
found="" ##Nullifying found here.
next ##next will skip all further statements from here.
}
/^Host vps6/{ ##If line starts from Host vps6 then do following.
found=1 ##Setting found here.
line=$0"-old"
next
}
found{ ##If found is set then do following.
val=(val?val ORS:"") $0 ##Creating val which is keep adding current line into it.
next ##next will skip all further statements from here.
}
!found ##If found is NOT set then print that line.
END{ ##Starting END block of this program from here.
print ORS line ORS val ##Printing ORS line ORS and val here.
}
' Input_file ##Mentioning Input_file name here. 发布于 2022-09-14 04:17:28
另一只鹰:
$ awk -v RS= '{ # read blank line separated records
if(/^Host vps6/) { # buffer matching record
sub(/vps6/,"vps6-old") # add -old
b=$0
} else # print non-matching
print $0 ORS # with with extra newline
}
END { # in the end
print b # output buffer
}' file如果您想使用变量而不是硬编码字符串,请使用以下内容:
$ awk -v s=vps6 -v RS= '{ # or -v s="$sshname"
if($0~"^Host " s) {
match($0,s)
b=substr($0,1,RSTART+RLENGTH-1) "-old" substr($0,RSTART+RLENGTH)
} else
print $0 ORS
}
END {
print b
}发布于 2022-09-14 03:08:35
编辑:这与@RavinderSingh13的回答基本相同,但没有那么好。
另一个可能的选择是:
cat file1
Host vps6
HostName 123.456.789.00
User dylan
Port 123
Host vps4
HostName 123.456.789.00
User dylan
Port 123
# old server
Host vps3-old
HostName 123.456.789.00
User dylan
Port 123awk '
{flag=0}
/vps6/,/^$/ {flag=1}
{
gsub("vps6", "vps6-old")
if (flag == 0) print
if (flag == 1) a[NR]=$0
}
END {
print ""
for (i in a) {
if(a[i] != "") print a[i]
}
}' file1
Host vps4
HostName 123.456.789.00
User dylan
Port 123
# old server
Host vps3-old
HostName 123.456.789.00
User dylan
Port 123
Host vps6-old
HostName 123.456.789.00
User dylan
Port 123https://stackoverflow.com/questions/73710773
复制相似问题