Skip to main content


Showing posts from January, 2014

Python Script : Delete or Keep only those lines contain a word

Hello friends,

Very often we need to delete all the lines from a text file that have some word. Or quite the opposite, we need to retain only those line and delete all other lines. Here is how:

Keep lines have some words. 


my_words = ['BAKDATA', 'zcat', 'poup']

with open('inputfile.txt') as oldfile, open('outputfile.txt', 'w') as newfile:
    for line in oldfile:
        if any(word in line for word in my_words):

1. This will delete all lines that do not contains words 'BAKDATA', 'zcat', 'poup' and write the result in new file outputfile.txt. Obeviously, inputfile.txt is the input file. If you do not want to specify path, put file and script in same folder.

Delete lines have some words. 


Python Script : Convert IPs to Hex

So you need to convert IP to Hex in regular basis. Till now you may be doing so manually, or by using some online tool. My script will convert all IPs to Hex at Once !!!

import fileinput, sys, os, subprocess
import binascii
import socket
open('hexfile.txt', 'w').close()['notepad.exe', 'hexfile.txt'])
for line in fileinput.input('hexfile.txt', inplace = True):
    line = binascii.hexlify(socket.inet_aton(line)).upper()
    line = line + '\n'
    sys.stdout.write(line)['notepad.exe', 'hexfile.txt'])



1. Opens a notepad. Put one IP in one line. This will convert all IP's to Hex.

2. You are not entering any input to python console, so if you save this file with extension with .pyw instead of .py, then python console will not open and a notepad will be o…