>_ IzzyLab

Hands-on task

This task is step 18 of the course “find and xargs: search and bulk operations”

Split a list into groups of three

medium~5 min

The file /root/nums.txt contains twelve numbers, each on its own line. Regroup them so that the result has three numbers per line (space-separated), and write it to /root/grouped.txt. The task should be solved by xargs — by how many arguments it puts into a single command run.

What you'll practise:
  • xargs
  • command-line arguments
  • stream processing
  • grouping

Topic breakdown

xargs reads data from standard input and turns it into arguments for a command, by default packing as many as possible into a single run. The key idea of the task is to control how many arguments go into each call: that's what the per-run argument-count limiter is responsible for. If no explicit command is given, xargs uses echo, so the same values can be neatly laid out in fixed portions across lines.

What's checked

  1. three numbers per line
  2. exactly four lines

What it looks like