How to split one string into multiple strings separated by at least one space in bash shell?

前端 未结 8 1206
日久生厌
日久生厌 2020-11-27 09:17

I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?

The stri

相关标签:
8条回答
  • 2020-11-27 09:48

    (A) To split a sentence into its words (space separated) you can simply use the default IFS by using

    array=( $string )
    


    Example running the following snippet

    #!/bin/bash
    
    sentence="this is the \"sentence\"   'you' want to split"
    words=( $sentence )
    
    len="${#words[@]}"
    echo "words counted: $len"
    
    printf "%s\n" "${words[@]}" ## print array
    

    will output

    words counted: 8
    this
    is
    the
    "sentence"
    'you'
    want
    to
    split
    

    As you can see you can use single or double quotes too without any problem

    Notes:
    -- this is basically the same of mob's answer, but in this way you store the array for any further needing. If you only need a single loop, you can use his answer, which is one line shorter :)
    -- please refer to this question for alternate methods to split a string based on delimiter.


    (B) To check for a character in a string you can also use a regular expression match.
    Example to check for the presence of a space character you can use:

    regex='\s{1,}'
    if [[ "$sentence" =~ $regex ]]
        then
            echo "Space here!";
    fi
    
    0 讨论(0)
  • 2020-11-27 09:54
    echo $WORDS | xargs -n1 echo
    

    This outputs every word, you can process that list as you see fit afterwards.

    0 讨论(0)
  • 2020-11-27 09:55

    Just use the shells "set" built-in. For example,

    set $text
    

    After that, individual words in $text will be in $1, $2, $3, etc. For robustness, one usually does

    set -- junk $text
    shift
    

    to handle the case where $text is empty or start with a dash. For example:

    text="This is          a              test"
    set -- junk $text
    shift
    for word; do
      echo "[$word]"
    done
    

    This prints

    [This]
    [is]
    [a]
    [test]
    
    0 讨论(0)
  • 2020-11-27 09:57
    $ echo "This is   a sentence." | tr -s " " "\012"
    This
    is
    a
    sentence.
    

    For checking for spaces, use grep:

    $ echo "This is   a sentence." | grep " " > /dev/null
    $ echo $?
    0
    $ echo "Thisisasentence." | grep " " > /dev/null     
    $ echo $?
    1
    
    0 讨论(0)
  • 2020-11-27 09:59

    Did you try just passing the string variable to a for loop? Bash, for one, will split on whitespace automatically.

    sentence="This is   a sentence."
    for word in $sentence
    do
        echo $word
    done
    

     

    This
    is
    a
    sentence.
    
    0 讨论(0)
  • 2020-11-27 10:01

    Probably the easiest and most secure way in BASH 3 and above is:

    var="string    to  split"
    read -ra arr <<<"$var"
    

    (where arr is the array which takes the split parts of the string) or, if there might be newlines in the input and you want more than just the first line:

    var="string    to  split"
    read -ra arr -d '' <<<"$var"
    

    (please note the space in -d ''; it cannot be omitted), but this might give you an unexpected newline from <<<"$var" (as this implicitly adds an LF at the end).

    Example:

    touch NOPE
    var="* a  *"
    read -ra arr <<<"$var"
    for a in "${arr[@]}"; do echo "[$a]"; done
    

    Outputs the expected

    [*]
    [a]
    [*]
    

    as this solution (in contrast to all previous solutions here) is not prone to unexpected and often uncontrollable shell globbing.

    Also this gives you the full power of IFS as you probably want:

    Example:

    IFS=: read -ra arr < <(grep "^$USER:" /etc/passwd)
    for a in "${arr[@]}"; do echo "[$a]"; done
    

    Outputs something like:

    [tino]
    [x]
    [1000]
    [1000]
    [Valentin Hilbig]
    [/home/tino]
    [/bin/bash]
    

    As you can see, spaces can be preserved this way, too:

    IFS=: read -ra arr <<<' split  :   this    '
    for a in "${arr[@]}"; do echo "[$a]"; done
    

    outputs

    [ split  ]
    [   this    ]
    

    Please note that the handling of IFS in BASH is a subject on its own, so do your tests; some interesting topics on this:

    • unset IFS: Ignores runs of SPC, TAB, NL and on line starts and ends
    • IFS='': No field separation, just reads everything
    • IFS=' ': Runs of SPC (and SPC only)

    Some last examples:

    var=$'\n\nthis is\n\n\na test\n\n'
    IFS=$'\n' read -ra arr -d '' <<<"$var"
    i=0; for a in "${arr[@]}"; do let i++; echo "$i [$a]"; done
    

    outputs

    1 [this is]
    2 [a test]
    

    while

    unset IFS
    var=$'\n\nthis is\n\n\na test\n\n'
    read -ra arr -d '' <<<"$var"
    i=0; for a in "${arr[@]}"; do let i++; echo "$i [$a]"; done
    

    outputs

    1 [this]
    2 [is]
    3 [a]
    4 [test]
    

    BTW:

    • If you are not used to $'ANSI-ESCAPED-STRING' get used to it; it's a timesaver.

    • If you do not include -r (like in read -a arr <<<"$var") then read does backslash escapes. This is left as exercise for the reader.


    For the second question:

    To test for something in a string I usually stick to case, as this can check for multiple cases at once (note: case only executes the first match, if you need fallthrough use multiple case statements), and this need is quite often the case (pun intended):

    case "$var" in
    '')                empty_var;;                # variable is empty
    *' '*)             have_space "$var";;        # have SPC
    *[[:space:]]*)     have_whitespace "$var";;   # have whitespaces like TAB
    *[^-+.,A-Za-z0-9]*) have_nonalnum "$var";;    # non-alphanum-chars found
    *[-+.,]*)          have_punctuation "$var";;  # some punctuation chars found
    *)                 default_case "$var";;      # if all above does not match
    esac
    

    So you can set the return value to check for SPC like this:

    case "$var" in (*' '*) true;; (*) false;; esac
    

    Why case? Because it usually is a bit more readable than regex sequences, and thanks to Shell metacharacters it handles 99% of all needs very well.

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