Learn how to create a powerful shell script to retrieve JSON log files between specified dates and timestamps. Enhance your log searching capabilities today! --- This video is based on the question https://stackoverflow.com/q/63069234/ asked by the user 'Mani' ( https://stackoverflow.com/u/11988224/ ) and on the answer https://stackoverflow.com/a/63562221/ provided by the user 'Mani' ( https://stackoverflow.com/u/11988224/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Shell script to fetch log (json format) file between date and timestamp Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- How to Create a Shell Script for Quickly Fetching Log Files Between Dates and Timestamps When working with log files, especially in JSON format, it can be challenging to efficiently extract the information you need, particularly when it comes to date and time constraints. If you've ever found yourself needing to filter logs based on certain keywords and specific date ranges, you know how valuable a well-crafted shell script can be. In this guide, we'll explore how to create a shell script that can fetch log files between specified start and end dates, and timestamps, making your log management process significantly easier. Problem Overview In our scenario, log files are structured in a nested folder system where each file corresponds to a specific hour of a given day. The folders follow a specific format: [[See Video to Reveal this Text or Code Snippet]] For instance, logs for July 24, 2020, can be found in \Mainfolder\folder1\2020\07\24\, containing files such as 00:00:00_00:59:59.json, 01:00:00_01:59:59.json, and so forth. Our goal is to develop a script that not only filters these files based on a keyword but also allows for the selection of both start and end dates, as well as start and end times. Existing Scripts and Challenges You may already have a basic shell script to perform keyword filtering for a specified date. However, this script may lack the ability to handle date ranges and timestamps, which are essential for narrowing down your search. Below we will build upon this initial foundation to create a more robust solution. Solution Breakdown Let's dive into how to build this shell script step-by-step. The following script enables you to fetch logs between your specified conditions: [[See Video to Reveal this Text or Code Snippet]] Script Explanation Setting Up the Script: The shebang (# !/bin/bash) indicates that the script should run in the Bash shell. We disable the command tracing with set + x for cleaner output during execution. Initial Variable Definitions: DTE: Generates a unique timestamp for the output file. startdate, enddate: Define the date range for log retrieval. start_Time, end_Time: Define the time range of interest. keyword: The term you are searching for in the logs. BKT: Placeholder for representing your storage location. Handling Date Iteration: The script uses a while loop to iterate through each date from the start date to the end date. Inside the loop, the script checks if the current date is less than the end date; if not, it breaks out of the loop. Fetching and Filtering Log Data: The script utilizes gsutil cat to retrieve log files and pipe the result through sed and grep for filtering based on timestamps and the specified keyword. Results are appended to a temporary log output file. Final Copy of Result: Once all desired logs have been processed, it copies the aggregated results back to your Google Cloud Storage or specified directory. Conclusion By implementing the script outlined above, you will significantly enhance your ability to fetch and filter log files based on keywords, date ranges, and timestamps. This will streamline your log management process, making it more efficient and effective. Whether you manage system logs, application logs, or any other type of JSON log files, having a robust script to handle date and time filtering can save you a great deal of time and effort. Feel free to customize the script as per your needs, and happy logging!