-1

This is my code:

grouplist = open("/etc/group" , "r")
with grouplist as f2:
    with open("group" , "w+") as f1:
    f1.write(f2.read())
    f1.seek(0,0)
    command = f1.read()
    print
    print command

What command can I use to make it display only the names of the users without the ":x:1000:"

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kulyuly
  • 43
  • 4
  • Do not understand, how `grep` and `debian` tags are involved. Do you want solution with `python`? – John_West Feb 23 '16 at 14:02
  • 2
    I'd recommend using the [`grp` module](https://docs.python.org/2/library/grp.html) in Python's standard library rather than parsing `/etc/group` directly. – Sven Marnach Feb 23 '16 at 14:05
  • I use python on debian and I need to use some sort of grep inthis command. Also I need to do this without using any modules. – Kulyuly Feb 23 '16 at 14:08
  • 1
    "I need to do this without using any modules" – I can't see any valid reason for this requirement. – Sven Marnach Feb 23 '16 at 14:35

2 Answers2

0

How about split() [1] [2]

with open("/etc/group" , "r") as f2:
   for line in f2:
       list1=line.split(str=":")
       print list1[0]
Community
  • 1
  • 1
John_West
  • 2,239
  • 4
  • 24
  • 44
  • How come it only works for the first line in the file? as in it only works for "root" – Kulyuly Feb 23 '16 at 14:10
  • 1
    You should add a cycle. I answered exactly the question "What command can I use to make it display only the names of the users without the ":x:1000:"" – John_West Feb 23 '16 at 14:15
0

You almost achieved the target. With a little fix in your code, here is the solution.

 with open("/etc/group" , "r") as source:
    with open("./group" , "w+") as destination:
        destination.write(source.read())
        source.seek(0,0)
        for user in source.readlines():
            print user[: user.index(':')]

Nevertheless, this only show the names, but still copy the original file.

This way you write only the names in the new file.

with open("/etc/group" , "r") as source:
    with open("./group" , "w+") as destination:
        for user in source.readlines():
            u = user[: user.index(':')]
            destination.write('%s\n' % u)
            print u
Mauro Baraldi
  • 6,346
  • 2
  • 32
  • 43