LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Get duplicated names listed in /etc/passwd including the line number... (https://www.linuxquestions.org/questions/linux-newbie-8/get-duplicated-names-listed-in-etc-passwd-including-the-line-number-713718/)

shorte85 03-23-2009 02:33 AM

Get duplicated names listed in /etc/passwd including the line number...
 
I've been at this all day today and for some odd reason I can't get what I am doing wrong. What I am trying to do is run a command that will list only the duplicated names and show what line that duplicated name is on. Say for instance I run a particular command and it outputs this:

111 Timonthy Johnson
465 Tomonthy Johnson

and so forth.

So basically I'm trying to get a list of duplicated names that are indentical, but also get the line number of where it is in the list. I hope that makes sense? Here is what I have so far as a command:

Code:

cut -f5 -d: /etc/passwd | uniq -D |
Any suggestions? Like I said, I've been wracking my brain around this and haven't been able to find the solution. I'm not sure if I'm not looking in the right place or what.

I know that uniq -D part of the command lists only and all duplicated lines, which is what I want. But the part that I'm stumped on is getting what line it is on. Gosh, I hope I'm making sense of what I'm trying to do. lol

trist007 03-23-2009 03:39 AM

Try this:

cat /etc/passwd | cut -d " " -f2 | uniq -d

dunno, not sure if you can pipe uniq -d

shorte85 03-23-2009 04:16 AM

It didn't work, it gave an output of a blank file. Anyone else willing to give this a shot?

Robhogg 03-23-2009 07:51 AM

You could try this:

Code:

for line in $(cut -f5 -d: /etc/passwd | grep -v "^$" \
| sort | uniq -d);\
do grep -n $line /etc/passwd; done

I found I needed the first grep statement to remove multiple blank lines, and then to sort the output (as uniq works on successive identical lines), then feed each line back into grep to get the line numbers it occurs on.

Not perfect, but should do the job.

shorte85 03-23-2009 10:05 AM

I was hoping to keep it a command, and not a script type deal. I am not trying to be a butt about it. lol I also want to keep the anything after the first pipe can be altered as to what I have, but everything before the first pipe I would like to stay the same.

cut -f5 -d: /etc/passwd |

Keep what is in red, but was hoping there was something we could add at the end of command to get the result I was hoping to get. I hope that makes sense? Hope I'm not being a pain.

Robhogg 03-23-2009 10:37 AM

Quote:

Originally Posted by shorte85 (Post 3485046)
I was hoping to keep it a command, and not a script type deal...

I tried to find a way to do it like that, but eventually gave up. If there is a way, it will take someone who is more expert than me to work it out.

Rob

shorte85 03-23-2009 10:52 AM

Alright, thanks for trying. :) I appreciate it, and that goes for trist007 as well. :) Maybe someone else has a solution to my problem, ha!


All times are GMT -5. The time now is 02:04 PM.