Skip to content

Instantly share code, notes, and snippets.

@stephenfeather
Created June 28, 2023 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenfeather/dc62ce6572f10580043dbc05f18b8d5f to your computer and use it in GitHub Desktop.
Save stephenfeather/dc62ce6572f10580043dbc05f18b8d5f to your computer and use it in GitHub Desktop.
Bash script to clean up sourceMappingURL references in web files.
#!/bin/bash
# Script: grep_and_remove.sh
# Author: Stephen Feather
# Co-author: OpenAI (ChatGPT)
# Copyright (c) 2023
# All rights reserved.
# License: CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/)
# Define the directory where you want to search
directory=$(pwd)
# Define the file type you want to search (optional)
file_type=""
# If a file type is specified as an argument, assign it to the file_type variable
if [[ $# -gt 0 ]]; then
file_type=".$1"
fi
# Define the pattern to search for
pattern="\\/\\*# sourceMappingURL=[^*]+ \\*\\/\\n"
# Find all files of the specified type in the directory and its subdirectories
files=$(find "$directory" -type f -name "*$file_type")
# Iterate over the files and perform the search and replace
for file in $files; do
# Search for the pattern and confirm the replacement
if grep -q "$pattern" "$file"; then
echo "Processing file: $file"
grep "$pattern" "$file"
read -p "Do you want to remove the reference? (y/n): " choice
if [[ $choice == "y" ]]; then
# Replace the pattern with an empty string
sed -i "s/$pattern//g" "$file"
echo "Reference removed."
fi
echo "---------------------------"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment