Remove spaces in file through shell script
Solution 1:
You may use this sed
:
sed -E 's/^[[:blank:]]+|[[:blank:]]*(\|)[[:blank:]]*|[[:blank:]]+$/\1/g' file
Apple|Banana|I want to eat banana|Carrot
Explanation:
-
^[[:blank:]]+
: Match 1 or more whitespaces after start -
|
: OR -
[[:blank:]]*(\|)[[:blank:]]*
: Match 0 or more whitespaces before and after a|
and capture|
in group #1 -
|
: OR -
[[:blank:]]+$
: Match 1 or more whitespaces before end -
Replacement is a
\1` that puts captured value of group #1 in replacement
An awk
solution:
awk -F '[[:blank:]]*\\|[[:blank:]]*' -v OFS='|' '{
gsub(/^[[:blank:]]+|[[:blank:]]+$/, ""); $1=$1} 1' file
Apple|Banana|I want to eat banana|Carrot