Code explanation?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Code explanation?
# 1  
Old 12-18-2011
Code explanation?

I need someone to tell me how exactly works the following code αfter /etc/passwd Smilie:
Code:
cat /etc/passwd|grep "^[^:][^:][^:][^:]:"|sed '1,$s/^\([^:]*\):[^:]*:\([^:]*\):.*$/ \1 \2 /'|sort -nrk3 -t:

I want a good explanation to understand the code please
# 2  
Old 12-18-2011
Well... It's a cluster. The person who wrote it seems to me to be insane. Smilie

Code:
cat /etc/passwd | grep "^[^:][^:][^:][^:]:"

This greps for lines beginning with "XXXX:", where X is any character except a colon. So basically, this will only show you 4-letter users. ... Kinda weird, but.. okay. Also, the person that wrote it is perpetuating the misunderstanding that cat serves a function for single files. (Grep can take files as arguments.)

Code:
| sed '1,$s/^\([^:]*\):[^:]*:\([^:]*\):.*$/ \1 \2 /'

That is the most ridiculous thing I've ever seen. Unless I'm missing something big, the purpose of it is apparently to pull out only the username and the uid. It could have been accomplished so much more simply, clearly, and efficiently by replacing it with a cut command (this is what cut was made for!) like this:
Code:
| cut -d: -f1,3 --output-delimiter=' '

Lastly...
Code:
| sort -nrk3 -t:

What I think this is trying to do: sort the 4-letter users in descending order of their uid (so bob with uid 1100 would be before aaron with uid 505). However, the code is broken, probably because the person's over-complicated sed string is broken. To fix it, just change that trailing colon to a quoted space character:
Code:
' '

Look up those options in the sort man page if you're curious.

Long story short, here's what the command should be:
Code:
grep "^[^:][^:][^:][^:]:" /etc/passwd | cut -d: -f1,3 --output-delimiter=' ' | sort -nr -k2 -t' '


Last edited by ryran; 12-19-2011 at 10:04 AM..
This User Gave Thanks to ryran For This Post:
# 3  
Old 12-19-2011
Thank you very much for your help!I will try to learn it now!And you are right!The person who wrote this is completely insaneSmilie!In three hours he taught us the grep,sort,tr and sed command(not cut)!I dont know how can i learn this code with 3 hours of lesson,because i see these things for first time!Anyway if i have any more questions i will post here to help me again Smilie

---------- Post updated at 03:28 AM ---------- Previous update was at 03:00 AM ----------

As i said before he didn't taught us the cut command and now i have to learn the code without cut Smilie
In this command how do i understand pulling out only the username and the uid?
Code:
| sed '1,$s/^\([^:]*\):[^:]*:\([^:]*\):.*$/ \1 \2 /'

As i know according to this
Code:
http://www.cyberciti.biz/faq/understanding-etcpasswd-file-format/

the username is on the first field and the uid on the third,but i can't understand nothing on this code!Could you explain it without the cut command?
# 4  
Old 12-20-2011
Quote:
Originally Posted by kotsos13
As i said before he didn't taught us the cut command and now i have to learn the code without cut Smilie
In this command how do i understand pulling out only the username and the uid?
I'm sorry man. I love sed, and I use it for all kinds of things, but I would never use it for this. ... I don't understand how it's being used here, and I don't quite care to try and muddle through it and figure it out, though I'd be curious to have someone explain it. If your uh... unorthodox.. instructor won't explain it to you (and no one else answers), you could try to puzzle through it with the help of the sed info page (type info sed).

As a consolation prize, here's a command that does the same thing your instructor's original command was supposed to do, but more simply and more efficient. Maybe he'll be impressed. Smilie
HTML Code:
awk -F: '/^[^:][^:][^:][^:]:/ {print $1" "$3}' /etc/passwd | sort -t' ' -k2 -nr
# 5  
Old 12-21-2011
As far as i know he will teach us the awk command on the next lesson!I may not know how to use the awk but as i see now this command seems a lot helpful!

