Removing whitespace in bash shell
Solved
jean
-
jean -
jean -
Hello,
I have a script with a variable that contains a list of first names and last names.
But I can't remove the leading spaces at the beginning of the line.
When I run sed 's/ //g', it removes the spaces between the words but not the 2 spaces at the start.
How can I do that?
With cat -A I see the code "M-BM- M-BM- " appearing where the troublesome spaces are
Thank you.
I have a script with a variable that contains a list of first names and last names.
But I can't remove the leading spaces at the beginning of the line.
When I run sed 's/ //g', it removes the spaces between the words but not the 2 spaces at the start.
How can I do that?
With cat -A I see the code "M-BM- M-BM- " appearing where the troublesome spaces are
Thank you.
6 answers
-
-
```plaintext I clearly see the similarity of the problem, but could you explain the following regexp to me (because I assume it's with this that I should solve my problem):
sed '/^\xC2\xA0$/{:z;N; /\n\xC2\xA0$/!b; s/^\xC2\xA0\n\xC2\xA0\n\xC2\xA0\n\xC2\xA0$/#/;T z}' ``` -
If all you need to do is remove the 2 spaces at the beginning of the line, the following syntax should suffice:
sed 's/^\xC2\xA0//' file
-
Indeed, with `sed 's/^\xC2\xA0\xC2\xA0//' file it worked.
What does xC2\xA0 correspond to? How do we know that it is this code that corresponds?
Thank you anyway. -
In fact, "<b>\xC2\xA0</b>" is the hexadecimal notation of the ASCII code (<b>\302\240</b>) corresponding to the non-printable character sequence "<b>M-BM- M-BM- </b>". <br /> <br />You just need to display your file with "<b>cat -A</b>" or "<b>sed -n l</b>" or even with a hexadecimal editor like "<b>hexedit</b>" or "<b>od</b>" to see this code. Then you just need to look for a table online for the correspondence...
-