我有以下文件,它是 ssh 的配置文件。
Host vps2 # Linode
HostName xxx.xx.xx.xxx
User foo_user
Host vps3 # Vultr
HostName xxx.xx.xx.xxx
User foo_user
Host vps4
HostName xxx.xx.xx.xxx
User foo_user
Host vps5
HostName xxx.xx.xx.xxx
User foo_user
Host vps6
HostName xxx.xx.xx.xxx
User foo_user
Host vps7 # DigitalOcean
HostName xxx.xx.xx.xxx
User foo_user
Host vps8 # GCP
HostName xxx.xx.xx.xxx
User foo_user
Host pi
HostName xxx.xx.xx.xxx
User pi
# OLD SHALL NOT BE USED
Host vps13
HostName xxx.xx.xx.xxx
User foo_user
Host vps14-old
HostName xxx.xx.xx.xxx
User foo_user
Host vps4-old
HostName xxx.xx.xx.xxx
User foo_user
Host vps15-old
HostName xxx.xx.xx.xxx
User foo_user
Host vps11-old
HostName xxx.xx.xx.xxx
User foo_user
我需要打印以 , 开头的别名vps*
,下面的(复制的)片段将完全做到这一点。
$ awk '{for(i=1;i<=NF;i++){if($i~/^vps/){print $i}}}' $HOME/.ssh/config
vps2
vps3
vps4
vps5
vps6
vps7
vps8
vps3-old
vps4-old
vps5-old
vps11-old
现在我想打印所有没有-old
后缀的别名,添加| grep -v old
作品。
$ awk '{for(i=1;i<=NF;i++){if($i~/^vps/){print $i}}}' $HOME/.ssh/config | grep -v "old"
vps2
vps3
vps4
vps5
vps6
vps7
vps8
有没有更清洁的方法?最好只涉及 1 个工具,我尝试使用awk
命令无济于事。