Skip to content

Instantly share code, notes, and snippets.

@maximivanov
Created January 14, 2021 13:29
Show Gist options
  • Save maximivanov/995505443f7b4372702813464678a3e0 to your computer and use it in GitHub Desktop.
Save maximivanov/995505443f7b4372702813464678a3e0 to your computer and use it in GitHub Desktop.
Replace multiple regular expressions with sed
#!/bin/bash
#
# Read file content, replace all occurences of multiple regex'es listed below, print result to the stdout
# Usage: ./sed-replace.sh FILENAME
# sed regex reference: http://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html#Regular-Expressions
#
# Example source.txt:
#
# lorem ipsum search 1 lorem
# lorem ipsum search 2 ending with number 5 lorem
#
# ./sed-replace.sh source.txt
# prints
#
# lorem ipsum REPLACED 1 lorem
# lorem ipsum REPLACED 2 lorem
regex1='s/search 1/REPLACED 1/g'
regex2='s/search 2 ending with number [[:digit:]]/REPLACED 2/g'
sed -z -E "$regex1;$regex2" $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment