Printing out name for each row of /etc/passwd file output
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
add a comment |
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
1
Read up on awk.
– Shawn
Jan 2 at 13:07
add a comment |
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
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
linux bash scripting
asked Jan 2 at 12:58
O. SherlockO. Sherlock
192
192
1
Read up on awk.
– Shawn
Jan 2 at 13:07
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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
______
1
If you're parsing withawk
, why are you wrapping it in awhile read
loop?
– Paul Hodges
Jan 2 at 14:12
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 2 at 16:06
answered Jan 2 at 14:12
Paul HodgesPaul Hodges
3,7501524
3,7501524
add a comment |
add a comment |
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
______
1
If you're parsing withawk
, why are you wrapping it in awhile read
loop?
– Paul Hodges
Jan 2 at 14:12
add a comment |
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
______
1
If you're parsing withawk
, why are you wrapping it in awhile read
loop?
– Paul Hodges
Jan 2 at 14:12
add a comment |
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
______
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
______
answered Jan 2 at 13:06
rɑːdʒɑrɑːdʒɑ
1,62232650
1,62232650
1
If you're parsing withawk
, why are you wrapping it in awhile read
loop?
– Paul Hodges
Jan 2 at 14:12
add a comment |
1
If you're parsing withawk
, why are you wrapping it in awhile 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Read up on awk.
– Shawn
Jan 2 at 13:07