Perl one-liner to remove duplicates from authorized_keys?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl one-liner to remove duplicates from authorized_keys?
I found an example that uses a Perl one-liner to remove duplicate entries in an authorized_keys file. However as you can see below the example is set up as an alias.
Code:
alias dup="perl -i -ne 'print unless \$a{\$_}++' \$*"
How do you run this one liner against a file without using an alias?
Code:
# perl -i -ne 'print unless \$a{\$_}++' \$* authorized_keys
Can't open $*: No such file or directory.
# cat authorized_keys |perl -i -ne 'print unless \$a{\$_}++' \$*
Can't open $*: No such file or directory.
# perl -i -ne 'print unless \$a{\$_}++' \$* < authorized_keys
Can't open $*: No such file or directory.
It could be just as simple as the ubiquitous sort | uniq . . .
After all, this is a text-file, and what you want is to eliminate the duplicates from it. Therefore, first sort the file, then pass it through a filter which removes consecutive duplicate records. Direct that to a new file. Then, swap the new file for the old one. Q.E.D.
Very new to Perl and trying to come up with something similar. I am getting dups in the authorized_keys file. Looking for a simple way to just flag there are dups or even possibly remove them. What was the final solution on this thread?
Thanks for the reply. I guess my problem is slightly different than the one listed, but very similar. I have users recreating their keys and copying their new public key from the primary host to the remote host authorized_key file. So the two keys are not identical, but the signature of the user@hostname is and it causes the authentication to fail.
So what I really need to do is look for duplicate user@hostname, not the entire key as the new key is different. Only the last part of each key is duplicate.
any idea how to look for dups on just part of the file? See the example below.
any idea how to look for dups on just part of the file?
Several.
These days I'd use awk rather than perl - it splits into fields based on whitespace, so the string you are looking for would be the last field - which awk also gives you a variable for.
Could also be done easily in bash using parameter substitution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.