bash remove whitespaces from string code example
Example 1: bash strip preceeding white space
# Basic syntax:
sed 's/^ *//g'
# Example usage:
echo ' text' # Printing without sed command
--> text
echo ' text' | sed 's/^ *//g' # Pipe to sed command
--> text
Example 2: bash how to remove all whitespace from a file
# Example usage:
awk '{gsub(" ","",$0); print $0;}' input_file
# This replaces every space " " with nothing "", thereby eliminating all
# whitespace from the file
# Basic syntax:
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)