A_Elric A_Elric - 1 month ago 6
Python Question

Sort list of strings with data in the middle of them

So I have a list of strings which correlates to kibana indices, the strings look like this:

λ curl '10.10.43.210:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size
yellow open filebeat-2016.10.08 5 1 899 0 913.8kb 913.8kb
yellow open filebeat-2016.10.12 5 1 902 0 763.9kb 763.9kb
yellow open filebeat-2016.10.13 5 1 816 0 588.9kb 588.9kb
yellow open filebeat-2016.10.10 5 1 926 0 684.1kb 684.1kb
yellow open filebeat-2016.10.11 5 1 876 0 615.2kb 615.2kb
yellow open filebeat-2016.10.09 5 1 745 0 610.7kb 610.7kb


The dates are coming back unsorted. I want to sort these by the index (which is a date) filebeat-2016-10.xx ASC or DESC is fine.

As it stands now I isolate the strings like this:

subp = subprocess.Popen(['curl','-XGET' ,'-H', '"Content-Type: application/json"', '10.10.43.210:9200/_cat/indices?v'], stdout=subproce$
curlstdout, curlstderr = subp.communicate()
op = str(curlstdout)
kibanaIndices = op.splitlines()
for index,elem in enumerate(kibanaIndices):
if "kibana" not in kibanaIndices[index]:
print kibanaIndices[index]+"\n"
kibanaIndexList.append(kibanaIndices[index])


But can't sort them in a meaningful way.

Answer

Is it what you need?

lines = """yellow open   filebeat-2016.10.08   5   1        899            0    913.8kb        913.8kb
yellow open   filebeat-2016.10.12   5   1        902            0    763.9kb        763.9kb
yellow open   filebeat-2016.10.13   5   1        816            0    588.9kb        588.9kb
yellow open   filebeat-2016.10.10   5   1        926            0    684.1kb        684.1kb
yellow open   filebeat-2016.10.11   5   1        876            0    615.2kb        615.2kb
yellow open   filebeat-2016.10.09   5   1        745            0    610.7kb        610.7kb
""".splitlines()
def extract_date(line):
    return line.split()[2]
lines.sort(key=extract_date)
print("\n".join(lines))

Here extract_date is a function that returns third column (like filebeat-2016.10.12). We use this function as key argument to sort to use this value as a sort key. You date format can be sorted just as strings. You can probably use more sophisticated extract_line function to extract only the date.