Cut fields from /etc/passwd file into variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cut fields from /etc/passwd file into variables?
# 1  
Old 11-22-2011
Cut fields from /etc/passwd file into variables?

The script must ask the user to enter the user name and check whether the user exists in /etc/passwd (you must allow the partial usernames also). If the username exists, display the details as:

List of users

Login Name:
User ID:
--------------------------------------------------------------

I have started by reading the username:

read username;

Once it pulls my username from the variable I want to cut the first and second field from the /etc/passwd file into their own variables. But this is not working for me and I cannot seem to figure out how I should do this. This is the closest I have came:

grep -E "$username" /etc/passwd|login=$(cut -f1)|userid=$(cut -f2);

Any help would be greatly appreciated!

Regards,

Justin
# 2  
Old 11-22-2011
Quote:
Originally Posted by nobletechnology
grep -E "$username" /etc/passwd|login=$(cut -f1)|userid=$(cut -f2);

Seems you've misunderstood the syntax and data flow of the pipeline. Each process in the pipe receives the output from the previous command, munges it a bit, and passes it along to the next process in the pipe. Unfortunately, assignment of the output the way you are attempting isn't possible. All, though, is not lost!

If you read the man page for the cut command you'll notice that you can cut two fields from a file in the same pass. The output from the cut command would be something like scooter:1001 which you can then assign to two variables. The method for assignment depends on the shell you are using. Also, it's easier if you use the --output-delimeter option to have cut remove the colon and replace it with a space.

So, something like this should generate your desired data:
Code:
grep "$username" /etc/passwd | cut -f 1,3 -d : --output-delimiter=" "

To assign the output to variables, you can use this command in an array assignment, or assign it to a single variable and split it into two (bash) or assign it straight into two variables if you are using Korn shell.

Code:
array=( $( grep "$username" /etc/passwd | cut -f 1,3 -d : --output-delimiter=" " ) )

echo "user=${array[0]}  user-num=${array[1]}"

The previous code will work in both shells. The next code will work only in Korn shell:

Code:
grep "$username" /etc/passwd | cut -f 1,3 -d : --output-delimiter=" "  | read user userid

and assigns the values straight to the variables.

You are faced with one problem.... if given a partial user name the output from cut will be more than just two fields. To deal with this, you'll need a bit of a different approach that involves a loop. As this seems more like a homework exercise, than something for work, or maintenance of your personal system, I'll leave you to doing that.
# 3  
Old 11-23-2011
I have found an alternative solution to cutting each field. It works perfectly, but if a partial variable is used it combines each field from all the usernames it finds . I would like to have each username it finds paste each field separately into own heading. I have tried a do while loop but it has no affect. Can anyone help me find an alternative solution or maybe point me in the right direction?
--------------------------------------------------------------------------------------------------

Code:
echo Please enter the username to check:;

while read login;

do
    username=$(grep $login /etc/passwd|cut -f1 -d":");
    userid=$(grep $login /etc/passwd|cut -f3 -d":");
    groupid=$(grep $login /etc/passwd|cut -f4 -d":");
    fullname=$(grep $login /etc/passwd|cut -f5 -d":");
    homedir=$(grep $login /etc/passwd|cut -f6 -d":");
    shell=$(grep $login /etc/passwd|cut -f7 -d":");
     
    echo "         List of users";
    echo Login Name    : $username;
    echo User ID       : $userid;
    echo Group ID      : $groupid;
    echo User Full Name: $fullname | tr '[a-z]' '[A-Z]';
    echo Home Directory: $homedir;
    echo Shell         : $shell;
done

# 4  
Old 11-23-2011
Not only is grepping through the file to cut each field very inefficient, it is also the reason you are getting bad results when the user enters a partial name. Because I believe this to be an assignment, I'll only give you some hints to the overall logic:

Code:
while exit condition is not met
do
    prompt user
    read search-target
    grep passwd file for search-target |cut all needed fields | while lines read fields
    do
      print fields with headers
    done
done

Another thing that you need to think about is this: If the user enters a user name that is a partial match for something in another field, you might over pick entries that are not desired.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to count lines of CSV file where 2 fields match variables?

I'm trying to use awk to count the occurrences of two matching fields of a CSV file. For instance, for data that looks like this... Joe,Blue,Yes,No,High Mike,Blue,Yes,Yes,Low Joe,Red,No,No,Low Joe,Red,Yes,Yes,Low I've been trying to use code like this... countvar=`awk ' $2~/$color/... (4 Replies)
Discussion started by: nmoore2843
4 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. UNIX for Dummies Questions & Answers

passwd -S on linux- what are the fields?

I'm looking for some documentation on what the different fields mean in the output of passwd -S username: passwd -S foo foo PS 2012-03-20 0 70 3 -1 (Password set, MD5 crypt.) I think the date given is the date of the last password change, the 0 after that is the minimum password age, and... (2 Replies)
Discussion started by: Anne Neville
2 Replies

4. UNIX for Dummies Questions & Answers

How to extract fields from etc/passwd file?

Hi! i want to extract from /etc/passwd file,the user and user info fileds, to a another file.I've tried this: cut -d ':' -f1 ':' -f6 < file but cut can be used to extract olny one field and not two. maybe with awk is this possible? (4 Replies)
Discussion started by: strawhatluffy
4 Replies

5. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

6. Shell Programming and Scripting

Cut 2 fields and write to a output file

Hi, I am writing a code where the file is a pipe delimited and I would need to extract the 2nd part of field2 if it is "ATTN", "C/O" or "%" and check to see if field9 is populated or not. If field9 is already populated then leave it as is but if field9 is not populated then take the 2nd part of... (3 Replies)
Discussion started by: msalam65
3 Replies

7. Shell Programming and Scripting

how to cut fields in file

Hi, I have data in following format. 10001, John, Daves, Architecture, -2219 10002, Jim, Cirners, Businessman, -2219 1003, Tom, Katch, Engineer, -14003 I want to select the last column of the above given file and paste it on a different file in the following manner. File TEST column... (11 Replies)
Discussion started by: kam786sim
11 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. UNIX for Dummies Questions & Answers

Cut specific fields from a file containing multiline records

Hi, I am looking for a method to get column13 to column 50 data from the 1st line of a multiline reord. The records are stored in a large file and are separated by newline. sample format is (data in red is to be extracted) <header> A001dfhskhfkdsh hajfhksdhfjh... (3 Replies)
Discussion started by: sunayana3112
3 Replies

10. Solaris

cut -d: -f4 /etc/passwd | more

Describe what the output of the command “cut -d: -f4 /etc/passwd | more” would be? (3 Replies)
Discussion started by: wickbc
3 Replies
Login or Register to Ask a Question