Skip to content
String partition

String partition

Extract output between two markers with partition()

Marker strings isolate command output when the response also contains logs, templates, warnings, or unrelated application content.

The command writes a unique marker before and after its output:

start_marker = "COMMAND_OUTPUT_START"
end_marker = "COMMAND_OUTPUT_END"

wrapped_command = f"echo {start_marker}; {command}; echo {end_marker}"

partition() splits a string at the first occurrence of the separator and returns three values: the text before it, the separator that was found, and the text after it. Applying it twice isolates the text between both markers.

def parse_output(response_text):
    before_output, start_found, after_start = response_text.partition(start_marker)
    command_output, end_found, after_output = after_start.partition(end_marker)
    parsed_output = command_output.strip()
    return parsed_output

Response body this parses

application content
COMMAND_OUTPUT_START
uid=33(www-data) gid=33(www-data)
COMMAND_OUTPUT_END
remaining application content

Returned value

uid=33(www-data) gid=33(www-data)

start_found and end_found contain the matching marker when it exists, or an empty string when it does not. before_output and after_output retain the discarded response content.

Find by: partition, string partition, split at marker, start marker, end marker, extract between markers, command output, noisy response, in band output, response parsing