Challenge 3: Historical commands

Introduction

In Linux, the processing and analysis of text is extremely important. Now there is a file called data1, which can be downloaded using the following command:

$ wget https://labfile.oss.aliyuncs.com/courses/1/data1

The record in the data1 file is the operation record of some commands. Now you need to find the top 3 commands with the frequency of occurrence from it and save them in /home/skx/result .

aims

  1. Process the text file /home/skx/data1
  2. Write the result to /home/skx/result
  3. The result contains three lines of content, each line of content is the number of occurrences and the name of the command, such as "100 ls"

prompt

  1. cut interception (parameters can use -c 8- , use man cut to see the meaning)
  2. uniq -dc deduplication
  3. Sort parameter selection -k1 -n -r
  4. The operation process uses pipes, for example:
$ cd /home/skx
$ cat data1 |....|....|....   >  /home/skx/result

Reference answer

Note: Please be sure to consider the reference answer after you solve the problem independently. At the beginning, it is not very rewarding to look at the reference answer directly.

cat data1 |cut -c 8-|sort|uniq -dc|sort -rn -k1 |head -3 > /home/shiyanlou/result

# cut -c 8- 从第8个字符到结尾,用于去除行号
# sort 进行排序
# uniq -dc 输出重复过的行及重复次数,-d表示重复的只输出一行,-c表示输出次数
# sort -rn -k1,-r:反向排序,-k1 指定对第一个字段进行排序,-n:按数字排序
# head -3,取前三行

Guess you like

Origin blog.csdn.net/happyjacob/article/details/107007681