How to remove extra spaces in bash?

后端 未结 10 1709
说谎
说谎 2020-12-01 05:46

How to remove extra spaces in variable HEAD?

HEAD=\"    how to  remove    extra        spaces                     \"

Result:

相关标签:
10条回答
  • 2020-12-01 05:53

    Take advantage of the word-splitting effects of not quoting your variable

    $ HEAD="    how to  remove    extra        spaces                     "
    $ set -- $HEAD
    $ HEAD=$*
    $ echo ">>>$HEAD<<<"
    >>>how to remove extra spaces<<<
    

    If you don't want to use the positional paramaters, use an array

    ary=($HEAD)
    HEAD=${ary[@]}
    echo "$HEAD"
    

    One dangerous side-effect of not quoting is that filename expansion will be in play. So turn it off first, and re-enable it after:

    $ set -f
    $ set -- $HEAD
    $ set +f
    
    0 讨论(0)
  • 2020-12-01 05:55

    echo -e " abc \t def "|column -t|tr -s " "

    column -t will:

    1. remove the spaces at the beginning and at the end of the line
    2. convert tabs to spaces

    tr -s " " will squeeze multiple spaces to single space


    BTW, to see the whole output you can use cat - -A: shows you all spacial characters including tabs and EOL:

    echo -e " abc \t def "|cat - -A

    output: abc ^I def $

    echo -e " abc \t def "|column -t|tr -s " "|cat - -A

    output: abc def$

    0 讨论(0)
  • 2020-12-01 05:55

    echo variable without quotes does what you want:

    HEAD="    how to  remove    extra        spaces      "
    echo $HEAD
    
    # or assign to new variable
    NEW_HEAD=$(echo $HEAD)
    echo $NEW_HEAD
    

    output: how to remove extra spaces

    0 讨论(0)
  • 2020-12-01 05:57

    Whitespace can take the form of both spaces and tabs. Although they are non-printing characters and unseen to us, sed and other tools see them as different forms of whitespace and only operate on what you ask for. ie, if you tell sed to delete x number of spaces, it will do this, but the expression will not match tabs. The inverse is true- supply a tab to sed and it will not match spaces, even if the number of them is equal to those in a tab.

    A more extensible solution that will work for removing either/both additional space in the form of spaces and tabs (I've tested mixing both in your specimen variable) is:

    echo $HEAD | sed 's/^[[:blank:]]*//g'
    

    or we can tighten-up @Frontear 's excellent suggestion of using xargs without the tr:

    echo $HEAD | xargs
    

    However, note that xargs would also remove newlines. So if you were to cat a file and pipe it to xargs, all the extra space- including newlines- are removed and everything put on the same line ;-).

    Both of the foregoing achieved your desired result in my testing.

    0 讨论(0)
  • 2020-12-01 05:59

    Try this:

    echo "$HEAD" | tr -s " "
    

    or maybe you want to save it in a variable:

    NEWHEAD=$(echo "$HEAD" | tr -s " ")
    

    Update

    To remove leading and trailing whitespaces, do this:

    NEWHEAD=$(echo "$HEAD" | tr -s " ")
    NEWHEAD=${NEWHEAD%% }
    NEWHEAD=${NEWHEAD## }
    
    0 讨论(0)
  • 2020-12-01 06:03

    This horse isn't quite dead yet: Let's keep beating it!*

    Read into array

    Other people have mentioned read, but since using unquoted expansion may cause undesirable expansions all answers using it can be regarded as more or less the same. You could do

    set -f
    read HEAD <<< $HEAD
    set +f
    

    or you could do

    read -rd'' -a HEAD <<< "$HEAD"
    HEAD="${HEAD[*]}"
    

    Extended Globbing with Parameter Expansion

    $ shopt -s extglob
    $ HEAD="${HEAD//+( )/ }" HEAD="${HEAD# }" HEAD="${HEAD% }"
    $ printf '"%s"\n' "$HEAD"
    "how to remove extra spaces"
    

    *No horses were actually harmed – this was merely a metaphor for getting six+ diverse answers to a simple question.

    0 讨论(0)
提交回复
热议问题