Analyze Historical Commands Using Stream Redirection
Requirements
You need to analyze a file containing a record of command usage. Your task is to identify the top three most frequently used commands in the file and save the results in /home/labex/project/result
. You must use stream redirection techniques to accomplish this task.
Tasks
- Use the
sort
, uniq
, and head
commands in combination with stream redirection to process the data1
file located in /home/labex/project/
.
- Redirect the output to
/home/labex/project/result
.
- Ensure that results include both the number of occurrences and the command, formatted as "count command" (e.g., "100 ls").
Example
A sample of the data1
file content:
895 echo $?
896 openstack compute service list
897 cd /home/chy/openstack/
898 . admin-openrc
...
The expected output format for the top commands in /home/labex/project/result
:
150 openstack
114 systemctl
105 ls
Tips
You may need to use awk
to extract the command from the file content. awk
usage:
awk '{print $2}'
: Prints the second field of each line.
awk '{print $1, $2}'
: Prints the first and second fields of each line.