It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




已关闭8年。




如何使用STANDARD UNIX UTILITIES编写程序,该程序将一次从标准输入中读取一个字符的数据,并将结果输出到标准输出中。我知道在这种情况下它的运行类似于C程序。有人告诉我,这可以用一行代码完成,但是从来没有做过Unix Pipeline Programming,所以我很好奇。 该程序的目的是从标准输入中读取数据并计算一行中的单词数,然后在标准输出中计算出单词和行的总数

我想出了以下代码,但不确定:
tr A-Z a-z < file | tr -sc a-z | sort uniq -c wc '\n'
关于如何获得所需的任何想法或建议?

最佳答案

您可以将wc(字数统计)与-w选项一起使用,以对文件中的字数或-l中的行数进行计数。

$ cat file.txt
this file has 5 words.

$ wc -w file.txt             # Print number of words in file.txt
5 file.txt

$ wc -l file.txt             # Print number of lines in file.txt
1 file.txt

$ wc file.txt                # No option, print line, words, chars in file.txt
 1  5 23 file.txt
wc的其他选项:
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
  -L, --max-line-length  print the length of the longest line

09-12 04:09