nushell / nushell

A new type of shell
https://www.nushell.sh/
MIT License
32.48k stars 1.68k forks source link

Add 'like' operator (clone of =~) #13261

Closed freijon closed 3 weeks ago

freijon commented 4 months ago

Related problem

The ~ character is awkward to write on certain keyboards (I need to hit three keys to insert it). Also, I often confuse the order and write ~= instead of =~, but maybe that's just my brain that's wired wrong.

Describe the solution you'd like

In SQL languages the like operator is used. I propose that like could be used on top of =~. The operators should work exactly the same, it's just another way to write it. An operator-alias if you like. The syntax would be very SQL-like.

Example: ls | where name like .bash could be used instead of ls | where name =~ .bash

Describe alternatives you've considered

No response

Additional context and details

No response

fdncred commented 4 months ago

If we added like for a synonym to =~ we'd probably need to add not-like for a synonym to !~ too. I wouldn't be against these changes.

hustcer commented 4 months ago

I don't think it's good idea to add like: We can say 'm1cNnTsf2blMGiThe2beJnerh4xiA6mRy1NVjAY0VtULuOEX5BJWBiY6v0wi' =~ (or contains) '2', But can we say, as a matter of common sense, that m1cNnTsf2blMGiThe2beJnerh4xiA6mRy1NVjAY0VtULuOEX5BJWBiY6v0wi like 2, or vice versa?

BTW: We already have str contains command, you can also write ls | where name =~ .bash as ls | where ($it.name | str contains '.bash')

freijon commented 4 months ago

My example was probably not the best, I use =~ with regex, which is not possible with str contains. The operator like would just say "use regex", like on SQL. It would be logical because nushell uses several commands borrowed from SQL, like where, select.

hustcer commented 4 months ago

find works with regex:

ls | find -r .bash -c [name]
Or1g3n commented 1 month ago

I agree it would be very nice to have a 'like' operator. Especially since nushell's ethos is about structured data. Leveraging existing syntax from tools like SQL/Pandas etc. when working with similar structures only makes it much easier for developers to pick up and go with minimal learning curve.

=~ or like !~ or not like