2

Here is my code my aim is to print a specific whole line from large text file where string equal to "SAMPLE TYPE" when i tried its printing in the object how to print specified line from whole file

import re
target = open("debug.txt", "r")
for line in target:
    search=re.search("[SAMPLE TYPE]",line)
    v1=search
    print v1

Gives output of:

None
None
None
None
None
<_sre.SRE_Match object at 0x7f870b7d5578>in this place instead of this i need to print the specifed line and i dont want the lines which doesnt contain specifed string.

My input file data:

HLS| 04/14/16 17:56:58:933 |hls_tsr_module.cpp|ReceiveData              |418 |DEBUG: Data copied to TSD from TSR is 0
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp   |FrameType                |1341|DEBUG: AU_DELIM NALU Unit Type
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp   |FrameType                |1341|DEBUG: SEI NALU Unit Type
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp   |FrameType                |1341|DEBUG: NON_IDR_PICTURE NALU Unit Type
HLS| 04/14/16 17:56:58:933 |mpm_h264.cpp   |FrameType                |1377|DEBUG: B Frame Received
HLS| 04/14/16 17:56:58:933 |hls_tsd_module.cpp|ProcessVideoBuffer       |4151|DEBUG: SAMPLE TYPE: B - FRAME PTS: 8573002542 DTS: 8573002542 
HLS| 04/14/16 17:56:58:933 |hls_tsd_module.cpp|ProcessVideoBuffer       |4193|DEBUG: Video send pts 8573002542
HLS| 04/14/16 17:56:58:933 |hls_tsm_module.cpp|AlternateFrameInterLeave |11770|DEBUG: Audio Record Status is 1
HLS| 04/14/16 17:56:58:934 |hls_tsd_module.cpp|ProcessVideoBuffer       |4261|DEBUG: Frame video pts 8573004043
HLS| 04/14/16 17:56:58:934 |hls_tsm_module.cpp|AlternateFrameInterLeave |11770|DEBUG: Audio Record Status is 1
HLS| 04/14/16 17:56:58:934 |hls_tsd_module.cpp|SegmentStream            |1597|DEBUG: Not an AV/Subtitle Packet 256
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp   |FrameType                |1341|DEBUG: AU_DELIM NALU Unit Type
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp   |FrameType                |1341|DEBUG: SEI NALU Unit Type
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp   |FrameType                |1341|DEBUG: NON_IDR_PICTURE NALU Unit Type
HLS| 04/14/16 17:56:58:934 |mpm_h264.cpp   |FrameType                |1377|DEBUG: B Frame Received
HLS| 04/14/16 17:56:58:934 |hls_tsd_module.cpp|ProcessVideoBuffer       |4151|DEBUG: SAMPLE TYPE: B - FRAME PTS: 8573004043 DTS: 8573004043 
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
RaviTeja
  • 39
  • 4

5 Answers5

2
import os
os.system('grep -rnw "[FOLDER_NAME]" -e "SEARCH_STRING" > OP.txt')
  • if i want to open folder suppose containg debug1,2,3,4,5 files and check for same string means – RaviTeja Apr 20 '16 at 08:01
  • import os for filename in os.listdir(DIR_NAME): repeat_above_snippet – Niranj Rajasekaran Apr 20 '16 at 08:23
  • i cant get u its not working if suppose i had folder example ABC it contain different files 1,2,3,4,5 i have to read all the files at once and search for the string if file 1 has the string it has copy to the separate file of file1 in the same way file2 can u elaborate in detail so tht it could help me – RaviTeja Apr 27 '16 at 11:18
  • can u help me in printing tht string after sample type and remove unnecessary details – RaviTeja May 02 '16 at 14:23
  • I want to check my understanding, we have a folder ABC inside that we have 1.txt, 2.txt, 3.txt. Need to search a string say 'Ravi' in all these three files if found a match need to copy it to new file. Is this what you want? – Niranj Rajasekaran May 03 '16 at 05:03
  • yes and it is printing whole line like this ::::HLS| 04/14/16 17:56:58:933 |hls_tsd_module.cpp|ProcessVideoBuffer |4151|DEBUG: SAMPLE TYPE: B - FRAME PTS: 8573002542 DTS: 8573002542 i want oly after the keyword sample is there any method to del the words before tht string and print the string after the keyword sample – RaviTeja May 03 '16 at 05:25
  • yes what u said is tht i want and i want to check all the files in folder once and if 1st file contain strng it has to print 1.new and 2nd has tht strng it has to print to 2.new file if folder has 10 files it has to check all the 10 files at once for tht string and print to 10 diff new files – RaviTeja May 03 '16 at 05:39
  • now i am selecting specific file and searching for tht string and printing to new file by giving >newfile – RaviTeja May 03 '16 at 05:43
  • `grep -rnw {FOLDER_NAME} -e {STRING} > {OP_FILE}` eg.: `grep -rnw ABC -e Ravi > newfile` – Niranj Rajasekaran May 03 '16 at 06:16
  • import re target = open("debug.txt", "r") for line in target: search=re.search("[SAMPLE TYPE]",line) v1=search if v1 is not None: print line in this file i didnt understood – RaviTeja May 03 '16 at 06:32
  • I have edited the answer please have a look and try to implement it – Niranj Rajasekaran May 03 '16 at 06:43
  • which os are you running? – Niranj Rajasekaran May 03 '16 at 08:18
1

Python regex search method returns match object or None (if nothing is found). To get string from match object you need to call .group().

In [1]: import re

In [2]: re.search("a", "aaa")
Out[2]: <_sre.SRE_Match at 0x7fe3514e98b8>

In [3]: re.search("a", "aaa").group()
Out[3]: 'a'

Check out the docs and this or this SO questions.

Community
  • 1
  • 1
Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
1

This is how it can be done:

with open("debug.txt", "r") as target:
    for line in target:
        if "[SAMPLE TYPE]" in line:
            print(line)

Note that I used the context manager (the with statement) for opening the file. Besides, no re is needed if you simply want to find a substring in another string.

elzell
  • 2,228
  • 1
  • 16
  • 26
0

Try:

if search:
    print(line)

The regular expression object that is being returned will evaluate to False when it can't find anything.

Oliver W.
  • 13,169
  • 3
  • 37
  • 50
0

From the documentation: re.search returns a MatchObject instance. The documentation shows you how to access the searched for string, in your case for example with v1.group(0).

tschale
  • 975
  • 1
  • 18
  • 34