sed command to replace code example

Example 1: linux replace string in all files

sed -i 's/old-text/new-text/g' input.txt

Example 2: sed replace in file

sed -i 's/foo/bar/g' hello.txt

Example 3: sed replace with variable

Just concatenate var '"$var"' to replace values string in sed command:
$ sed -i 's/string_to_replace/'"$var"'/g' file_to_replace.txt

Example 4: sed replace

For using match in sed replacement, just border it with '\(' and '\)':
echo Before123 | sed 's/Before\([0-9]*\)/\1After/g'
123After	# number is matched withtin '\( \)' and replaced in '\1'
Example with 2 match replacements
echo a_b | sed 's/\(^.*\)_\(.*$\)/first is \1 and \2 is after/g'

Example 5: bash search and replace text in file

# Basic syntax using awk:
awk '{gsub(regex, substitution_text, $field#); print $0;}' input_file
# Where:
#	- gsub is a function that replaces every regular expression (regex)
#		match with substitution_text. 
#	- $field# is optional but can be used to specify a particular field
#		where gsub should operate. (This is useful if you want to 
#		restrict the substitutions to a specific column)

# Example usage:
awk '{gsub(" ","",$0); print $0;}' input_file
# This replaces every space " " with nothing "", thereby eliminating all
# whitespace from the file