0

So we have a test file that has a 3 digit number per line, several hundred lines of in the file.

I need to find a way to read from the file the number (or 3 digits that make up the number), add them together, and then determine if the resulting sum is odd or even. My current script is reading each line as a whole number, and I am missing the part where I am able to sum the digits...

while read number
do

echo $number
if [ $((number % 2)) -eq 0 ]; then 
  echo even
else
  echo odd
fi
done < input.txt
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Is [Bash: Split string into character array](https://stackoverflow.com/q/7578930/3266847) helpful? – Benjamin W. Oct 26 '22 at 18:38
  • 1
    please update the question with a sample set of data from `input.txt` along with the (wrong) output generated by your code as well as the (correct) expected output, making sure both sets of output correpond to the sample input – markp-fuso Oct 26 '22 at 18:47
  • 1
    This isn't something you would do in pure `bash`, except as part of a poorly designed homework assignment. At least use `awk`. – chepner Oct 26 '22 at 19:00

3 Answers3

0

Something like this maybe.

#!/usr/bin/env bash

while IFS= read -r numbers; do
   [[ $numbers =~ ^([[:digit:]]{1})([[:digit:]]{1})([[:digit:]]{1})$ ]] &&  
   arr=("${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}")
   sum=$(IFS=+; printf '%s' "${arr[*]}")
   if (( $((sum)) % 2 == 0)); then
     printf '%s is %d %% 2 = 0 is even\n' "$sum" "$((sum))"
   else
     printf '%s is %d %% 2 = 0 is odd\n' "$sum" "$((sum))"
   fi
done < file.txt

several hundred lines of in the file.

Bash is not the tool for this, It can be done with the shell but it will be very slow, and memory intensive, Use something like awk or perl or ...

Jetchisel
  • 7,493
  • 2
  • 19
  • 18
0

Simply replace

$((number % 2))

with

$(( (number/100 + number/10%10 + number%10) % 2))

in your code. Assuming number consists of three decimal digits, number/100 extracts the most significant digit, number/10%10 extracts the digit in the middle, and number%10 extracts the least significant digit. Note that shell arithmetic is integer only, and the operators / and % have equal precedence and are left-associative.

M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
0

Setup:

$ cat input.txt
123
456
789

Assuming the results need to be used in follow-on operations then one bash idea:

while read -r in
do
    sum=$(( ${in:0:1} + ${in:1:1} + ${in:2:1} ))
    result="odd"
    [[ $((sum%2)) -eq 0 ]] && result="even"
    echo "${in} : ${sum} : ${result}"
done < input.txt

This generates:

123 : 6 : even
456 : 15 : odd
789 : 24 : even

If the sole purpose is to generate the even/odd flag, and performance is an objective, you'll want to look at something other than bash, eg:

awk '
{ sum=0
  for (i=1;i<=length($1);i++)
      sum+=substr($1,i,1)
  result=(sum%2 == 0 ? "even" : "odd")
  printf "%s : %s : %s\n",$1,sum,result
}
' input.txt

This generates:

123 : 6 : even
456 : 15 : odd
789 : 24 : even
markp-fuso
  • 28,790
  • 4
  • 16
  • 36