从ls -l命令的输出中提取模式

时间:2014-08-04 15:19:29

标签: python regex

我想从ls -1hl命令中提取信息。

我必须在不同的远程服务器上运行此命令,并从每个远程服务器上获得不同类型的输出,因为其中一些是solaris,其中一些是GNU linux

这里是ls -1hl命令的片段,我想格式化: -

ssh user@server 'ls -1hl /path/to/directory'

ls_command: -

-rw-r-----   1 root groupA     0 2014-08-04 10:42 a.log
-rw-------   1 root  groupA  48132720 Aug  1 23:45 core
-rwxr-xr-x   1 root  groupA      208 Mar 13 17:18 restart.sh
lrwxrwxrwx 1 root groupA   49 2014-06-06 09:32 MerSem -> ../combined_ABC/scripts
drwxrwxrwx   3 root groupA     1.0K Apr 22 16:12 logs

我想从上面的代码段中提取以下信息: -

第一行

permission = -rw-r-----  
links = 1
owner = rooot
group = groupA
size = 0
date = 2014-08-04 10:42
File/Directory Name = a.log

我的代码: -

Final_list = []
result = ls_command.split('\n')
for line in result:
   coldata = []
   a = re.search(r"^(.+?)\s{1,3}.*$",line)
   b = re.search(r"^.+?\s{1,3}(\d{1,5}).*$",line)
   c = re.search(r"^.+?\s{1,3}\d{1,5}(.+?)\s{1,3}.*$",line)
   d = re.search(r"^.+?\s{1,3}\d{1,5}.+?\s(.+?)\s{1,3}.*$",line)
   e = re.search(r"^.+?\s{1,3}\d{1,5}.+?\s.+?\s{1,3}([0-9.A-Za-z]+?)\s{1,3}.*$",line)
   f = re.search(r"^.+?\s{1,3}\d{1,5}.+?\s.+?\s{1,3}[0-9.A-Za-z]+?\s{1,3}(.+?)\s{1,3}.*$",line)
   g = re.search(r"^.*\s{1,3}(.+)$",line)
   if a:
           permissions = a.group(1)
   if b:
           links = b.group(1)
   if c:
           owner = c.group(1)
   if d:
           group = d.group(1)
   if e:
           size = e.group(1)
   if f:
           date = f.group(1)
   if g:
           filename = g.group(1)
   if a and b and c and d and e and f and g:
           coldata.append(permissions)
           coldata.append(links)
           coldata.append(owner)
           coldata.append(group)
           coldata.append(size)
           coldata.append(date)
           coldata.append(filename)
           Final_list.append(coldata)

但是这段代码不适用于我得到的那种日期。

Print Final_list

 [['-rw-r-----','1','root','groupA','0','2014-08-04','a.log'],['-rw-------','1','root','groupA','48132720','Aug','core'],['-rwxr-xr-x','1','root','groupA','208','Mar','restart.sh'],['lrwxrwxrwx','1','root','groupA','49','2014-06-06','../combined_ABC/scripts'],['drwxrwxrwx','3','root','groupA','1.0K','Apr','logs']]

预期产出: -

[['-rw-r-----','1','root','groupA','0','2014-08-04 10:42 ','a.log'],['-rw-------','1','root','groupA','48132720','Aug  1 23:45','core'],['-rwxr-xr-x','1','root','groupA','208','Mar 13 17:18','restart.sh'],['lrwxrwxrwx','1','root','groupA','49','2014-06-06 09:32','MerSem -> ../combined_ABC/scripts'],['drwxrwxrwx','3','root','groupA','1.0K','Apr 22 16:12','logs']]

1 个答案:

答案 0 :(得分:0)

s="""
-rw-r-----   1 root groupA     0 2014-08-04 10:42 a.log
-rw-------   1 root  groupA  48132720 Aug  1 23:45 core
-rwxr-xr-x   1 root  groupA      208 Mar 13 17:18 restart.sh
lrwxrwxrwx 1 root groupA   49 2014-06-06 09:32 MerSem -> ../combined_ABC/scripts
drwxrwxrwx   3 root groupA     1.0K Apr 22 16:12 logs
"""

line = s.lstrip().split("\n")[0].split() # split on newline and split the first line 

permission = line[0]
links = line[1]
owner = line[2]
group = line[3]
size = line[4]
date = line[5]
Name = line[-1]

print permission,links,owner,group,size,date,Name
-rw-r----- 1 root groupA 0 2014-08-04 a.log