Printing out name for each row of /etc/passwd file output












1















I have an assignment to print out information about a user from the /etc/passwd file. What I want is for the output to be column format instead of row format.



what I've managed to do is to get everything printed on new rows with help of the tr command, so the output right now is like the following.



user

x

1002

1002

/home/user

/bin/sh


but what i want is for the output to be like this.



username user

password x

user id 1002

group id 1002

comment

directory /home/user

shell /bin/sh


my code right now looks like this



    grep user /etc/passwd | tr ':' 'n'









share|improve this question


















  • 1





    Read up on awk.

    – Shawn
    Jan 2 at 13:07
















1















I have an assignment to print out information about a user from the /etc/passwd file. What I want is for the output to be column format instead of row format.



what I've managed to do is to get everything printed on new rows with help of the tr command, so the output right now is like the following.



user

x

1002

1002

/home/user

/bin/sh


but what i want is for the output to be like this.



username user

password x

user id 1002

group id 1002

comment

directory /home/user

shell /bin/sh


my code right now looks like this



    grep user /etc/passwd | tr ':' 'n'









share|improve this question


















  • 1





    Read up on awk.

    – Shawn
    Jan 2 at 13:07














1












1








1








I have an assignment to print out information about a user from the /etc/passwd file. What I want is for the output to be column format instead of row format.



what I've managed to do is to get everything printed on new rows with help of the tr command, so the output right now is like the following.



user

x

1002

1002

/home/user

/bin/sh


but what i want is for the output to be like this.



username user

password x

user id 1002

group id 1002

comment

directory /home/user

shell /bin/sh


my code right now looks like this



    grep user /etc/passwd | tr ':' 'n'









share|improve this question














I have an assignment to print out information about a user from the /etc/passwd file. What I want is for the output to be column format instead of row format.



what I've managed to do is to get everything printed on new rows with help of the tr command, so the output right now is like the following.



user

x

1002

1002

/home/user

/bin/sh


but what i want is for the output to be like this.



username user

password x

user id 1002

group id 1002

comment

directory /home/user

shell /bin/sh


my code right now looks like this



    grep user /etc/passwd | tr ':' 'n'






linux bash scripting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 12:58









O. SherlockO. Sherlock

192




192








  • 1





    Read up on awk.

    – Shawn
    Jan 2 at 13:07














  • 1





    Read up on awk.

    – Shawn
    Jan 2 at 13:07








1




1





Read up on awk.

– Shawn
Jan 2 at 13:07





Read up on awk.

– Shawn
Jan 2 at 13:07












2 Answers
2






active

oldest

votes


















1














Just parse the file with : as the value of IFS.



wanted=user
grep ":$wanted:" /etc/passwd | # this grep alone not a great idea
while IFS=: read usr pswd uid gid cmnt dir shell;
do [[ "$usr" == "$wanted" ]] || continue # skip false hits
printf 'username %snpassword %snuser id %sngroup id %sn%sndirectory %snshell %sn' "$usr" "$pswd" "$uid" "$gid" "$cmnt" "$dir" "$shell";
done


The grep alone will give you false hits though. My first test gave me username tss when the comment was Account used by the trousers package to sandbox the tcsd daemon and username saslauth when comment was Saslauthd user.



Adding colons around it prevented false hits in my passwd file, but to be sure(r), explicitly check the user on each iteration.






