How to compare contents of 2 variables?

chaoscreater

Active member
Let's say I have 2 variables:

Variable A:
apple
orange
pear
banana
kiwifruit

Variable B:
orange
apple
pear

I want to compare them and list only the differences that are found in Variable A but not Variable B. Does anyone have a shell script for this? I've tried a few shell scripts and just can't get it to output anything. And no, it's not a root/helper issue. I have other shell scripts that work fine, but I can't seem to get it working for this particular use case.

For example, this works perfectly fine in Termux, but nothing is output in MD and there's no errors. I definitely have the diff binary on Android btw.

#!/data/data/com.termux/files/usr/bin/bash

variable1="apple
banana
cherry
grape"

variable2="apple
banana
orange
grape"

# Use process substitution to compare the variables and filter the output
diff <(printf "%s" "$variable1") <(printf "%s" "$variable2") | grep '^<' | cut -c3-


If there's another way to do this without shell script, I'd be keen to know.
 
Last edited:

Dm114

Well-known member
Let's say I have 2 variables:

Variable A:
apple
orange
pear
banana
kiwifruit

Variable B:
orange
apple
pear

I want to compare them and list only the differences that are found in Variable A but not Variable B. Does anyone have a shell script for this? I've tried a few shell scripts and just can't get it to output anything. And no, it's not a root/helper issue. I have other shell scripts that work fine, but I can't seem to get it working for this particular use case. If there's another way to do this without shell script, I'd be keen to know.
Try Text manipulation action -> Remove text:
Source=var A
Text to match=var B (after replacing all \s by |)
 
Last edited:

chaoscreater

Active member
Try Text manipulation action -> Remove text:
Source=var A
Text to match=var B (after replacing all \s by |)
Thanks, I've just tried it but it doesn't quite work.

Firstly, it should be replacing \n by | because there could be words that are separated by spaces. It'd be better to do this on a new line instead.

Anyway, here's my data for variable A:
1.1.1.1 | 2Degrees | 70mai | 9Gag | ANZ | APK Pure | AT Mobile | AT Park | Action Block | Alexa | Aliexpress | Amazon | Android Messages | Android TV | App Manager | Aurora Store | Az Devops | Azure | BNZ | BPMe | Background Video Recorder | BardX | BetterBatteryStats | Bose Connect | Brave | Bridge | Bring! | Bromite | BubbleUPnP | CamCard | ChatGPT | CoPilot | Company Portal | Confluence | Cromite | Currency | CurrencyFair | DesktopSMS | Discord | DoorDash | Dosh | Droid-ify | Edge | Eufy | FTPClient | Facebook | Fake GPS 1 | Fake GPS 2 | Feedly | FolderSync | Foxy Droid | Free Proxy List | Freestuff | GSAM Battery Monitor | Gaspy | GetFlix | Gmail | Google | Google Calendar | Google Home | Google Keep | Google Lens | Google Maps | Google Pay | Google Photos | HTTP File Server | Headphones | Heartland | HelloGuestWiFi | Here Maps | Hoyts | Hungry Panda | Hushed | Ivacy | Kiwi Browser | Lean Remote | Lime | Line | Location Changer | Maps.Me | McDonalds | Menulog | Mi Fitness | Mi Gallery | Mi Remote | Microsoft Remote Desktop | Milkrun | Mullvad VPN | Neo Store | Netflix | New World | NextDNS | NextDNS Manager | Nine | OneDrive | Opera Mini | Opera Mobile | OpsGenie | Order Meal | Outlook Lite | Pak'nSAVE | Panda Remit | Parkable | Parkmate | Parkopedia | Perplexity | Petrol Spy | Pikashow | Play Store | Popcorn Time | PriceGrabber | Pricespy | ProtonVPN | Proxy List | Psiphon Pro | Pushbullet | Rabobank | RateX | Reddit | Rethink | Revolut | Rewards | SBS | Salefinder | Secure Parking | Shazam | Show Wifi login acreen | Signal | Slack | SlowDNS | SnakeTunnel VPN | Snapchat | SpotiFlyer | Spotify | Squirrel | Stocard | Stremio | Super Proxy | Swann | Swift Backup | Swipefy | Taobao | Tasks | Teams | Teddy | Telegram | Temu | Termux | TetherFi | The Warehouse | Tile | Today Weather | TopCashBack | Tournament Parking | Trademe | UP TV | Uber | Uber Eats | VPN China | VPN Hotspot | VPNHood! | Wallet | WangVPN | Waze | WeChat | Webtoon | Westfield | WhatsApp | WhosCall | WiFi Map | WiFiMan | Windscribe | Woolworths | Wyze | Xe Currency | Your Freedom | YourPhone | Youtube Vanced | Z App | Zepp Life | ZiVPN | Zoom | microG |

