nowcoder shell练习 1-10

地址:牛客网在线编程_编程学习|练习题_数据结构|系统设计题库

 shell1:

#!/bin/bash
# 使用wc命令统计行数并输出
wc -l nowcoder.txt | awk '{print $1}'

To understand the purpose of the command wc -l nowcoder.txt | awk '{print $1}', let's break it down:

wc -l nowcoder.txt counts the number of lines in the file nowcoder.txt.
| is a pipe that redirects the output of the previous command to the input of the next command.
awk '{print $1}' prints the first field (column) of the input. In this case, it prints the number of lines counted by wc.
Therefore, the entire command wc -l nowcoder.txt | awk '{print $1}' outputs the number of lines in the file nowcoder.txt.shell

shell2:

# 使用tail命令输出文件最后5行
tail -n 5 nowcoder.txt

To understand the purpose of the command tail -n 5 nowcoder.txt, let's break it down:

tail is a command that outputs the last part of a file.

The -n option specifies the number of lines to output, and in this case, it is set to 5. nowcoder.txt is the file that we want to output the last 5 lines of.

Therefore, the entire command tail -n 5 nowcoder.txt outputs the last 5 lines of the file nowcoder.txt.

shell3:

# 输出数字 0 到 500 中 7 的倍数
for ((i=0; i<=500; i+=7)); do echo $i; done 

To understand the purpose of the command for ((i=0; i<=500; i+=7)); do echo $i; done, let's break it down:

This is a for loop that iterates over the range of numbers from 0 to 500 with a step of 7. In each iteration, it prints the value of the loop variable i using the echo command. Therefore, the entire command outputs all the numbers from 0 to 500 that are multiples of 7.

This is a useful technique for generating a sequence of numbers that follow a certain pattern, such as multiples of a given number.

shell4:

# 使用sed命令输出第5行
sed -n '5p' nowcoder.txt 

The command sed -n '5p' nowcoder.txt is used to output the fifth line of the file nowcoder.txt.

To break it down, sed is a command that is used to perform text transformations on an input stream. The -n option is used to suppress the default behavior of sed which is to print every line of the input stream. Instead, it is used to specify a command to be executed on the input stream. In this case, the command is 5p, which means to print the fifth line of the input stream.

Therefore, the entire command sed -n '5p' nowcoder.txt outputs the fifth line of the file nowcoder.txt.

shell5:

# 使用grep命令查找空行并输出行号
grep -n '^$' nowcoder.txt | cut -d ':' -f 1

grep -n '^$' nowcoder.txt | cut -d ':' -f 1
The command grep -n '^$' nowcoder.txt | cut -d ':' -f 1 is used to find and output the line numbers of empty lines in the file nowcoder.txt.

To break it down, grep -n '^$' nowcoder.txt searches for lines that match the regular expression ^$, which matches empty lines. The -n option is used to print the line numbers of the matching lines. The output of this command is then piped to cut -d ':' -f 1, which uses the delimiter : to split the output into fields and outputs only the first field, which is the line number.

Therefore, the entire command grep -n '^$' nowcoder.txt | cut -d ':' -f 1 outputs the line numbers of empty lines in the file nowcoder.txt.

shell6:

# 使用sed命令删除空行并输出结果
sed '/^$/d' nowcoder.txt

sed '/^$/d' nowcoder.txt
The command sed '/^$/d' nowcoder.txt is used to delete empty lines from the file nowcoder.txt and output the result.

To break it down, sed is a command that is used to perform text transformations on an input stream. The regular expression ^$ matches empty lines. The command /^$/d deletes the empty lines from the input stream.

Therefore, the entire command sed '/^$/d' nowcoder.txt outputs the contents of the file nowcoder.txt with empty lines removed.

shell7:

# 使用grep命令查找字母数小于8的单词并输出
grep -oE '\b\w{1,7}\b' nowcoder.txt

The command grep -oE '\b\w{1,7}\b' nowcoder.txt is used to find and output all words in the file nowcoder.txt that are less than or equal to 7 characters long.

To break it down, grep is a command that searches for patterns in a file. The -o option is used to output only the matching parts of a line, rather than the entire line. The -E option is used to enable extended regular expressions. The regular expression '\b\w{1,7}\b' matches words that are between 1 and 7 characters long. The \b represents a word boundary, and \w represents a word character.

