Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Last active December 12, 2018 02:24
Show Gist options
  • Save mttjohnson/0dee48266e2fee7cad4efe3019d49f08 to your computer and use it in GitHub Desktop.
Save mttjohnson/0dee48266e2fee7cad4efe3019d49f08 to your computer and use it in GitHub Desktop.
Perl Bash/Shell RegEx Examples
# example perl one line replacing contents in a file in place
perl -pi -e 's/something_to_match/replaced_with/' ./my_path/to_file.txt
# Additional regex modifiers got global multi-line and case insensitive
perl -pi -e 's/something_to_match/replaced_with/gmi' ./my_path/to_file.txt
# Using different delimiters for the regex, in this case using a # instead of the tranditional /
perl -pi -e 's#something_to_match#replaced_with#' ./my_path/to_file.txt
# If you don't want to modify a file in place you can split things up a bit
# and then send the output to a new file, while also seeing the contents on the screen
cat ./my_path/to_file.txt | perl -p -e 's/something_to_match/replaced_with/' | sudo tee -a ./my_path/to_file_NEW_FILE.txt
# replace cat with pv to see a progress bar, and just redirect the STDOUT output of perl directly to a file
pv ./my_path/to_file.txt | perl -p -e 's/something_to_match/replaced_with/' > ./my_path/to_file_NEW_FILE.txt
# to preview the changes you can use head or tail to get some beginning lines or ending lines in a file
cat ./my_path/to_file.txt | perl -p -e 's/something_to_match/replaced_with/' | head -100
cat ./my_path/to_file.txt | perl -p -e 's/something_to_match/replaced_with/' | tail -100
# Use variables with the regex operation
SOMETHING_TO_MATCH="something_to_match"
REPLACE_WITH="replaced_with"
ORIGINAL_FILE="./my_path/to_file.txt"
NEW_FILE_AFTER_REPLACEMENTS="./my_path/to_file_NEW_FILE.txt"
cat ${ORIGINAL_FILE} | perl -p -e "s/${SOMETHING_TO_MATCH}/${REPLACE_WITH}/" > ${NEW_FILE_AFTER_REPLACEMENTS}
# You can also do some experimentation online with https://regexr.com/
# Then once you have a working example of a regex you want to apply,
# set the results in the variables and test it against a file.
# Experiment with replacement of content ad-hoc
MY_CONTENTS=$(cat <<CONTENTS_HEREDOC
something_not_matched
something_to_match
something else
whatever
CONTENTS_HEREDOC
)
SOMETHING_TO_MATCH="something_to_match"
REPLACE_WITH="replaced_with"
echo "${MY_CONTENTS}" | perl -p -e "s/${SOMETHING_TO_MATCH}/${REPLACE_WITH}/"
# Trim whitespace from beginning and ending of value
MY_VAR=" Something that needs trimming "
MY_VAR=$(echo "${MY_VAR}" | perl -p -e 's/^\s*(.*?)\s*$/$1/')
echo "\"${MY_VAR}\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment