Here is the working code (intended to be executed with perl -e '<code>' ~/.ssh/config):
Actual version:
while(<>){if(/^Host (.+)/){$_=1ドル;foreach $i(/([^ ]+)/g){$h{$i}=1}}}print "$_ " for keys %h;
Identical version with whitespace for reading:
while(<>) {
if(/^Host (.+)/) {
$_=1ドル;
foreach $i(/([^ ]+)/g) {
$h{$i}=1
}
}
}
print "$_ " for keys %h;
I'm sort of a perl newbie. Is $_=1ドル kosher? Is there a straight-forward way to do this with a single regex? Is there anything else I should be doing better here?
Example input output:
For a config file with contents like this:
Host build
HostName build.myserver.com
Host build build.myserver.com
User ubuntu
IdentityFile ~/.ssh/build
Host tunnel
LocalForward 6397 redis.production.com:6379
The output should be (order is not required, trailing space is not required but acceptable):
build build.myserver.com tunnel
1 Answer 1
I would use something like
perl -lane '@h{@F[1..$#F]}=()if/^Host\b/;END{,ドル=" ";print keys %h}' -- file
or
perl -lane '@h{ @F[ 1 .. $#F ] } = () if /^Host\b/;
END {
,ドル = " ";
print keys %h;
}' -- file
-lremoves newlines from input and adds them toprints-nruns the code for each line of the input-asplits each line on whitespace into the @F array- the hash
%his populated by all the non-whitepsace strings followingHost - at the end, all the keys are printed separated by space
-
1\$\begingroup\$ Commenting some other things I learned from this answer that I didn't know previously:
$#arrayreturns the last index of an array,,ドルis the "output field separator" printed automatically between fields,END{}gives a block of code that is executed when exiting, useful for doing final one-time code while still using the-noption. \$\endgroup\$GrandOpener– GrandOpener2019年01月28日 20:22:25 +00:00Commented Jan 28, 2019 at 20:22 -
\$\begingroup\$ can shave a few characters there by switching to
print "@{[ EXPR ]}"and usingeofin place of an END block. And the spaces after-eandkeysare optional.perl -lane'@h{@F[1..$#F]}=()if/^Host\b/;eof&&print"@{[keys%h]}"' -- file\$\endgroup\$Oh My Goodness– Oh My Goodness2019年01月31日 13:21:10 +00:00Commented Jan 31, 2019 at 13:21