Python Forum
Search string in mutliple .gz files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search string in mutliple .gz files
#1
Hi,
Kindly your support to provide python script to search given strings (Number, Text etc.) from multiple ".gz" text files

Directory contains multiple".gz" files date wise
Output:
/bkup/TC/XYZ/20210818 File Names: A_7235818.csv.gz A_7235819.csv.gz . .
Output:
Content of sample file. 38486,22625,XYZ_06_0_20210817204446-3997 88279,77617,XYZ_06_0_20210817204846-3998
Getting error while running below Code.

import glob
import gzip

matched_lines = []

ZIPFILES='/bkup/TC/XYZ/20210818/*.gz'

grep = raw_input('Enter Search: ')

filelist = glob.glob(ZIPFILES)
for gzfile in filelist:
     #print("#Starting " + gzfile)  #if you want to know which file is being processed
    with gzip.open( gzfile, 'rb') as f:
#    grep = raw_input('Enter Search: ')
    for line in f: # read file line by line
        if grep in line: # search for string in each line
            matched_lines.append(line) # keep a list of matched lines

file_content = ''.join(matched_lines) # join the matched lines

print(file_content)
Output:
Error:
$ ./srch6.py File "./srch6.py", line 17 for line in f: # read file line by line ^ IndentationError: expected an indented block
Larz60+ write Aug-18-2021, 11:20 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
fixed for you this time, please use bbcode tags on future posts
Reply


Messages In This Thread
Search string in mutliple .gz files - by SARAOOF - Aug-18-2021, 11:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 946 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  splitting file into multiple files by searching for string AlphaInc 2 898 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Search multiple CSV files for a string or strings cubangt 7 8,043 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,132 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  fuzzywuzzy search string in text file marfer 9 4,579 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  how search files Ron_Crafter 4 48,539 Apr-17-2021, 11:19 AM
Last Post: Ron_Crafter
  I want to search a variable for a string D90 lostbit 3 2,620 Mar-31-2021, 07:14 PM
Last Post: lostbit
  Merging all file_name.log's files from directory to one and search “PerformanceINFO" sutra 0 1,788 Dec-09-2020, 05:14 PM
Last Post: sutra
  String search in different excel Kristenl2784 0 1,707 Jul-20-2020, 02:37 PM
Last Post: Kristenl2784
  Complex word search multiple files Kristenl2784 0 1,583 Jul-18-2020, 01:22 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020