Cancel down 2 integers

Wonderful evening to all of you!

My problem has to possible starting points.
Well, not really, but getting to either one is no problem at all.
So i got either a string in the format of "1920x1080" or simply the integers X = 1920 and Y = 1080.

When I am done, I would like to have an output as the following "16x09" or at least X = 16 and Y = 9, which of course could be simply concatenated.

As I am fairly new to shell-scripting, I got no idea how to even begin, and neither google nor the search were of help to me..

echo "1920x1080" | awk -F"x" ' { print "X = " $1 " and Y = " $2 } '

Is this what you want?

Nope not exactly, but never the less thank you for trying :slight_smile:

Hm english is not my native language and I don't know wheter i used the right word for the meaning I intended to express..

What i meant by "to cancel down" is for example: 6/4 => 3/2
Maybe reducing would be a more fitting expression?

Try Perl.

$ perl -MMath::BigInt=bgcd -le '$x=bgcd(1920,1080); print 1920/$x." ".1080/$x'
16 9
1 Like

Thank you balajesuri!

I like you short perl version...

Meanwhile I tried to write something on my own and finally managed to do so..

It's not pretty and it's not optimized, but it works..
Would be nice, if s.o. could tell me, if I made any mistakes,
or if s.th. could/should be made differently..

Here is my bash-version using: comm, cut, factor, let, sed and tr

#!/bin/bash
## reduce2ints.sh

#
# returns the reduced values of 2 given integers.
#

# error-handling
if [[ ( "$#" -lt 2 ) ]]; then
  echo -e "Error!!!\x0ATry \"$0 value1 value2\"";
  exit 1;
else

# put parameters in own variables
  X=$1;  
  Y=$2;
  
# split values into their prime-factors and put them in arrays
  declare -a XPrimes=$(factor $X | cut -d$'\x20' -f2-);
  declare -a YPrimes=$(factor $Y | cut -d$'\x20' -f2-);
  
# subtract arrays from eachother to get the differences
  declare -a RestX=("$(comm -23 <(echo ${XPrimes} | sed 's/ /\n/g') <(echo ${YPrimes} | sed 's/ /\n/g') | tr "\n" " ")");
  declare -a RestY=("$(comm -13 <(echo ${XPrimes} | sed 's/ /\n/g') <(echo ${YPrimes} | sed 's/ /\n/g') | tr "\n" " ")");
  
# prepare outvariables
  NewX=1;
  NewY=1;

# calculate the 'rests'
  for element in ${RestX[@]}; do
    let NewX=$NewX*$element;
  done
  
  for element in ${RestY[@]}; do
    let NewY=$NewY*$element;
  done
  
# output the solutions
  echo "1. Reduced Value=$NewX";
  echo "2. Reduced Value=$NewY";
fi

# EOF

Good night and thanks again!
jakunar

A humble bash code.

#! /bin/bash
y=$1; r=$2
while [ $r -ne 0 ]
do
gcd=$r
r=$(($y % $r))
y=$gcd
done
echo $(($1 / $gcd))
echo $(($2 / $gcd))

$ ./test.sh 1024 768
4
3