Last edited by kotsos13; 12-21-2011 at 02:36 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh code explanation

Hi. Can somebody please explain the following lines of KSH code for me? The code checks all sub directories in a specific location which are numbered (E.g. test_01, test_02 ... etc.), then finds the one with highest number and extracts that number from the dir name into the variable num. I'd just... (9 Replies)
Discussion started by: user052009
9 Replies

2. Homework & Coursework Questions

Minix 3 Source Code Organisation Explanation

1. I've been asked to provide a detailed description on how Minix's source code is organised. This may sound slightly simple, but what exactly am I to refer to when explaining? Any documents that would provide assistance to understand whats being asked? Thanks 2. Not applicable: 3.... (1 Reply)
Discussion started by: Menace12
1 Replies

3. UNIX for Dummies Questions & Answers

Explanation of NAWK Code

Hi all, I need your help in understanding the meaning and syntax of the below nawk line. Here is an extract of a script which I use daily and works well. The script extracts the hostnames and messages within a syslog file. I would also like to extract the message time in the 3rd column by... (2 Replies)
Discussion started by: wthomas
2 Replies

4. Shell Programming and Scripting

code explanation

Can you please explain the following code plz? my_cd=' ' while getopts :e: OPTION; do case "$OPTION" in e) my_cd ="$OPTARG";; esac done if ; then echo " >>> ERROR - I am wrong" echo " >>> ERROR - Hello" exit 99 fi What I don't understand is what is OPTION or... (3 Replies)
Discussion started by: RubinPat
3 Replies

5. UNIX for Dummies Questions & Answers

In need of explanation

Its great someone provided this script that strips out a filename and extension but can someone explain how each line works? file1='Jane Mid Doe.txt' newfile='Jane.txt' 1) ext=${file1##*.} 2) filename=${file%%.???} 3) set -- $filename 4) newfile="1.$extension" (1 Reply)
Discussion started by: Lillyt
1 Replies

6. Homework & Coursework Questions

Please help with the following fork code..with complete explanation

I have the following piece of codes. Please explain it to me in great detail how are these codes working. 1. #include <stdio.h> int main(){ int x; x=0; while (x<2 && fork()){ if (!fork()) execlp("echo","x++","x",0); x++; system("echo x+x"); } } 2. #include <stdio.h> int i;... (1 Reply)
Discussion started by: prakashabii
1 Replies

7. Shell Programming and Scripting

nawk code explanation

Hi I have a line/command which greps certain pattern () from the file and 22 lines AFTER that: nawk '/\/{c=22}c&&c--' input_file Can somebody explain the coding in this command (what " c&&c-- " does)? and how to modify this command to output 22 lines BEFORE this pattern () Thanks a lot ... (3 Replies)
Discussion started by: aoussenko
3 Replies

8. Shell Programming and Scripting

AWK deifnition and some small code explanation

Could someone give me a quick simple explanation for the AWK command. And also help me to explain the code i have made. I have made some general comments about it myself. I was wondering if people could help me with the rest: awk -F'' 'END { fmt = "%-20s\t%s\t%s\n" ... (0 Replies)
Discussion started by: amatuer_lee_3
0 Replies

9. Shell Programming and Scripting

tr explanation please

how does below tr command replace nonletters with newlines? I think I understand tr -cs '\n' part.. but what is A-Za-z\' <--- what is this?? tr -cs A-Za-z\' '\n' | -c --complement -s, --squeeze-repeats replace each input sequence of a repeated character that is... (0 Replies)
Discussion started by: convenientstore
0 Replies

10. Shell Programming and Scripting

Need explanation for the syntax(code)

Hi I am new to shell script programming... want to know the process of the following: if then echo "$0: missing argument for option(s) :$MISSINGOPTARG" echo "usage" $USAGE" exit 1 fi (1 Reply)
Discussion started by: chandhar
1 Replies
Login or Register to Ask a Question