Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8472705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:08:59+00:00 2026-06-10T17:08:59+00:00

From /etc/passwd , I need just the user part. Example: backup:x:34:34:backup:/var/backups:/bin/sh has backup as

  • 0

From /etc/passwd, I need just the “user” part.

Example:

backup:x:34:34:backup:/var/backups:/bin/sh

has backup as the user.

To extract the user part I am using this now:

perl -pe 's/^(.*?):.*/\1/g' < /etc/passwd

which is a few characters less than my old method:

while read line; do echo ${line%%:*}; done < /etc/passwd

but it sounds like an overkill. It’s a lot of text to write.

A Google search gives me:

awk -F  ':' '{print $1}' < /etc/passwd

which is equivalent, I suppose? Is it?

cmp <(perl -pe 's/^(.*?):.*/\1/g' < /etc/passwd) <(awk -F  ':' '{print $1}' < /etc/passwd)

Is there a standard UNIX tool to do this or an easier method using perl? Or cut?

I am a beginner.

BTW, I tried my hand at Python as follows but I suck at it, there may be better ways to do this in Python :(

python -c 'print "\n".join([u[:u.find(":")] for u in open("/etc/passwd")])'

EDIT: actually, maybe more like this for Python:

python -c 'print "\n".join([u.split(":")[0] for u in open("/etc/passwd")])'

Hmm… still very verbose.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:09:01+00:00Added an answer on June 10, 2026 at 5:09 pm
    perl -F: -lane 'print $F[0]' < /etc/passwd
    

    does nore-or-less what the awk solution does. -a means split each line of input into fields, -F: means that : is the field delimiter. And the -l outputs a newline for each line of input so you don’t have to say print "$F[0]\n".

    Since you requested it, the cut solution is even more straightforward:

    cut -d: -f1 < /etc/passwd
    

    Meaning: split on :, output the first field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Crystal Reports 2008, I need to extract a date from a text field.
i am using hybridauth to get userinformation from social web sites like facebook,twitter, etc.
i am using hybridauth to get userinformation from social web sites like facebook,twitter, etc.
I'm converting code to IL (CIL/MSIL, etc.) from C#. I'm stuck on the conditionals
I have the following query to select users and their locations, etc from MySQL
How can I retrieve the meta data such as Description, Modified/Create Dates etc from
I moved my website from the /v1/etc... directory to the /v2/etc... directory and would
I have looked at various methods ranging from string.erase/ ispunct etc. and I cant
I let users embed videos from Youtube, Google, Vimeo etc. I thought about the
I came into matrix world from loop world (C, etc) I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.