Here's my data for variable B:
1.1.1.1 | 2Degrees | 70mai | 9Gag | APK Pure | AT Mobile | AT Park | Action Block | Aliexpress | Amazon | Android Messages | Android TV | App Manager | Aurora Store | Az Devops | Azure | BNZ | BPMe | BardX | BetterBatteryStats | Bose Connect | Brave | Bridge | Bring! | Bromite | BubbleUPnP | CamCard | ChatGPT | CoPilot | Company Portal | Confluence | Cromite | Currency | CurrencyFair | DesktopSMS | Discord | DoorDash | Dosh | Droid-ify | Edge | Eufy | FTPClient | Facebook | Fake GPS 1 | Fake GPS 2 | Feedly | FolderSync | Foxy Droid | Free Proxy List | Freestuff | GSAM Battery Monitor | Gaspy | GetFlix | Gmail | Google | Google Calendar | Google Keep | Google Lens | Google Maps | Google Pay | Google Photos | HTTP File Server | Headphones | Heartland | HelloGuestWiFi | Here Maps | Hoyts | Hungry Panda | Hushed | Ivacy | Kiwi Browser | Lean Remote | Lime | Line | Location Changer | Maps.Me | McDonalds | Menulog | Mi Fitness | Mi Gallery | Mi Remote | Microsoft Remote Desktop | Milkrun | Mullvad VPN | Neo Store | Netflix | New World | NextDNS | NextDNS Manager | Nine | Opera Mini | Opera Mobile | Order Meal | Outlook Lite | Pak'nSAVE | Panda Remit | Parkable | Parkmate | Parkopedia | Perplexity | Petrol Spy | Pikashow | Play Store | Popcorn Time | PriceGrabber | Pricespy | ProtonVPN | Proxy List | Psiphon Pro | Pushbullet | Rabobank | RateX | Reddit | Rethink | Revolut | SBS | Salefinder | Secure Parking | Shazam | Show Wifi login acreen | Signal | Slack | SlowDNS | SnakeTunnel VPN | Snapchat | SpotiFlyer | Spotify | Squirrel | Stocard | Stremio | Super Proxy | Swann | Swift Backup | Swipefy | Taobao | Tasks | Teams | Teddy | Telegram | Temu | Termux | TetherFi | The Warehouse | Tile | Today Weather | TopCashBack | Tournament Parking | Trademe | UP TV | Uber | Uber Eats | VPN China | VPN Hotspot | VPNHood! | Wallet | WangVPN | Waze | WeChat | Webtoon | Westfield | WhatsApp | WhosCall | WiFi Map | WiFiMan | Windscribe | Woolworths | Wyze | Xe Currency | Your Freedom | YourPhone | Youtube Vanced | Z App | Zepp Life | ZiVPN | Zoom |

And here's the result:
|||| ANZ ||||| Alexa ||||||||||| Background Video Recorder ||||||||||||||||||||||||||||||||||||||Calendar |Home |Keep |Lens |Maps |Pay |Photos ||||||||||||||||||||||||||||Manager || OneDrive ||| OpsGenie |||||||||||||||||||||||| Rewards ||||||||||||||||||||||||||||||||||||Eats |||||||||||||||||||||||| Zoom | microG |

There's a bunch of leftover | but I'm not too worried about that now. The issue is that some of the words got removed even though it shouldn't have. For example, the word "Google" is found in both, so it removed it. However, it also removed that same word from Google Calendar, Google Home, Google Keep, Google Lends, etc...

I think it needs to do a strict matching of the exact word contained within | .



UPDATE:

I've also tried converting them into a dictionary and looping them, basically something like this:

foreach $variable_x in $variables1
{
foreach $variable_y in $variables2
{
compare $variable_x to $variable_y
}
}

but it was extremely slow and just isn't a good solution.
 
Last edited:

chaoscreater

Active member
Ended up appending a - between the words using regex, so that it makes each word unique. So instead of Google-Home, it'll become Google-Home and therefore when the word "Google" is matched, it'll just match itself and not any other words that contain Google.

Not ideal, but it'll do for my use case.
 

Dm114

Well-known member
Ended up appending a - between the words using regex, so that it makes each word unique. So instead of Google-Home, it'll become Google-Home and therefore when the word "Google" is matched, it'll just match itself and not any other words that contain Google.

Not ideal, but it'll do for my use case.
I based my suggestion on the very simple example you gave at first. You're right when replacing all spaces by a character to make all expressions a unique word (I would have rather choosen underscore [ _ ] or any other "rare" character).

I suggested this kind of method (Regex) because it's much faster than iterating an array/dictionary.
 

chaoscreater

Active member
I based my suggestion on the very simple example you gave at first. You're right when replacing all spaces by a character to make all expressions a unique word (I would have rather choosen underscore [ _ ] or any other "rare" character).

I suggested this kind of method (Regex) because it's much faster than iterating an array/dictionary.
Your suggestion did help me a lot. I ended up tweaking a few things and got it working. And yes it's definitely faster than iterating an array/dictionary. Thanks again.
 
Top