2016-10-13 3 views
1

그래서 내가 키바 지수와 상관 관계가 문자열 목록을 가지고, 문자열은 다음과 같이 :정렬 목록

λ 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 

날짜는 분류되지 않은 돌아오고있다. 이 인덱스 (날짜)로 정렬하려면 filebeat-2016-10.xx ASC 또는 DESC가 정상입니다.

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]) 

을하지만 의미있는 방식으로 정렬 할 수 없습니다 : 지금의 약자로서

나는 다음과 같은 문자열을 격리 할 것.

+0

를' kibanaIndices' list, 어떻게 보이는지 보여 줄 수 있니? –

+0

kibanaIndices 목록은 위의 목록과 똑같습니다. 실수로 제거하지 않으려는 .kibana의 기본 색인이있는 행이 있습니다.이 스크립트는 로그 순환 용이므로 for 루프는 해당 항목을 제외하고 –

+0

각 행이'yellow open filebeat-'로 시작하면 일반 정렬이 작동합니다. 정렬을 위해 시도한 것을 보여주십시오. –

답변

1

필요한 것은 무엇입니까? 여기 extract_date

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)) 

는 세 번째 열을 리턴하는 함수 (filebeat-2016.10.12을 등)이다. 이 값을 sortkey 인수로 사용하여이 값을 정렬 키로 사용합니다. 날짜 형식은 문자열로 정렬 할 수 있습니다. 아마도 좀 더 정교한 extract_line 함수를 사용하여 날짜 만 추출 할 수 있습니다.

+0

이것은 나에게 잘 돌아갔다. 도와 줘서 고마워. :) –

0

내가 참조한 서버에 액세스 할 수 없어서 샘플 데이터를 UTF-8로 텍스트 파일에 복사했습니다. 목록 보급 및 문자열 메소드를 사용하여 데이터를 지우고 구성 요소로 분해 할 수 있습니다. 정렬은 내장 정렬() 메소드에 대한 인수로 람다 함수를 통과함으로써 달성된다 : 여기 정렬에 https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions

기타 정보 : 여기 지능형리스트에

# read text data into list one line at a time 
result_lines = open('kibana_data.txt').readlines() 

# remove trailing newline characters 
clean_lines = [line.replace("\n", "") for line in result_lines] 

# get first line of file 
info = clean_lines[0] 

# create list of field names 
header = [val.replace(" ", "") 
      for val in clean_lines[1].split()] 

# create list of lists for data rows 
data = [[val.replace(" ", "") for val in line.split()] 
     for line in clean_lines[2:]] 

# sort data rows by date (third row item at index 2) 
final = sorted(data, key=lambda row: row[2], reverse=False) 

상세 정보 https://wiki.python.org/moin/HowTo/Sorting