share|improve this answer

































    0














    You can use following shell script. I just wrote it for two fields such as username and password. I hope you can manage for other fields.



    Script



    while read line
    do
    echo $line | awk -F':' '{print "Username: "$1"nPassword: "$2"n_______"}'
    done</etc/passwd


    OUTPUT



    [root@967dd7743677 test]# while read line; do echo $line | awk -F':' '{ print "Username: " $1 "nPassword: " $2 "n______" }'; done</etc/passwd
    Username: root
    Password: x
    ______
    Username: bin
    Password: x
    ______
    Username: daemon
    Password: x
    ______





    share|improve this answer



















    • 1





      If you're parsing with awk, why are you wrapping it in a while read loop?

      – Paul Hodges
      Jan 2 at 14:12











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006829%2fprinting-out-name-for-each-row-of-etc-passwd-file-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Just parse the file with : as the value of IFS.



    wanted=user
    grep ":$wanted:" /etc/passwd | # this grep alone not a great idea
    while IFS=: read usr pswd uid gid cmnt dir shell;
    do [[ "$usr" == "$wanted" ]] || continue # skip false hits
    printf 'username %snpassword %snuser id %sngroup id %sn%sndirectory %snshell %sn' "$usr" "$pswd" "$uid" "$gid" "$cmnt" "$dir" "$shell";
    done


    The grep alone will give you false hits though. My first test gave me username tss when the comment was Account used by the trousers package to sandbox the tcsd daemon and username saslauth when comment was Saslauthd user.



    Adding colons around it prevented false hits in my passwd file, but to be sure(r), explicitly check the user on each iteration.






    share|improve this answer






























      1














      Just parse the file with : as the value of IFS.



      wanted=user
      grep ":$wanted:" /etc/passwd | # this grep alone not a great idea
      while IFS=: read usr pswd uid gid cmnt dir shell;
      do [[ "$usr" == "$wanted" ]] || continue # skip false hits
      printf 'username %snpassword %snuser id %sngroup id %sn%sndirectory %snshell %sn' "$usr" "$pswd" "$uid" "$gid" "$cmnt" "$dir" "$shell";
      done


      The grep alone will give you false hits though. My first test gave me username tss when the comment was Account used by the trousers package to sandbox the tcsd daemon and username saslauth when comment was Saslauthd user.



      Adding colons around it prevented false hits in my passwd file, but to be sure(r), explicitly check the user on each iteration.






      share|improve this answer




























        1












        1








        1







        Just parse the file with : as the value of IFS.



        wanted=user
        grep ":$wanted:" /etc/passwd | # this grep alone not a great idea
        while IFS=: read usr pswd uid gid cmnt dir shell;
        do [[ "$usr" == "$wanted" ]] || continue # skip false hits
        printf 'username %snpassword %snuser id %sngroup id %sn%sndirectory %snshell %sn' "$usr" "$pswd" "$uid" "$gid" "$cmnt" "$dir" "$shell";
        done


        The grep alone will give you false hits though. My first test gave me username tss when the comment was Account used by the trousers package to sandbox the tcsd daemon and username saslauth when comment was Saslauthd user.



        Adding colons around it prevented false hits in my passwd file, but to be sure(r), explicitly check the user on each iteration.






        share|improve this answer















        Just parse the file with : as the value of IFS.



        wanted=user
        grep ":$wanted:" /etc/passwd | # this grep alone not a great idea
        while IFS=: read usr pswd uid gid cmnt dir shell;
        do [[ "$usr" == "$wanted" ]] || continue # skip false hits
        printf 'username %snpassword %snuser id %sngroup id %sn%sndirectory %snshell %sn' "$usr" "$pswd" "$uid" "$gid" "$cmnt" "$dir" "$shell";
        done


        The grep alone will give you false hits though. My first test gave me username tss when the comment was Account used by the trousers package to sandbox the tcsd daemon and username saslauth when comment was Saslauthd user.



        Adding colons around it prevented false hits in my passwd file, but to be sure(r), explicitly check the user on each iteration.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 16:06

























        answered Jan 2 at 14:12









        Paul HodgesPaul Hodges

        3,7501524




        3,7501524

























            0














            You can use following shell script. I just wrote it for two fields such as username and password. I hope you can manage for other fields.



            Script



            while read line
            do
            echo $line | awk -F':' '{print "Username: "$1"nPassword: "$2"n_______"}'
            done</etc/passwd


            OUTPUT



            [root@967dd7743677 test]# while read line; do echo $line | awk -F':' '{ print "Username: " $1 "nPassword: " $2 "n______" }'; done</etc/passwd
            Username: root
            Password: x
            ______
            Username: bin
            Password: x
            ______
            Username: daemon
            Password: x
            ______





            share|improve this answer



















            • 1





              If you're parsing with awk, why are you wrapping it in a while read loop?

              – Paul Hodges
              Jan 2 at 14:12
















            0














            You can use following shell script. I just wrote it for two fields such as username and password. I hope you can manage for other fields.



            Script



            while read line
            do
            echo $line | awk -F':' '{print "Username: "$1"nPassword: "$2"n_______"}'
            done</etc/passwd


            OUTPUT



            [root@967dd7743677 test]# while read line; do echo $line | awk -F':' '{ print "Username: " $1 "nPassword: " $2 "n______" }'; done</etc/passwd
            Username: root
            Password: x
            ______
            Username: bin
            Password: x
            ______
            Username: daemon
            Password: x
            ______





            share|improve this answer



















            • 1





              If you're parsing with awk, why are you wrapping it in a while read loop?

              – Paul Hodges
              Jan 2 at 14:12














            0












            0








            0







            You can use following shell script. I just wrote it for two fields such as username and password. I hope you can manage for other fields.



            Script



            while read line
            do
            echo $line | awk -F':' '{print "Username: "$1"nPassword: "$2"n_______"}'
            done</etc/passwd


            OUTPUT



            [root@967dd7743677 test]# while read line; do echo $line | awk -F':' '{ print "Username: " $1 "nPassword: " $2 "n______" }'; done</etc/passwd
            Username: root
            Password: x
            ______
            Username: bin
            Password: x
            ______
            Username: daemon
            Password: x
            ______





            share|improve this answer













            You can use following shell script. I just wrote it for two fields such as username and password. I hope you can manage for other fields.



            Script



            while read line
            do
            echo $line | awk -F':' '{print "Username: "$1"nPassword: "$2"n_______"}'
            done</etc/passwd


            OUTPUT



            [root@967dd7743677 test]# while read line; do echo $line | awk -F':' '{ print "Username: " $1 "nPassword: " $2 "n______" }'; done</etc/passwd
            Username: root
            Password: x
            ______
            Username: bin
            Password: x
            ______
            Username: daemon
            Password: x
            ______






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 13:06









            rɑːdʒɑrɑːdʒɑ

            1,62232650




            1,62232650








            • 1





              If you're parsing with awk, why are you wrapping it in a while read loop?

              – Paul Hodges
              Jan 2 at 14:12














            • 1





              If you're parsing with awk, why are you wrapping it in a while read loop?

              – Paul Hodges
              Jan 2 at 14:12








            1




            1





            If you're parsing with awk, why are you wrapping it in a while read loop?

            – Paul Hodges
            Jan 2 at 14:12





            If you're parsing with awk, why are you wrapping it in a while read loop?

            – Paul Hodges
            Jan 2 at 14:12


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006829%2fprinting-out-name-for-each-row-of-etc-passwd-file-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]