0
len=${#newarray[*]}

for ((i=0;i<${len};i++)); do

    if [[ "${newarray[$i]}" =~ ^[[:digit:]]+$ ]]; then 

        echo "${newarray[$i]}"     
    fi
  

this is my code I need to get only digits from the array list and their count as well. but there is an issue that shows alphabetical values only.

given getdata.txtcontains -

   cat  
   dog
   1234
   pl345
   567ab
   12234
     
18
  • Your if/else is backwards. You should do the echo in the if and continue in the else, since if matches numeric values. Commented Oct 1, 2020 at 6:02
  • Why are you echoing newfolders instead of value? Commented Oct 1, 2020 at 6:04
  • You can simply use len in for ((i=0;i<len;i++)); do (variables are automatically expanded within (( ... ))) Commented Oct 1, 2020 at 6:11
  • yes i changed the if-else but it captures pl345 as well @Barmar Commented Oct 1, 2020 at 6:21
  • thank you i did it that way my problem with the if else stement @DavidC.Rankin Commented Oct 1, 2020 at 6:22

6 Answers 6

3

Having:

value=(cat dog 1234 pl345 567ab 12234)

You could print "only numbers":

$ printf "%s\n" "${value[@]}" | grep -o "[0-9]*"
1234
345
567
12234

or maybe you want "only lines that contain only numbers":

$ printf "%s\n" "${value[@]}" | grep -x "[0-9]*"
1234
12234

or maybe you want "only lines with a number":

$ printf "%s\n" "${value[@]}" | grep "[0-9]"
1234
pl345
567ab
12234
Sign up to request clarification or add additional context in comments.

Comments

1

Your if/else is backwards.

value=(cat dog 1234 pl345 567ab 12234)
len=${#value[*]}
    
for ((i=0;i<${len};i++)); do
    if [[ "${value[$i]}" =~ ^[[:digit:]]+$ ]]; then 
         echo "${value[$i]}"   
    fi
done

This outputs:

1234
12234

Comments

1

There is a non-regex way of doing it in bash using shopt:

# enable extended glob
shopt -s extglob

value=(cat dog 1234 pl345 567ab 12234)

# lop through array and print only it contains 1+ digits
for v in "${value[@]}"; do [[ $v == +([0-9]) ]] && echo "$v"; done
1234
12234

Comments

0

You probably should use two nested loops:

  1. iterate over all elements of an array
  2. iterate over all chars in the element, check if the char is a digit

(2) would probably be simpler with grep, but since we're speaking of bash implementaton I've came up with this:

#!/bin/bash

a=(cat dog 1234 pl345 567ab 12234) # input array

for s in "${a[@]}"; do # iterate over all elements of our array
        res=''
        for ((i=0;i<${#s};i++)); do # iterate over all chars of element $s
                c="${s:$i:1}" # $c is now char at $i position inside $s
                case "$c" in
                        [0-9]) res="${res}${c}" # append digit to res
                esac
        done
        [ -n "$res" ] && echo "$res" # if $s contains at least 1 digit, print it
                                     # you may add $res to another array here if needed

done

Comments

0

Simply

tr -dc '0-9\n' <getdata.txt

Comments

0

You can use:

$ cat getdata.txt |grep -P '^[0-9]+$'

Comments

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.