Bash Script to Search Text in Multiple Files and Show Context

Bash script to search text in multiple files with context lines, enabling quick and recursive searches in specified directories using grep.

Brief Introduction

Bash Script to Search Text in Multiple Files and Show Context (bash)
DESCRIPTION:
This script allows you to search for a word or phrase within multiple files in a specified directory, showing the lines containing the searched text along with a defined number of context lines before and after the match.

ADDITIONAL NOTES:
The script is simple and robust, suitable for users who want to perform quick text searches in text files. It can be extended to include file type filters or more complex output.

EXPLANATIONS:
- The CONTEXT variable defines how many lines before and after the match to show.
- The grep command with the -r option performs a recursive search in the specified directory.
- The -n option shows the line number of the match.
- The -C option shows the context, i.e., lines before and after the match.
- Argument checking is handled to ensure the user provides both the word and directory.

Example Code: bash

#!/bin/bash

# Script to search text in multiple files and show context

# Check that two arguments are provided: word to search and directory
if [ "$#" -ne 2 ]; then
  echo "Usage: $0 <text_to_search> <directory>"
  exit 1
fi

TESTO="$1"
DIR="$2"

# Number of context lines to show before and after the match
CONTEXT=2

# Check that the directory exists
if [ ! -d "$DIR" ]; then
  echo "Error: directory '$DIR' does not exist."
  exit 1
fi

# Perform recursive search with grep showing context and line number
# -r: recursive
# -n: show line number
# -C $CONTEXT: show $CONTEXT lines before and after the match
# --color=auto: highlight the searched word

grep -r -n -C $CONTEXT --color=auto "$TESTO" "$DIR"

exit 0
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

Bash: shell script executed in Linux/Unix environment.

grep: command used for text searching with options to show context.

User input: word or phrase to search and directory where to perform the search.

Configuration parameters: number of context lines to show (default 2).

Dependencies/configurations: the script requires grep to be installed and available in the PATH.

No hardware or Arduino connections required.

Similar Topics

Script to batch rename files in a folder
Bash script to batch rename files in a folder by adding a custom prefix and sequential numbers, ideal for organizing files efficiently.
Text menu to launch frequent commands
Simple bash script providing a text menu to launch frequent commands, automating tasks without typing full commands each time.
Script to download files with curl and check the result
Bash script to download files using curl, verify success, and handle errors with clear messages for reliable command-line file transfers.
Automatic cleanup of temporary files in /tmp
Simple bash script to automatically delete temporary files older than 7 days in /tmp, freeing space and keeping your Linux system clean.
Batch Image Converter with ImageMagick from bash
Batch Image Converter bash script using ImageMagick to resize and convert images in bulk, supporting multiple formats and maintaining proportions.
Script to compare two folders and show file differences
Bash script to compare two folders, highlighting unique and modified files using standard Unix commands like diff, find, sort, and comm.
Simple contact book saved in CSV file managed by bash
Bash script for managing a simple contact book saved in a CSV file, allowing adding, searching, displaying, and deleting contacts via a text menu.
Multiple downloader reading a list of URLs from file
Simple bash script to sequentially download multiple files from URLs listed in a text file using wget, automating batch downloads efficiently.