Removing whitespace in bash shell

Solved
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.

6 answers

  1. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    Hi,

    See this post!
    0
  2. jean
     
    ```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}' ```
    0
  3. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    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
    0
  4. jean
     
    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.
    0
  5. jipicy Posted messages 40842 Registration date   Status Moderator Last intervention   4 898
     
    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...
    0
  6. jean
     
    Very well. Thank you for the information, I'll be able to manage from now on :)
    0