简体   繁体   中英

Concatenating digits from a string in sh

Assuming that I have a string like this one:

string="1 0 . @ 1 1 ? 2 2 4"

Is it possible to concatenate digits that are next to each other?

So that string be like: 10 . @ 11 ? 224 10 . @ 11 ? 224 10 . @ 11 ? 224 ?

I found only basic things how to distinguish integers from other characters and how to "connect" them. But I have no idea how to iterate properly.

num=""
 for char in $string; do
    if [  $char -eq $char 2>/dev/null ] ; then
        num=$num$char

Here's an almost pure-shell implementation -- transforming the string into a character per line and using a BashFAQ #1 while read loop.

string="1 0 . @ 1 1 ? 2 2 4"
output=''

# replace spaces with newlines for easier handling
string=$(printf '%s\n' "$string" | tr ' ' '\n')

last_was_number=0
printf '%s\n' "$string" | {
  while read -r char; do
    if [ "$char" -eq "$char" ] 2>/dev/null; then # it's a number
      if [ "$last_was_number" -eq "1" ]; then
        output="$output$char"
        last_was_number=1
        continue
      fi
      last_was_number=1
    else
      last_was_number=0
    fi
    output="$output $char"
  done
  printf '%s\n' "$output"
}

To complement Charles Duffy's helpful, POSIX-compliant sh solution with a more concise perl alternative:

Note: perl is not part of POSIX, but it is preinstalled on most modern Unix-like platforms.

$ printf '%s\n' "1 0 . @ 1 1 ? 2 2 4" | perl -pe 's/\d( \d)+/$& =~ s| ||gr/eg' 
10 . @ 11 ? 224
  • The outer substitution, s/\\d( \\d)+/.../eg , globally ( g ) finds runs of at least 2 adjacent digits ( \\d( \\d)+ ), and replaces each run with the result of the expression ( e ) specified as the replacement string (represented as ... here).

  • The expression in the inner substitution, $& =~ s| ||gr $& =~ s| ||gr , whose result is used as the replacement string, removes all spaces from each run of adjacent digits:

    • $& represents what the outer regex matched - the run of adjacent digits.
    • =~ applies the s call on the RHS to the LHS, ie, $& (without this, the s call would implicitly apply to the entire input string, $_ ).
    • s| ||gr s| ||gr replaces all ( g ) instances of <space> from the value of the value of $& and returns ( r ) the result, effectively removing all spaces.
      • Note that | is used arbitrarily as the delimiter character for the s call, so as to avoid a clash with the customary / delimiter used by the outer s call.

POSIX compliant one-liner with sed:

string="1 0 . @ 1 1 ? 2 2 4"
printf '%s\n' "$string" | sed -e ':b' -e ' s/\([0-9]\) \([0-9]\)/\1\2/g; tb'

It just iteratively removes the any space between two digits until there aren't any more, resulting in:

10 . @ 11 ? 224

Here is my solution:

string="1 0 . @ 1 1 ? 2 2 4"
array=(${string/// })
arraylength=${#array[@]}
pattern="[0-9]"
i=0

while true; do
    str=""
    start=$i

    if [ $i -eq $arraylength ]; then
        break;
    fi

    for (( j=$start; j<${arraylength}; j++ )) do
       curr=${array[$j]}
       i=$((i + 1))

       if [[ $curr =~ $pattern ]]; then 
           str="$str$curr"
       else
           break
       fi
   done

   echo $str
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM