Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python In Lunix
#1
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:
  • The user accounts that starts with exactly one “j”
  • The user accounts that starts with exactly two “j”s
  • The user accounts that have exactly 4 characters
  • The user accounts that only have English letters
  • The user accounts that are ended with numbers
  • The user accounts that start with “a” and involve one or more than one “b”s 
  • The user accounts that start with any capital letter
Thank you for all and any help!
Reply
#2
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.
Reply
#3
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?
Reply
#4
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:

cut -d: -f1 /etc/passwd
To list all users capable of authenticating (in some way), including non-local, see this reply: https://askubuntu.com/a/414561/571941
Reply
#5
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?
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
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
Reply
#8
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?
Reply
#9
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).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020