Python In Lunix - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Python In Lunix (/thread-3290.html) |
Python In Lunix - Wizard - May-11-2017 Hello World! I am in dire need of assistance on a current assignment in my IT class. We are using Linux terminals from our schools system to write code, and only two classes were dedicated to learning this in the year. Obviously I am struggling and cannot figure out how to even start. When it comes to the arguments, I believe I can handle it, however I am lacking the knowledge on where to begin and get rolling on the assignment, here is the HW description. Regular expression for user account analysis Write a Python scripting that uses the regular expression to find out:
RE: Python In Lunix - nilamo - May-11-2017 Start with something simple, that you can test over and over, like maybe this: import re regex = re.compile(r"your-regex-here") test_strings = [ "Jose", "Jjunior", "4chr", "letters", "ending-in-number5", "athenbthenc", "Capital", "doesn'tmatch@nyth1ng" ]I'd suggest using something like regexpal (http://www.regexpal.com/) to test out your regex quickly to find something that works for each case. But really, there's almost no python involved here... it's all just regular expressions. RE: Python In Lunix - Wizard - May-11-2017 I understand what you are showing, however am still confused on how to write arguments that pull the accounts from the school's Linux terminal? RE: Python In Lunix - nilamo - May-11-2017 That depends entirely on where the accounts are stored. You can either ask Linux nicely to tell you everyone with a local account, or you connect to your school's database and query some sort of users table. https://askubuntu.com/a/410274 Wrote:To list all local users you can use: RE: Python In Lunix - Wizard - May-11-2017 I see! So when now that I acquired the account names (30+). I opened a new python file by the name of test.py, and gave the shebang along with the subprocess. Is this the write Idea or am I completely off track, do I even need to go into a script or should I be staying in the main terminal? RE: Python In Lunix - wavic - May-11-2017 You don't need subprocess to repeat the @nilamo's example. Read the file using the csv module and delimiter ':'. Get the first element of each row. RE: Python In Lunix - Wizard - May-11-2017 I am having trouble making this work, any thoughts? Perhaps I am not executing it correctly? import sys import re data = sys.stdin.readlines() list1 = [] #For storing strings that start with only one j list2 = [] #For storing strings that start with only two j list3 = [] #For storing strings that have exactly 4 characters list4 = [] #For storing strings that only have English letters list5 = [] #For storing strings that are ended in numbers list6 = [] #For storing strings that start with 'a' and involve one or more than one 'b' list7 = [] #For storing strings that start with any Capital letter for i in data: i=i.strip() if re.search(r'^[j][^j]\w*',i): list1.append(i) if re.search(r'^[j][j][^j*]',i): list2.append(i) if re.search(r'^(.{4})$',i): list3.append(i) if re.search(r'^[A-Za-z]*$',i): list4.append(i) if re.search(r'^.*\d$',i): list5.append(i) if re.search(r'^a[b]{1,}$',i): list6.append(i) if re.search(r'^[A-Z].*$',i): list7.append(i) print "Printing user accounts that start with only one j" print list1 print "Printing user accounts that start with only two j" print list2 print "Printing user accounts that have exactly 4 characters" print list3 print "Printing user accounts that only have English letters" print list4 print "Printing user accounts that are ended in numbers" print list5 print "Printing user accounts that start with 'a' and involve one or more than one 'b'" print list6 print "Printing user accounts that start with any capital letter" print list7 RE: Python In Lunix - nilamo - May-11-2017 Please use code tags. Without the indentation, it'd give an error to anyone who ran it... a syntax error. Also, please give some more details about what isn't working about it. Are you getting errors? What are they? Or is your output just not what you expected it to be? In that case, what output DO you get? RE: Python In Lunix - Ofnuts - May-12-2017 Your 6 is wrong, it won't match axxxxbxxxx . Others look right, even though they often include more brackets and parentthes than really need ([j] and j are the same thing, for instance).
|