Skip to main content
1 of 2
Larry
  • 321
  • 1
  • 7

A solution that calls several programs from a shell:

fmt -1 words.txt | sort -u | xargs -Ipattern sh -c 'echo "pattern:$(grep -cw pattern words.txt)"'

Here, the string "pattern", given through the -I option, is a placeholder for xargs that it substitutes for each single line in its standard input.

The indirection with sh is needed because xargs only knows how to execute a single program, given it's name, passing everything else as arguments to it. xargs does not handle things like command substitution or piping.

Larry
  • 321
  • 1
  • 7