2

I like remove spaces from the beginning and the end of line.

result="$(mysql -r --column-names=0 -e "SELECT String FROM Table")"
echo $result | cat -E
+stringA +StringB     $
echo $result | sed 's/^\([[:blank:]]\)\+//g' | sed 's/\([[:blank:]]\)\+$//g' | cat -E
+stringA +StringB     $

I copied the string with my mouse, and inserted it with the mouse key 3 (press down the wheel)

echo '+stringA +StringB     ' | sed 's/^\([[:blank:]]\)\+//g' | sed 's/\([[:blank:]]\)\+$//g' | cat -E
+stringA +StringB     $

now i pressed the key , navigate to the spaces and replace it with del and space

echo '+stringA +StringB     ' | sed 's/^\([[:blank:]]\)\+//g' | sed 's/\([[:blank:]]\)\+$//g' | cat -E
+stringA +StringB$

and now the sed command matched and deleted the spaces. How can i convert wrong spaces non-breaking space to normal spaces?

echo $LANG
en_US.UTF-8
$bash --version
GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
mysql -e "SHOW VARIABLES LIKE '%char%'"
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

Update1:

echo '+stringA +stringB         ' | od -tx1z
0000000 2b 73 74 72 69 6e 67 41 20 2b 73 74 72 69 6e 67  >+stringA +string<
0000020 42 c2 a0 c2 a0 c2 a0 c2 a0 c2 a0 c2 a0 c2 a0 c2  >B...............<
0000040 a0 c2 a0 0a                                      >....<
0000044
echo '+stringA +stringB         ' | od -tx1z
0000000 2b 73 74 72 69 6e 67 41 20 2b 73 74 72 69 6e 67  >+stringA +string<
0000020 42 20 20 20 20 20 20 20 20 20 0a                 >B         .<
0000033
echo '+stringA +stringB         ' | od -c
0000000   +   s   t   r   i   n   g   A       +   s   t   r   i   n   g
0000020   B 302 240 302 240 302 240 302 240 302 240 302 240 302 240 302
0000040 240 302 240  \n
0000044
echo '+stringA +stringB         ' | od -c
0000000   +   s   t   r   i   n   g   A       +   s   t   r   i   n   g
0000020   B                                      \n
0000033

the echo-commands 1 and 3 are copied with mouse key 3 the echo-commands 2 and 4 are with normal spaces

Update2:

i inserted the sedcommand with [^[:graph:]] but it does not change the out.

for UKWID in 123 456 678; do
    result="$(mysql -r --column-names=0 -e "SELECT String FROM Table WHERE id = $UKWID")"
    echo "$result" | sed 's/^[^[:graph:]]*//;s/[^[:graph:]]*//' | cat -E
done
+stringA +stringB         $
   +stringc +stringx    $
+stringe +stringf      $
  • what does mean your wrong spaces to normal spaces? – RomanPerekhrest Dec 13 '17 at 12:37
  • I mean the spaces in the $result var, they are different (i think the encodeing) to normal spaces, which i inserted with my keyboard. There are no visible differ. @RomanPerekhrest – FaxMax Dec 13 '17 at 12:41
  • non-breaking spaces maybe? Do you have to do it with sed or are there other options? – ilkkachu Dec 13 '17 at 12:44
  • of course, whitespaces are not visible at the end of the line. But the initial nuance is that your mysql query returns column values which have different number of spaces. – RomanPerekhrest Dec 13 '17 at 12:45
  • @RomanPerekhrest, yes the mysql select statement can return strings with a multible number of spaces at EOL – FaxMax Dec 13 '17 at 12:48
4

There are blanks (like U+00A0 the non-breaking space) or invisible characters (like backspace or U+200B) that are not classified as [:blank:] or even [:space:] in some locales.

In theory, you should be able to trim all non-graphical characters with:

sed 's/^[^[:graph:]]*//;s/[^[:graph:]]*$//'

But again on some locales in some systems that doesn't work for U+00A0.

perl -Mopen=locale -lpe 's/^\P{Graph}+|\P{Graph}+$//'

Works for your U+00A0, but doesn't for U+200B

You could also always adds those characters individually by yourself. Using zsh-like $'\u....' expansion (also supported by recent versions of bash ad some other shells):

trim=$'[[:space:][:cntrl:]\u00a0\u200b]*'
sed "s/^$trim//;s/$trim\$//"
  • I updated my question, it doesn't solve my problem. – FaxMax Dec 13 '17 at 13:20
  • 1
    @FaxMax, like I said it doesn't work in some locale that incorrectly classifies U+00A0 as a graphical character. That's the case of locales of GNU systems. Use perl instead. – Stéphane Chazelas Dec 13 '17 at 13:31
-1

Use below command to remove empty spaces at beginning and end of the line
Command Let me know for any other additional requirement


 sed -r "s/^\s+//g"| sed -r "s/\s+$//g"

  • Note that's GNU sed specific. On most GNU systems however, that doesn't work for the OP's U+00A0 character, as \s is just short for [[:space:]] and the non-breaking space is not considered a spacing character in GNU locales. – Stéphane Chazelas Dec 13 '17 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.