Skip to content
Advertisement

How do i split output of “who -m”command in linux?

I want to split output of who command and store output in variable

EX.

[root@ip ~]# who -m
root_user pts/1        2015-09-16 08:32 (windows-host.net)

I want to split this output like

var1 = root_user

var2 = 2015-09-16

var3 = 08:32

var4 = windows-host.net (This is hostname)

Advertisement

Answer

The following should work:

import re

text = "root_user pts/1        2015-09-16 08:32 (windows-host.net)"
var1, var2, var3, var4 = re.search(r'(.*?)s.*?(d{4}-d{2}-d{2})s(d{2}:d{2})s+((.*))', text).groups()

print 'var1 =', var1
print 'var2 =', var2
print 'var3 =', var3
print 'var4 =', var4

Giving:

var1 = root_user
var2 = 2015-09-16
var3 = 08:32
var4 = windows-host.net
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement