print all lines of a file which contain a specific string - regex

I want to search and a log file for specific informations which are contained in the file and get the whole line printed out
for example line 32 in a .log file is:
2019-08-07 15:21:09.783 'lineid' -> 'DEU.DTAG.NBGNE00111'
I want every line printed out that includes the word lineid.
If someone could help me with my problem I would very appreciate it.
thanks
I am quite new to ruby and regex so I tried by searching something from the internet.
File.open("/Users/filip/Documents/Testcases/NG-ART_TC1054921_MKS+Results1/1054921_1042510_2242_TID-2123/TC1042510-TID2123-sequencer.log", "r") do |file|
for line in file.readlines().include? "lineid"
puts line
end
end
what I am getting back is only
/Users/filip/Documents/cucumber/features/features/step_definitions/lineid.rb:7: syntax error, unexpected end-of-input, expecting end

In ruby is recommended to use each to iterate, you don't usually use for loops.
You could try this:
File.foreach("/Users/filip/Documents/Testcases/NG-ART_TC1054921_MKS+Results1/1054921_1042510_2242_TID-2123/TC1042510-TID2123-sequencer.log").each do |line|
puts line if line.include?('lineid')
end
This will execute the given block for each line in the file without slurping the entire file into memory.
See: IO::foreach
Making your example work would look like:
File.open("/Users/filip/Documents/Testcases/NG-ART_TC1054921_MKS+Results1/1054921_1042510_2242_TID-2123/TC1042510-TID2123-sequencer.log", "r") do |file|
file.readlines().each do |line|
puts line if line.include?("erc")
end
end

Related

Get the first x lines of a File

I'm trying to get the first 10 lines from a file into a string and write them to another file.
I've got:
File.open("read_file.txt", "r") do |rf|
File.open("write.txt", "w") do |wf|
rf.each_line.with_index do |line, idx|
break if idx > 9
wf.puts(line)
end
end
end
Is there a more elegant and efficient way to break on a specified number of lines
Ideally something like file.lines(3) : String would be nice but that certainly isn't available.
Here is a more elegant way (but with the same efficiency, I believe)
File.open "read_file.txt" do |io|
File.write "write.txt", io.each_line.first(10).join("\n")
end
Read the lines of the file with File#read_lines and then take the first 10 lines:
File.read_lines("file")[0..9]

Find and remove specific string from a line

I am hoping to receive some feedback on some code I have written in Python 3 - I am attempting to write a program that reads an input file which has page numbers in it. The page numbers are formatted as: "[13]" (this means you are on page 13). My code right now is:
pattern='\[\d\]'
for line in f:
if pattern in line:
re.sub('\[\d\]',' ')
re.compile(line)
output.write(line.replace('\[\d\]', ''))
I have also tried:
for line in f:
if pattern in line:
re.replace('\[\d\]','')
re.compile(line)
output_file.write(line)
When I run these programs, a blank file is created, rather than a file containing the original text minus the page numbers. Thank you in advance for any advice!
Your if statement won't work because not doing a regex match, it's looking for the literal string \[\d\] in line.
for line in f:
# determine if the pattern is found in the line
if re.match(r'\[\d\]', line):
subbed_line = re.sub(r'\[\d\]',' ')
output_file.writeline(subbed_line)
Additionally, you're using the re.compile() incorrectly. The purpose of it is to pre-compile your pattern into a function. This improves performance if you use the pattern a lot because you only evaluate the expression once, rather than re-evaluating each time you loop.
pattern = re.compile(r'\[\d\]')
if pattern.match(line):
# ...
Lastly, you're getting a blank file because you're using output_file.write() which writes a string as the entire file. Instead, you want to use output_file.writeline() to write lines to the file.
You don't write unmodified lines to your output.
Try something like this
if pattern in line:
#remove page number stuff
output_file.write(line) # note that it's not part of the if block above
That's why your output file is empty.

Python - using raw_input() to search a text document

I am trying to write a simple script that a user can enter what he/she wants to search in a specified txt file. If the word they searching is found then print it to a new text file. This is what I got so far.
import re
import os
os.chdir("C:\Python 2016 Training")
patterns = open("rtr.txt", "r")
what_directory_am_i_in = os.getcwd()
print what_directory_am_i_in
search = raw_input("What you looking for? ")
for line in patterns:
re.findall("(.*)search(.*)", line)
fo = open("test", "wb")
fo.write(line)
fo.close
This successfully creates a file called test, but the output is nothing close to what word was entered into the search variable.
Any advice appreciated.
First of all, you have not read a file
patterns = open("rtr.txt", "r")
this is a file object and not the content of file, to read the file contents you need to use
patterns.readlines()
secondly, re.findall returns a list of matched strings, so you would want to store that. You regex is also not correct as pointed by Hani, It should be
matched = re.findall("(.*)" + search + "(.*)", line)
rather it should be :
if you want the complete line
matched = re.findall(".*" + search + ".*", line)
or simply
matched = line if search in line else None
Thirdly, you don't need to keep opening your output file in the for loop. You are overwriting your file everytime in the loop so it will capture only the last result. Also remember to call the close method on the files.
Hope this helps
you are searching here for all lines that has "search" word in it
you need to get the lines that has the text you entered in the shell
so change this line
re.findall("(.*)search(.*)", line)
to
re.findall("(.*)"+search+"(.*)", line)

In python how can i print only the first matching line from the log output?

I have input like this
line 1: [DEBUG]...
line 2: [DEBUG]...
line 2: [DEBUG]...
From this I want to print only the first matching string meaning only
The first matching line 1: [DEBUG] and stop the traversing.
I have tried the code below:
for num1,line1 in enumerate(reversed(newline),curline):
ustr1="[DEBUG]"
if ustr1 in line1:
firstnum=num1
Can anyone help me in this?
your question is not formatted very well, I cannot really see what your object is looking like etc.... I'd assume it's this way:
input="1: [DEBUG]....\n2: [DEBUG]...\n3:...."
# now you could do e.g.:
print(input.split("\n")[0].strip("\r"))
# which would be the first line. as you are searching a line containing a certain string "ustr1", you could do:
for line in input.split("\n"):
if ustr1 in line:
print(line)
break #as you dont want more than one line
#furthermore, if you need the index of the line, do:
for i,line in enumerate(input.split("\n")):
pass #i = index, line = line
Hope i understood it right ;)

Python3.4 : Matching and returning list values

I have opened a text file in Python which has thousands of lines. I need to search each line to see if it contains 1 of many different specified values. I then need to return the specific value and the corresponding line that contains that value.
q1 = open('/home/lost/StockRec/StockIndex/edgar.full-index.2015.QTR1.master.idx', 'r')
list = ['1341234', '12341234', '4563456', '12341234', '6896786', '2727638']
for line in q1:
for listValue in list:
if listValue in line:
print(listValue, line)
I know this code is wrong. I need to search each line in q1 for each of the specific values in the list. I need to then print the specific list value and the line containing that value.
Unless your file is already somehow separated into lines, it looks like you will have to first split the file into lines when you import it. Right now it is returning all of it because q1 is only one line.
Look for some identifying information in your file such as new line characters ('\n') or if each line starts with a specific character.
so once you open the file you will include:
q1.split('your identifying character here')
That will split the copy of your file then you can perform the loops that you have already written