The choose command in Linux

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

Hello, friends. In this post, you will learn how to use choose command in Linux. We have tested this tutorial on Debian 11, but it should work on Ubuntu 20.04 and derivatives.

What is the choose command?

The choose command is a tool created with Rust that is intended to be a more robust and easier to use alternative to the cut and awk commands.

Thanks to this command, we will be able to select words or word ranges from a string or an entire file. This makes it ideal for scripts and especially third-party programs that require work with character strings.

As it is created with Rust, we can install it without problems in Linux. In addition to this, it is a lightweight tool and its installation is straightforward to do.

Some features of the choose command are:

  • Terse field selection syntax similar to Python’s list slices
  • Negative indexing from end of line
  • Optional start/end index
  • Zero-indexed

So let’s go for it.

Install the Choose command

Being a tool created with Rust, we will be able to install using cargo. If you already have Rust installed on the system, then you already have cargo as well.

Otherwise, you can install it as a separate package from Rust. About Debian and Ubuntu, you can open a terminal and before installing it, update the system.

sudo apt update
sudo apt upgrade

Now you can install cargo by running

sudo apt install cargo

With cargo installed, we can install choose. To do so, just run

cargo install choose

At the end of the installation, you will be prompted to add the installation path to the PATH so you can use it in the terminal.

1.-Installing choose command in Linux
1.-Installing choose command in Linux

In my case, I have run this command

export PATH=$PATH:/home/angelo/.cargo/bin

Modify it to yours and now, check the version of Choose.

choose --version

Output:

choose 1.3.3

This way, we will be able to use it without problems.

Using the choose command

The choose command allows us to select one or several words from a text string or a file according to their position. In addition to this, it also works with ranges.

To explain it better, consider this text.

Hi, welcome to unixcop. Nice to meet you. So, have a nice day

If in this text, we want to show the first word and the sixth word.

echo 'Hi, welcome to unixcop. Nice to meet you. So, have a nice day' | choose 0 5

Output:

Hi, to

Remember that choose starts the count at 0. That is, 0 is the first word in the string.

To get a range, then use :. For example,

echo 'Hi, welcome to unixcop. Nice to meet you. So, have a nice day' | choose 1:6

Output:

welcome to unixcop. Nice to meet

In this case, I printed from the second word to the seventh.

Similar to Python, choose takes part of its syntax to define the last words of the string. So -1 refers to the last word, -2 to the second last word and so on.

For example:

echo 'Hi, welcome to unixcop. Nice to meet you. So, have a nice day' | choose 2:-1

In this case, we are displaying from the third word to the end. The result is:

to unixcop. Nice to meet you. So, have a nice day

You can also select different words and ranges

echo 'Hi, welcome to unixcop. Nice to meet you. So, have a nice day' | choose 0 10 4:-3 -1

So, I have selected, the first word, the eleventh, the fifth to the ante penultimate and the last word. The result is:

Hi, a Nice to meet you. So, have a day
2.- Using the choose command
2.- Using the choose command

The choose command is also useful to handle .csv file. In this case, the usage is the same but if it is delimited by , you have to add the -f option. For example,

cat [csv_file] | choose -f ',' 0:2

This way, you tell choose that the delimiter is ,.

If you want the screen output to be more readable, then you can use the -o option with the tab key.

cat [csv_file] | choose -f ',' -o '0 3

It is that simple.

Conclusion

In this post, we have introduced you to an important tool like choose. It is intended to be a solid alternative to the awk and cut commands but simplifying everything.

Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"
Angelo
Angelo
I am Angelo. A systems engineer passionate about Linux and all open-source software. Although here I'm just another member of the family.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook