Skip to main content
Added a more in-depth explanation of what the command does.
Source Link

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)"'

HereA little explanation:

The fmt -1 words.txt prints out all the words, 1 per line, and the string "pattern"| sort -u sorts this output and extracts only the unique words from it.

In order to count the occurences of a word in a file, given throughone can use grep (a tool meant to search files for patterns). By passing the -Icw option, is a placeholder forgrep gives the number of word matches it finds. So you can find the total number of occurrences of pattern using grep -cw pattern words.txt.

The tool xargs that it substitutesallows us to do this for each and every single line in itsword output by sort. The -Ipattern means that it will execute the following command multiple times, replacing each occurrence of pattern with a word it reads from standard input, which is what it gets from sort.

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. The $(...) is command substitution in the above snippet, as it substitutes the output from grep into echo, allowing it to be formatted correctly. Since we need the command substitution, we must use the sh -c command which runs whatever it recieves as an argument in its own shell.

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.

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)"'

A little explanation:

The fmt -1 words.txt prints out all the words, 1 per line, and the | sort -u sorts this output and extracts only the unique words from it.

In order to count the occurences of a word in a file, one can use grep (a tool meant to search files for patterns). By passing the -cw option, grep gives the number of word matches it finds. So you can find the total number of occurrences of pattern using grep -cw pattern words.txt.

The tool xargs allows us to do this for each and every single word output by sort. The -Ipattern means that it will execute the following command multiple times, replacing each occurrence of pattern with a word it reads from standard input, which is what it gets from sort.

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. The $(...) is command substitution in the above snippet, as it substitutes the output from grep into echo, allowing it to be formatted correctly. Since we need the command substitution, we must use the sh -c command which runs whatever it recieves as an argument in its own shell.

Source Link
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.