Therefore, the entire command grep -oE '\b\w{1,7}\b' nowcoder.txt outputs all words in the file nowcoder.txt that are less than or equal to 7 characters long.

shell8:

# 使用awk命令计算所有%MEM的和并输出
awk '{sum+=$4} END {print sum}' nowcoder.txt

To understand the purpose of the command awk '{sum+=$4} END {print sum}' nowcoder.txt, let's break it down:

awk is a command that is used to perform text processing and data extraction. In this case, it is used to calculate the sum of the fourth column of the file nowcoder.txt.

The command '{sum+=$4}' initializes a variable called sum to zero and then adds the value of the fourth column to it for each line of the input file.

The END keyword is a pattern that is executed after all the input has been processed. In this case, it is used to print the value of the sum variable.

Therefore, the entire command awk '{sum+=$4} END {print sum}' nowcoder.txt outputs the sum of the fourth column of the file nowcoder.txt.

shell9:

#shell统计一个文本文件nowcoder.txt 中每个单词出现的个数,先输出个数,再输出单词(升序),
#!/bin/bash
# 使用tr命令将空格替换为换行符
# 使用sort命令排序并统计重复行
# 使用uniq命令去重并统计重复行
# 输出每个单词出现的个数
tr -s ' ' '\n' < nowcoder.txt | sort | uniq -c

#shell统计一个文本文件nowcoder.txt 中每个单词出现的个数,先输出单词(升序),再输出个数,
#!/bin/bash
# 使用tr命令将空格替换为换行符
# 使用sort命令排序并统计重复行
# 使用uniq命令去重并统计重复行
# 使用awk命令调整输出格式,先输出单词,再输出个数
tr -s ' ' '\n' < nowcoder.txt | sort | uniq -c | awk '{print $2" "$1}'

#shell统计一个文本文件nowcoder.txt 中每个单词出现的个数,先输出单词,再输出个数,按照个数升序排序,
# 使用tr命令将空格替换为换行符
# 使用sort命令排序并统计重复行
# 使用uniq命令去重并统计重复行
# 使用awk命令调整输出格式,先输出单词,再输出个数
# 使用sort命令按照个数升序排序, n即numerical升序排序, nr表示numerical reverse降序排序
tr -s ' ' '\n' < nowcoder.txt | sort | uniq -c | awk '{print $2" "$1}' | sort -k2n

To count the number of occurrences of each word in the file "nowcoder.txt" and output them in ascending order, you can use the following command:


tr -s ' ' '\n' < nowcoder.txt | sort | uniq -c | awk '{print $2" "$1}'

This command uses the tr command to replace all spaces with newlines, then pipes the output to sort to sort the words alphabetically, and then pipes it to uniq -c to count the number of occurrences of each word. Finally, it uses awk to adjust the output format, printing the word first and then the count.

To sort the output by the count in ascending order, you can modify the command as follows:

shell10:

# 输出重复的第二列信息
awk '{a[$2]++} END {for (i in a) if (a[i]>1) print i}' nowcoder.txt

#   java
#   go

#!/bin/bash
# 使用awk命令统计第二列重复次数并排序输出
awk '{a[$2]++} END {for (i in a) print a[i], i}' nowcoder.txt | awk '$1>1{print $1, $2}'
#    2 java
#    3 go

#!/bin/bash
# 使用awk命令统计第二列重复次数并排序输出
awk '{a[$2]++} END {for (i in a) print a[i], i}' nowcoder.txt | sort -k1,1 -k2,2 | awk '$1>1{print $1, $2}'
#    2 java
#    3 go

#!/bin/bash
# 使用awk命令统计第二列重复次数并排序输出
awk '{a[$2]++} END {for (i in a) print a[i], i}' nowcoder.txt | sort -k1,1nr -k2,2 | awk '$1>1{print $1, $2}'

#   3 go
#   2 java

#    To understand the purpose of the command`sort -k1,1nr -k2,2`, let's break it down:  
#    sort is a command that sorts lines of text files.
#    -k1,1nr specifies the sorting key. -k1,1 means to sort by the first field (column) only, 
#    and nr means to sort numerically in reverse order (largest to smallest).
#    -k2,2 means to use the second field as a secondary sorting key, sorting alphabetically.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值