3

My software outputs these two types of output:

-rwx------ Administrators/Domain Users  456220672   0% 2018-04-16 16:04:40 E:\\_WiE10-18.0.100-77.iso

-rwxrwx--- Administrators/unknown        6677   0% 2018-04-17 01:33:23 E:\\program files\\cluster groups\\sql server (mssqlserver)\\logs\\progress-MOD-1523883344023-3001-Windows.log

I would like to get the file names from both outputs:

  • E:\\_WiE10-18.0.100-77.iso, for the first one
  • E:\\program files\\cluster groups\\sql server (mssqlserver)\\logs\\progress-MOD-1523883344023-3001-Windows.log, for the second one

If i use something like the code below, it won't work if the second parameter has spaces in it. It works if there aren't any spaces in the Domain Username.

for item in outputs:
    outputs.extend(item.split())
for item2 in [' '.join(outputs[6:])]:
    new_list.append(item2)

How can I get all the parameters individually, including the filenames?

1
  • 2
    E. g. with a regular expression like \b[A-Z]:\\\\.+ Commented Apr 22, 2018 at 12:23

3 Answers 3

4

If regex is an option:

text = """-rwx------ Administrators/Domain Users  456220672   0% 2018-04-16 16:04:40 E:\\_WiE10-18.0.100-77.iso

-rwxrwx--- Administrators/unknown        6677   0% 2018-04-17 01:33:23 E:\\program files\\cluster groups\\sql server (mssqlserver)\\logs\\progress-MOD-1523883344023-3001-Windows.log"""

import re

for h in re.findall(r"^.*?\d\d:\d\d:\d\d (.*)",text,flags=re.MULTILINE):
    print(h)

Output:

E:\_WiE10-18.0.100-77.iso
E:\program files\cluster groups\sql server (mssqlserver)\logs\progress-MOD-1523883344023-3001-Windows.log

Pattern explained:

The pattern r"^.*?\d\d:\d\d:\d\d (.*)" looks for linestart '^' + as less anythings as possible '.*?' + the time-stamp '\d\d:\d\d:\d\d ' followed by a space and captures all behind it till end of line into a group.

It uses the re.MULTILINE flag for that.


Edit:

Capturing the individual things needs some more capturing groups:

import re

for h in re.findall(r"^([rwexXst-]+) ([^0-9]+) +\d+.+? +(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (.*)",text,flags=re.MULTILINE):
#                       ^^^^^^^^^^^^ ^^^^^^^^           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^
#                          flags     grpName                               datetime          filename
    for k in h:
        print(k)
    print("")

Output:

-rwx------
Administrators/Domain Users 
2018-04-16 16:04:40
E:\_WiE10-18.0.100-77.iso

-rwxrwx---
Administrators/unknown       
2018-04-17 01:33:23
E:\program files\cluster groups\sql server (mssqlserver)\logs\progress-MOD-1523883344023-3001-Windows.log
Sign up to request clarification or add additional context in comments.

8 Comments

How can i get all parameters individually like if i only need permissions(first one) and date and time as well?
i got the first one : for fileperm in re.findall(r"([^ ]+) .*",backup_files_val,flags=re.MULTILINE): print(fileperm)
@PetPan you would need more capturing groups. Look: regexr.com/3oart .. something along '^([rwexXst-]+) ([^0-9]+) +\d+.+? +(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (.*)' - groups 1 to 4 will give you the different capturing groups.
@PetPan look at the regex - the filesize is currently not captured: change regex to re.findall(r"^([rwexXst-]+) ([^0-9]+) +(\d+) .+? +(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) (.*)",text,flags=re.MULTILINE):
i Slightly modified it to get the full filesize, your regex was missing + sign for the following ^([rwexXst-]+) ([^0-9]+) +(\d+).... now, its working, thanks again. cheers
|
2

You could use a regular expression like

\b[A-Z]:\\\\.+

Comments

1

Aside from using regex, you can try something similar to this.

output = '-rwx------ ... 2018-04-16 16:04:40 E:\\\\_WiE10-18.0.100-77.iso'

drive_letter_start = output.find(':\\\\')
filename = output[drive_letter_start - 1:]

It looks for the first occurrence of ':\\'and gets the drive letter before the substring (i.e. ':\\') and the full file path after the substring.

EDIT
Patrick Artner's answer is better and completely answers OP's question compared to this answer. This only encompasses capturing the file path. I am leaving this answer here should anyone find it useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.