banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Text processing with xargs and jot

The xargs and jot commands are mainly useful for jobs other than manipulating text. You can feed a list of files to xargs to get another command to do something with each of the files, and you can call on jot to generate some random numbers.

Nevertheless, just as GNU datamash can do text processing as well as descriptive statistics, both xargs and jot have niche uses for the handling of plain text, as shown below.


xargs. For a demo, I'll work with the following list of words ("xlist"):

bare
care
dare
fare
hare
mare
pare
rare

To put the list on one line with space separation I could do paste -d " " -s < xlist,
but xargs < xlist is simpler:

xargs1

xargs is also easier to use than paste if I want to put only 3 items (for example) on each output line:

xargs2

From the xargs man page for the -L [max-lines] option: Use at most max-lines nonblank input lines per command [output] line.


jot has a number of text options, like printing a word N times. Here I'm printing "care" 7 times with the -b option, then changing the default separator from a newline to a comma with -s ",":

jot1

The -c option allows me to specify the decimal values of ASCII characters, for example 33 ("!") to 126 ("~"). If I combine that with the -r option for random selection and the -s "" option for no spacing, I can build an 8-character random ASCII password:

jot2

The jot syntax is a little demanding. In this case I first provide the options (-s, -r, -c), then the number of repeats (8), then the starting item (ASCII decimal value 33), then the end item (ASCII 126). Unfortunately jot doesn't know the characters past decimal 127, and returns � for those. On the other hand, the -b option will repeat words with multibyte characters.


xargs + jot. To combine things, I can get xargs to repeat a jot command N times by sending it the numbers from 1 to N, for example with seq:

seq 10 | xargs -I {} jot -s "" -r -c 8 33 126

jot3

For more on the -I "replace string" option see the xargs man page. What's happening here is that each of the numbers from 1 to 10 is being replaced by the jot command output. Note that -I and the max-lines option -L can't be used in the same xargs command.

But if all I wanted was a list of 10 random, 8-character passwords, an easier option would be to fire up pwgen:

pwgen

-s = "Generate completely random, hard-to-memorize passwords."
-y = "Include at least one special character in the password."
-1 = "Print the generated passwords one per line."
8 = password length
10 = number of passwords
 
I highly recommend pwgen for password-building with its default behaviour:
 
"The pwgen program generates passwords which are designed to be easily memorized by humans, while being as secure as possible. Human-memorable passwords are never going to be as secure as completely completely random passwords. In particular, passwords generated by pwgen without the -s option should not be used in places where the password could be attacked via an off-line brute-force attack. On the other hand, completely randomly generated passwords have a tendency to be written down, and are subject to being compromised in that fashion." [from the man page]


More from jot. Another use for this command is generating version codes with the -w option and printf formatting. Here are 2 different ways to build "boxNNNN" codes from 100 to 109 with a leading zero:

jot4

In the first command, 10 is the number of repeats, 100 is the starting number, 109 is the finishing number. In the second command the number of repeats is left out (with "-" as placeholder) and the final "1" is the interval between numbers. In the first command, the "1" isn't needed.

But there's an easier way, with BASH brace expansion:

bash

Next post:
2025-04-04   Rename time-series files for chronological sorting


Last update: 2025-03-28
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License