#This python codes reads a file and search for an specified keywords. Based on # the line number of the line containing that keyword, it moves few lines back- # ward or forward to seach for another keywords and then prints the entire line # containing that keyword. # #Get Line Number of certain phrase in file Python # #Add a counter to the line number # #Move the position of a file pointer by specified number of line # ----------------------------------------------------------------------------- import sys, os import numpy as np lnum = 0 #Number of lines to move backward from the line containing identified keyword ldel = 2 #Keywords stg = "Heat Flux" lbak = ldel + 1 #Get input file inFile = str(sys.argv[1]) f = open(inFile, "r") lnLst = f.readlines() f.close() with open(inFile) as f: while line := f.readline(): lnum = lnum + 1 if stg in line: print(line.rstrip(), "at line ->", lnum) if lnum > ldel: print(lnLst[lnum-lbak].rstrip()) print("-------------------------------------------------------")