LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2014, 11:00 PM   #1
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Rep: Reputation: 0
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.
Thanks,
Gary
 
Old 12-15-2014, 12:24 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
perl -i -ne 'script' filename
\$* belongs to the alias, that should be dropped....
 
Old 12-15-2014, 01:43 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
I'm not convinced "-i" should be considered here.
 
Old 12-15-2014, 08:19 PM   #4
maluhia
LQ Newbie
 
Registered: Jan 2014
Location: Central NJ
Distribution: Slackware
Posts: 10

Rep: Reputation: Disabled
perl -ne 'print $_ unless $a{$_}++' authorized_keys

You don't need to escape the $'s because they're inside the single quote. Equivalently, you could do:

perl -ne "print \$_ unless \$a{\$_}++" authorized_keys
 
Old 12-15-2014, 08:58 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
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.
 
Old 12-17-2014, 12:37 PM   #6
gctaylor1
Member
 
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

Original Poster
Rep: Reputation: 0
Thanks everyone. OP here. I've been kind of slammed and haven't been able to test but these look good.
 
Old 11-13-2015, 01:53 PM   #7
brent.brimacomb@hpe.com
LQ Newbie
 
Registered: Nov 2015
Posts: 3

Rep: Reputation: Disabled
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?

Brent
 
Old 11-13-2015, 06:22 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
There are two solutions presented - one of them perl.
If you want to learn, look at post #4 - if you aren't confident look at post #5.

In either case better to write to a new file and replace the original manually when convinced.
 
Old 11-13-2015, 09:20 PM   #9
brent.brimacomb@hpe.com
LQ Newbie
 
Registered: Nov 2015
Posts: 3

Rep: Reputation: Disabled
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.

Brent

>cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAvJPhWjzLlxLrPW94T86a/JRvP+HHWjIfERQUj8eBm3sg tHNyfMZLtiLfMGe9d/zGuFHVzm3R7g1Pv+88ZFQjZ0AjRZal+2NXtEQovVs88UjDdaZBXEO3VPWzktER URkkrInSk7LKvMXHOjIggzG+O1tiExJyKjXTxuRhTpsm2kSd6mXhkowh0wCIpG57GTGviCdC7Ut1uwKN VHBYBq0JNNblYf09JuA7vNzWjaJjSnSt8bLCvyIgzsAyLp6igM1urxXNepNwCvqwHXTEES0adLP2yLm+ tA/IPU17zDMX7DUqcDbxk+R0txgtoW/M1GcJX7n2gep1Mdwp1Ft40m7RfQ== brent@XYZSERVR
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAty/iV4YP1RhqZ0dL/HgFzma8wJwiWf8S1SMtLVvV5k4s wayDBZR8v0lVMeUXIMPcR6kL98KQAT9oxpBAxKDRl1HHBfddHu97jNpNl7RgFkPSF/jRkTBaY3O1iRYV SeOBKDOuoGc/JQuyFnZj/Rg4V1OHvFH2uojb9aHZcr+4F7yfqupdOTinf50UIXm809e5u6UH6hpm3YPE 60JcdFzEMtK7yHur7nB0onEDFo8SYXzNRUpVdv5WAJ521YYdlbURhJleS6lwyPLGwz/9mxaSm7ry8M3S o3aXrRKG74LdRFVVUbsoLKXI697xEw3BhXD6XaJscouloIlmuod4pA9rFw== brent@XYZSERVR
 
Old 11-13-2015, 10:20 PM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by brent.brimacomb@hpe.com View Post
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.
 
Old 11-14-2015, 06:59 AM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,131

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Try this
Code:
awk 'holdit[$NF]++ {print "Duplicate key: "$NF"\tRecord #: "NR}' authorized_keys
Succinct and clean.
awk is (also) well worth studying.
 
Old 11-17-2015, 09:41 PM   #12
brent.brimacomb@hpe.com
LQ Newbie
 
Registered: Nov 2015
Posts: 3

Rep: Reputation: Disabled
That's a great idea, but I've been told, "Though shall write it in Perl". Considering calling awk from Perl though?
 
Old 11-18-2015, 02:05 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,901

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
you need to improve the original perl script. Have you tried that?
 
Old 11-18-2015, 03:24 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
as sundial said, 'sort | uniq' and its done.

or try painting your house with a toothbrush.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] perl one liner how to using sort jheengut Programming 2 11-17-2014 12:10 PM
[SOLVED] Perl Array to remove duplicates d072330 Linux - Software 26 01-25-2013 01:15 PM
Remove duplicates from file sandikaxp Linux - Newbie 17 06-21-2012 06:58 PM
perl one liner error casperdaghost Linux - Newbie 6 08-24-2011 12:16 PM
I want to keep the duplicates not remove them! ieatbunnies Linux - Software 1 01-17-2011 12:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration