I am trying to sort a specific column from the given text file. The text file contains below information.
Interface Link Input packets (pps) Output packets (pps)
ge-0/0/0 Up 0 (0) 0 (0)
gr-0/0/0 Up 0 (0) 0 (0)
ip-0/0/0 Up 0 (0) 0 (0)
...
ge-0/0/8 Down 0 (0) 0 (0)
ge-0/0/9 Down 0 (0) 0 (0)
fxp2 Up 0 87559
gre Up 0 0
ipip Up 0 0
I am trying to sort the column Output packets.
I have with Pandas (pd) but Pandas can't read from a text file. So so I tried to create a list and to split it into columns with the " " and "\t" delimiter however that was unsuccessful. In fact, the whole text file comes in single list.
Third I tried to break the first line and keep it constant and then from line 2 below I tried to sort the list but it didn't work.
How can I do this?
import sys
import os
import io
import pandas as pd
import itertools
token = open ('testfile', 'r')
linestoken = token.readline()
print(linestoken)
for line in itertools.islice(token, 2, 20):
#print(line)
def my_sort(line):
line_fields = line.strip().split(' ')
#print(line_fields)
packets = line_fields[4]
return packets
temp = list(line)
temp.sort(key=my_sort)
#for line in contents:
#print(line)
print(temp)
token.close()