List Only Usernames

Started by nikolaymartin, May 26, 2020, 12:43:19 PM

Previous topic - Next topic

nikolaymartin

Iwant to list only users names in a Linux system byusing the /etc/passwd file.

Otanx

cut -d : -f 1 /etc/password

Cut each line in the file on every : then print the first field. Or...

sed -E s/:.*// /etc/password

The -E is for regex support. Then I am looking for a colon folowed by 0 or more characters. and replacing it with nothing. I find cut to be easier to understand, but sed is more powerful. I am pretty sure you could do this with awk too, but I couldn't do it off the top of my head.

-Otanx

edizgeorgi

#2
You can use the cut command which will select the specified column.
$cat /etc/passwd | cut -d : -f 1
Reference: https://www.networking-forums.com/programming-goodies-and-software-defined-networking/list-only-usernames


Otanx

While that works you will also get a "Useless use of cat award" from any serious linux guys. Using cat and piping to a command takes longer, and uses more resources than using the command directly. Think about it like this. You are using cat to read the file, and send the entire file to another command. That command then reads the entire file from cat, and does what it does. It is more efficient to just have the command read the file directly.

In this instance of a passwd file it does not really matter. When you start talking about 100s of megs or gigs of data it starts to be noticeable. One of my old Linux guys broke me of that habit by drawing cats on my whiteboard when he reviewed any of my scripts, or documents and I did that.

-Otanx

deanwebb

What's the difference between cut and sed? And awk for that matter?
Take a baseball bat and trash all the routers, shout out "IT'S A NETWORK PROBLEM NOW, SUCKERS!" and then peel out of the parking lot in your Ferrari.
"The world could perish if people only worked on things that were easy to handle." -- Vladimir Savchenko
Вопросы есть? Вопросов нет! | BCEB: Belkin Certified Expert Baffler | "Plan B is Plan A with an element of panic." -- John Clarke
Accounting is architecture, remember that!
Air gaps are high-latency Internet connections.

Otanx

Cut is a program that lets you define a delimiter ( -d ) and will take text, and split the text into fields based on that delimiter. You can then print out just the field you want ( -f 1 ). I am sure it can do more, but that is the main use case. In this case I am saying split the lines into fields seperated by a colon, and then print the first field which for the passwd file format is the username. I could also change -f 1 to -f 3, and it would print the user ID for that user.

Sed stands for String EDitor. Just like the name says it takes a string, and lets you define a rule to edit that string. In this specific example I am matching the regex of :.* and replacing it with nothing. I could do something like sed -E s/"hello"/"goodbye"/ and it would replace hello with goodbye. It can do alot more than that, but again this is my main use case. The down side is unless it is a very simple task you need to know regex.

AWK is basically a full scripting language for working with strings, and text. I do not use it. I know it exists, and can usually figure out what it does if someone else uses it for something simple. Otherwise I go ask one of our developers.

-Otanx

deanwebb

Quote from: Otanx on May 27, 2020, 01:08:53 PM
Cut is a program that lets you define a delimiter ( -d ) and will take text, and split the text into fields based on that delimiter. You can then print out just the field you want ( -f 1 ). I am sure it can do more, but that is the main use case. In this case I am saying split the lines into fields seperated by a colon, and then print the first field which for the passwd file format is the username. I could also change -f 1 to -f 3, and it would print the user ID for that user.

Sed stands for String EDitor. Just like the name says it takes a string, and lets you define a rule to edit that string. In this specific example I am matching the regex of :.* and replacing it with nothing. I could do something like sed -E s/"hello"/"goodbye"/ and it would replace hello with goodbye. It can do alot more than that, but again this is my main use case. The down side is unless it is a very simple task you need to know regex.

AWK is basically a full scripting language for working with strings, and text. I do not use it. I know it exists, and can usually figure out what it does if someone else uses it for something simple. Otherwise I go ask one of our developers.

-Otanx


TIL something new. Thanks, Otanx! :)
Take a baseball bat and trash all the routers, shout out "IT'S A NETWORK PROBLEM NOW, SUCKERS!" and then peel out of the parking lot in your Ferrari.
"The world could perish if people only worked on things that were easy to handle." -- Vladimir Savchenko
Вопросы есть? Вопросов нет! | BCEB: Belkin Certified Expert Baffler | "Plan B is Plan A with an element of panic." -- John Clarke
Accounting is architecture, remember that!
Air gaps are high-latency Internet connections.

Otanx

I am glad you learned something. For some reason I don't feel like nikolaymartin learned anything.

-Otanx

deanwebb

Quote from: Otanx on May 28, 2020, 08:24:35 AM
I am glad you learned something. For some reason I don't feel like nikolaymartin learned anything.

-Otanx


He doesn't really engage with the instructor. Not a good student strategy.
Take a baseball bat and trash all the routers, shout out "IT'S A NETWORK PROBLEM NOW, SUCKERS!" and then peel out of the parking lot in your Ferrari.
"The world could perish if people only worked on things that were easy to handle." -- Vladimir Savchenko
Вопросы есть? Вопросов нет! | BCEB: Belkin Certified Expert Baffler | "Plan B is Plan A with an element of panic." -- John Clarke
Accounting is architecture, remember that!
Air gaps are high-latency Internet connections.