1

How to remove extra spaces in a variable?

VAR="    Eget ac vel volutpat      dic  tumst   est dui      adipiscing  "

Correct result: VAR="Eget ac vel volutpat dic tumst est dui adipiscing" 

There are over 16,000,000 to be edited; sed is too slow!

!!! E D I T !!!

String OK! Thank you all

BUT

Text in array problem, gaps remain :-(

ITM=("  Eget ac  vel volutpat   |  Vel volutpat     dic tumst "
     "    Vestibulum  laoreet     a semper  |orttitor      eu laoreet justo congue ")

IFS=$'|'

for (( i=0 ; i<16000000 ; i++ ))
do
    AAA=( ITM[$i] )
    B=${AAA[0]}     # "  Eget ac  vel volutpat   "
    C=${AAA[1]}     # "  Vel volutpat     dic tumst "

done
7
  • 4
    16 million shell variables? You're doing it wrong! Commented Nov 7, 2012 at 21:48
  • 3
    I very much doubt that pushing all 16 million lines at once through sed will be that slow compared to anything you script in bash. Commented Nov 7, 2012 at 21:50
  • 1
    @bitmask: it depends on whether he's processing all 16 million lines through a single instance of sed (fast) or running sed 16 million times (once per array element) (very slow). It all depends on where those 16 million elements are coming from; if they're coming a file, I'd run it through sed first, then process that output a line at a time (and for God's sake and don't put it in an array unless you really need to). Commented Nov 7, 2012 at 23:35
  • @GordonDavisson: Yes, that was what I meant. Use one sed instance and have it process all lines, each holding one of the strings. Commented Nov 7, 2012 at 23:40
  • @bitmask: Now that I reread it, that was actually explicit in your original comment. But I suspect that's not what Charlie was doing... Commented Nov 7, 2012 at 23:46

3 Answers 3

2

You don't need to spawn subshells for this. Just use bash's built-in pattern substitution.

[ghoti@pc ~]$ shopt -s extglob
[ghoti@pc ~]$ VAR="    Eget ac vel volutpat      dic  tumst   est dui      adipiscing  "
[ghoti@pc ~]$ VAR=${VAR//+( )/ }
[ghoti@pc ~]$ echo "$VAR"
 Eget ac vel volutpat dic tumst est dui adipiscing 
[ghoti@pc ~]$ VAR=${VAR#+( )}
[ghoti@pc ~]$ echo "$VAR"
Eget ac vel volutpat dic tumst est dui adipiscing
[ghoti@pc ~]$ 
Sign up to request clarification or add additional context in comments.

3 Comments

That reduces the spaces but does not collapse them.
It replaces +( ) (any number of spaces) with a single space. What do you mean by "collapse"?
Am I just seeing things? I meant this for a post that suggested ${VAR// / }. That post was apparently deleted. Objection retracted.
0

Shell is not really set up to do massive data processing, even processing as simple as collapsing spaces. But here is a solution without using sed

VAR="    Eget ac vel volutpat      dic  tumst   est dui      adipiscing  "
VAR=$(echo $VAR)

This code will set VAR to the collapsed version of the original var. You can see this by executing:

echo "$VAR"

result:

Eget ac vel volutpat dic tumst est dui adipiscing

2 Comments

You edited your post to include that. Your minus 1 on my answer is unnecessary.
=) Don't be paranoid, it's not me. Moreover, "edited 22 mins ago" while you have answered 13 mins ago...
0

Just remove the quotes, ex :

$ VAR="    Eget ac vel volutpat      dic  tumst   est dui      adipiscing  "     
$ echo $VAR
Eget ac vel volutpat dic tumst est dui adipiscing
$ echo "$VAR"
    Eget ac vel volutpat      dic  tumst   est dui      adipiscing  
$ 

If you prefer :

$ VAR="    Eget ac vel volutpat      dic  tumst   est dui      adipiscing  "
$ VAR="$(echo $VAR)"
$ echo "$VAR"
Eget ac vel volutpat dic tumst est dui adipiscing

This behaviour is called word splitting.

NOTE

as mentioned in the comments, you should use this only if you know that your variables contains no special characters like *.

5 Comments

sputnick: thank, you but I do not want to write, I need to have it in a variable ...
@Lev Levitsky : x=$(</usr/share/dict/british); echo $x > /dev/null : no errors. Try it, you will see.
An alternative to command subsitition: arr=( $VAR ); VAR="${arr[*]}"
Removing the quotes is dangerous. There might be any other sort of funny character in the string that will be expanded.
And what happens when VAR="echo *" ? If the OP provides a dangerous example, it's better to show him a better way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.