How to use the command 'xe' (with examples)

How to use the command 'xe' (with examples)

The ‘xe’ command is a versatile utility that allows you to execute a command once for each line piped from another command or file. It replaces occurrences of the placeholder with the input line and provides additional functionalities like executing shell scripts, parallel execution, and more. This article will illustrate each of these use cases with examples.

Use case 1: Execute a command once for each line of input data as arguments

Code:

arguments_source | xe command

Motivation: This use case is helpful when you have a list of data and need to execute a command on each item individually. For example, you can use it to process a list of filenames and perform operations on each file.

Explanation:

  • arguments_source: This represents the source of input data, which can be a command or file that generates a list of lines.
  • xe: The ‘xe’ command is used to execute another command for each line.
  • command: This is the command that will be executed for each line from the arguments_source.

Example Output: Suppose we have a file named files.txt that contains a list of filenames:

file1.txt
file2.txt
file3.txt

If we want to calculate the word count for each file, we can use the following command:

cat files.txt | xe wc -w {}

The output will be:

10 file1.txt
24 file2.txt
16 file3.txt

Use case 2: Execute commands, replacing the placeholder with the input line

Code:

arguments_source | xe command {} optional_extra_arguments

Motivation: This use case is useful when you need to execute commands with additional arguments, where the input line needs to be substituted in a specific position within the command.

Explanation:

  • arguments_source: This represents the source of input data, which can be a command or file that generates a list of lines.
  • xe: The ‘xe’ command is used to execute another command for each line.
  • command: This is the main command that will be executed for each line from the arguments_source.
  • {}: This is a placeholder that represents the input line and will be replaced with each line from the arguments_source.
  • optional_extra_arguments: These are any additional arguments that the command requires.

Example Output: Suppose we have a file named numbers.txt that contains a list of numbers:

1
2
3

If we want to append each number with a string, we can use the following command:

cat numbers.txt | xe echo {} is a number

The output will be:

1 is a number
2 is a number
3 is a number

Use case 3: Execute a shell script, joining every N lines into a single call

Code:

echo -e 'a\nb' | xe -N2 -s 'echo $2 $1'

Motivation: This use case is helpful when you have a script that requires multiple lines of input for each execution. It allows you to process multiple lines together and perform complex operations.

Explanation:

  • echo -e 'a\nb': This generates two lines of input data: ‘a’ and ‘b’.
  • xe -N2 -s 'echo $2 $1':
    • -N2: This option specifies that the command should join every 2 lines together.
    • -s 'echo $2 $1': This is the shell script that will be executed. It uses $2 and $1 to refer to the second and first line of the joined lines, respectively.

Example Output: The output of the above command will be:

b a

Use case 4: Delete all files with a .backup extension

Code:

find . -name '*.backup' | xe rm -v

Motivation: This use case is useful when you want to delete specific files matching a certain pattern. It allows you to easily delete multiple files without manually specifying each file individually.

Explanation:

  • find . -name '*.backup': This command finds all files with a .backup extension in the current directory and its subdirectories.
  • xe rm -v: The ‘xe’ command executes the ‘rm’ command for each line passed from the previous command, which is the list of files matched by the ‘find’ command. The ‘-v’ option is used to enable verbose output, showing each file that is being deleted.

Example Output: Suppose we have the following files in the current directory:

file1.backup
file2.backup
file3.backup

Executing the above command will delete all files with a .backup extension and provide verbose output:

removed 'file1.backup'
removed 'file2.backup'
removed 'file3.backup'

Use case 5: Run up to max-jobs processes in parallel

Code:

arguments_source | xe -j max-jobs command

Motivation: This use case is beneficial when you have a command that can be parallelized to improve performance. By executing multiple processes in parallel, you can take advantage of multiple CPU cores and speed up the execution.

Explanation:

  • arguments_source: This represents the source of input data, which can be a command or file that generates a list of lines.
  • xe: The ‘xe’ command is used to execute another command for each line.
  • -j max-jobs: This option specifies the maximum number of parallel jobs to be run at the same time. The default value is 1. If ‘max-jobs’ is set to 0, ‘xe’ will run as many processes as CPU cores.
  • command: This is the command that will be executed for each line from the arguments_source.

Example Output: Suppose we have a file named commands.txt that contains a list of commands:

echo "Hello"
sleep 3
echo "World"

If we want to execute these commands in parallel, we can use the following command:

cat commands.txt | xe -j 2 sh -c {}

The output will be:

Hello
World

Conclusion:

The ‘xe’ command is a powerful utility that allows you to iterate over lines of input data and execute commands in various ways. Whether you need to process lines individually, join lines together, delete specific files, or run commands in parallel, ‘xe’ provides a flexible solution. By understanding the different use cases and examples illustrated in this article, you can leverage ‘xe’ to enhance your command-line workflow and improve productivity.

Related Posts

How to use the command 'browser-sync' (with examples)

How to use the command 'browser-sync' (with examples)

Browser-sync is a command-line tool that starts a local web server and automatically updates the browser whenever there are changes in the files.

Read More
How to use the command mate-dictionary (with examples)

How to use the command mate-dictionary (with examples)

This article will illustrate the use cases of the command mate-dictionary, which is used to look up words in dictionaries.

Read More
How to use the command ctkd (with examples)

How to use the command ctkd (with examples)

The ctkd command is a SmartCard daemon that should not be invoked manually.

Read More