I have following file, it's a config file for 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
I need to print alias that start with vps*
, below (copied) snippets will exactly do that.
$ 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
Now I want to print all alias that has no -old
suffix, adding | grep -v old
works.
$ 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
Is there any cleaner way ? Preferably involving only 1 tools, I tried playing with awk
command to no avail.
You could add a $i!~/-old$/
condition to the awk
command:
awk '{for(i=1;i<=NF;i++){if($i~/^vps/ && $i!~/-old$/){print $i}}}' ~/.ssh/config
(Note: I prefer ~
over $HOME
when it's not in double-quotes, just in case of weird characters in the path.)