Saturday, June 28, 2014

Squeezing white space in Linux Bash

http://www.linuxjournal.com/article/2563
For instance, we can squeeze all standard white space to a single space by specifying:
tr -s   \n [ *]
This will remove all spaces ...
echo " test test test " | tr -d ' '
so this results in
testtesttest
This will remove leading spaces...
echo " test test test " | sed 's/^ *//'
which results in
test test test
This will remove both trailing and leading spaces
echo " test test test " | sed -e 's/^ *//' -e 's/ *$//'
which results in
test test test
 
echo "   lol  " | xargs
Xargs will do the trimming for you, it's one command/program, no parameters, returns the trimmed string, easy as that!
WARNING:
echo " test    test " | xargs
gives
test test
not
 test    test
  
  
 var1=$(echo $STR | cut -f1 -d-)
var2=$(echo $STR | cut -f2 -d-)

No comments: