I need to grab all the palindrome lines (spelled same forwards and backwards) in a text file from the linux command line. I do not think you can use just one command to do this since you have to check for any length of line. So I do not know of a way to do this other than making a script (correct me if I'm wrong). So what I did is grab all of the lengths of lines and did a grep command that would check for lines that are that long:
import os
ex_front = "egrep -i '^"
ex_middle_front = ""
ex_middle_back = ""
ex_back = "$' textfile.txt"
textFile = open("textfile.txt", "r")
stringList = textFile.readlines()
lengthList = set([])
for line in stringList:
lengthList.add(len(line))
for x in lengthList:
ex_middle_front = ""
ex_middle_back = ""
for i in range(int(x/2), 0, -1):
ex_middle_front += "(.)"
ex_middle_back += "\\" + str(i)
if x % 2 == 0:
ex_middle = ""
else:
ex_middle = "."
os.system(ex_front + ex_middle_front + ex_middle + ex_middle_back + ex_back)
This works, but I am wondering if there is a better solution.