-1

I have the following test.txt data, and I want to to check the occurrence of each state and show it as summary output. I am able to count how many times which state occurred using following logic

for line in file:
    if 'success' in line: count+=1

but I am unable to add a column and append it to the end like my output. Any help will be highly appreciated.

Sample Input
-------------------------

11/11/2015              9.9.9.9   30s        success

11/11/2015              9.9.9.8   30s        stuck

11/11/2015              9.9.9.9   30s        Sync

11/11/2015              9.9.9.9   30s        success

11/12/2015              9.9.9.9   30s        success

11/12/2015              9.9.9.9   30s        stuck

11/12/2015              9.9.9.9   30s        stuck

11/12/2015              9.9.9.9   30s        success

11/12/2015              9.9.9.8   30s        success

11/12/2015              9.9.9.9   30s        success

11/12/2015              9.9.9.9   30s        stuck

11/12/2015              9.9.9.9   30s        success

11/12/2015              9.9.9.9   30s        Sync

11/12/2015              9.9.9.9   30s        Sync

--------------------Output I want -------------------------

11/11/2015              9.9.9.9   success         2

11/11/2015              9.9.9.8   stuck    1

11/11/2015              9.9.9.9   Sync         1

11/12/2015              9.9.9.9   success         4

11/12/2015              9.9.9.9   stuck    3

11/12/2015              9.9.9.9   Sync         2

11/12/2015              9.9.9.8   success         1
Sal
  • 5
  • 5

2 Answers2

0

Assuming this is a text file you're writing to you can use something like the following format but change it according to how you have your line and value set up.

with open('output.txt','w') as f:
    line = 'something'
    value = 2
    f.write(line + str(value))

This should give you the output 'something2'

To remove duplicates

for line in lines:
    read = []
    if line not in read:
       read.append(line)
       do something with it
    else:
       add current count to previous values

Not too sure how you have your script set up but this is just an idea.

tzee
  • 88
  • 8
  • that is helpful, thanks for taking me one step further, next how about removing the duplicate rows and combining my line to one, any similar insights to get me going there? – Sal Jul 12 '18 at 20:03
  • You can have a loop going where you save the lines that have already been read in a list of strings and if the next string is already in there just add the value together then use the write function I mentioned earlier. I'll add the approach as an edit. – tzee Jul 12 '18 at 20:25
  • /*file=open('logfile.txt' , 'r') contents=file.readlines() for line in contents: #print (line) if 'Established' in line: est += 1 if 'Syn' in line: syn += 1 if 'Failed' in line: fail += 1 for line in contents: if 'Established' in line: print(line + str(est)) if 'Syn' in line: print(line + str(syn)) if 'Failed' in line: print(line + str(fail)) */ I am stuck here, first I am unable to add the count in next column, any idea? – Sal Jul 14 '18 at 06:06
-1

Use dictionary to store the table, key as the combinations of the date and ip_address, value as counter.

ZhouQuan
  • 1,037
  • 2
  • 11
  • 19