I have a shell script with this code:
var=`hg st -R \"$path\"`
if [ -n \"$var\" ]; then
echo $var
fi
But the conditional code always ex
Assignments ignore leading and trailing whitespace and as such can be used to trim:
$ var=`echo ' hello'`; echo $var
hello
I found that I needed to add some code from a messy sdiff
output in order to clean it up:
sdiff -s column1.txt column2.txt | grep -F '<' | cut -f1 -d"<" > c12diff.txt
sed -n 1'p' c12diff.txt | sed 's/ *$//g' | tr -d '\n' | tr -d '\t'
This removes the trailing spaces and other invisible characters.
var=' a b c '
trimmed=$(echo $var)
This will remove all the whitespaces from your String,
VAR2="${VAR2//[[:space:]]/}"
/
replaces the first occurrence and //
all occurrences of whitespaces in the string. I.e. all white spaces get replaced by – nothing
You can delete newlines with tr
:
var=`hg st -R "$path" | tr -d '\n'`
if [ -n $var ]; then
echo $var
done
Removing spaces to one space:
(text) | fmt -su