题目:输出第5行的内容
描述
编写一个bash脚本以输出一个文本文件nowcoder.txt中第5行的内容。
示例
输入:
welcome
to
nowcoder
this
is
shell
code
输出:
is
代码
awk 'NR==5{print $0}' nowcoder.txt
题目:打印空行的行号
描述
编写一个bash脚本以输出一个文本文件nowcoder.txt中第5行的内容。
示例
输入:
a
b
c
d
e
f
输出:
3
5
7
9
10
代码
awk 'BEGIN{RS="\\n"; FS="\\n";}{if(length($0)==0) print NR}' nowcoder.txt
题目:打印字母数小于8的单词
描述
写一个bash脚本以统计一个文本文件nowcoder.txt中字母数小于8的单词。
示例
输入:how they are implemented and applied in computer
输出:
how
they
are
and
applied
in
代码
awk 'NR==5{print $0}' nowcoder.txt
题目:统计所有进程占用内存百分比的和
描述
以下内容是通过ps aux命令输出到nowcoder.txt文件中的,请你写一个脚本计算一下所有进程占用内存大小的和。
示例
输入:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 77744 8332 ? Ss 2021 1:15 /sbin/init noibrs splash
root 2 0.0 0.0 0 0 ? S 2021 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< 2021 0:00 [kworker/0:0H]
daemon 486 0.0 0.1 28340 2372 ? Ss 2021 0:00 /usr/sbin/atd -f
root 586 0.0 0.3 72308 6244 ? Ss 2021 0:01 /usr/sbin/sshd -D
root 12847 0.0 0.0 4528 68 ? S< Jan03 0:13 /usr/sbin/atopacctd
root 16306 1.7 1.2 151964 26132 ? S<sl Apr15 512:03 /usr/local/aegis/aegis_client/aegis_11_25/AliYunDun
root 24143 0.0 0.4 25608 8652 ? S<Ls 00:00 0:03 /usr/bin/atop -R -w /var/log/atop/atop_20220505 600
root 24901 0.0 0.3 107792 7008 ? Ss 15:37 0:00 sshd: root@pts/0
root 24903 0.0 0.3 76532 7580 ? Ss 15:37 0:00 /lib/systemd/systemd --user
root 24904 0.0 0.1 111520 2392 ? S 15:37 0:00 (sd-pam)
输出:
3.1
代码
awk -v 'total_memory=0' '{total_memory+=$4} END {printf "%.1f", total_memory}' nowcoder.txt
题目:统计每个单词出现的个数
描述
写一个bash脚本以统计一个文本文件nowcoder.txt 中每个单词出现的个数。
示例
输入:
welcome nowcoder
welcome to nowcoder
nowcoder
输出:
to 1
welcome 2
nowcoder 3
代码
awk '{for(i=1;i<=NF;i++){count[$i]++}} END {for(word in count){print word, count[word]}}' nowcoder.txt | sort -k2n
题目:第二列是否有重复
描述
给定一个nowcoder.txt文件,其中有3列信息,如下:
20201001 python 99
20201002 go 80
20201002 c++ 88
20201003 php 77
20201001 go 88
20201005 shell 89
20201006 java 70
20201008 c 100
20201007 java 88
20201006 go 97
编写一个shell脚本来检查文件第二列是否有重复,且有几个重复,并提取出重复的行的第二列信息(先按次数排序,如果次数相同,按照单词字母顺序排序),输入如下:
2 java
3 go
示例
输入:
20201001 python 99
20201002 go 80
20201002 c++ 88
20201003 php 77
20201001 go 88
20201005 shell 89
20201006 java 70
20201008 c 100
20201007 java 88
20201006 go 97
输出:
2 java
3 go
代码
awk '{count[$2]++} END {for(word in count){if(count[word]>1){print count[word] word}}}' nowcoder.txt | sort -k2n
题目:转置文件的内容
描述
写一个bash脚本来转置文本文件nowcoder.txt中的文件内容。
文件中每行列数相同,并且每个字段由空格分隔
示例
输入:
job salary
c++ 13
java 14
php 12
输出:
job c++ java php
salary 13 14 12
代码
awk '
{
# 对于每一行,将每一列的值存储到数组中
for (i = 1; i <= NF; i++) {
# 初始化数组,如果还没有这个索引
if (!a[i]) a[i] = ""
a[i] = a[i] $i " "
}
}
END {
# 遍历数组,输出转置后的内容
for (i = 1; i <= length(a); i++) {
print substr(a[i], 1, length(a[i]) - 1) # 删除末尾的空格
}
}' nowcoder.txt
题目:打印每一行出现的数字个数
描述
写一个bash脚本,统计一个文本文件nowcoder.txt中每一行出现的15数字的个数,并且计算一下整个文档中一共出现了几个15数字的总数。
示例
输入:
a12b8
10ccc
2521abc
9asf
输出:
line1 number: 2
line2 number: 1
line3 number: 4
line4 number: 0
sum is 7
代码
awk '{
count=0
for (i=1; i<=5; i++) {
count += gsub(i, "")
}
print "line" NR " number: " count
sum += count
}
END {
print "sum is " sum
}' nowcoder.txt
题目:去掉所有包含this的句子
描述
编写一个shell脚本以实现如下功能:去掉输入中含有this的语句,把不含this的语句输出
示例
输入:
that is your bag
is this your bag?
to the degree or extent indicated.
there was a court case resulting from this incident
welcome to nowcoder
输出:
that is your bag
to the degree or extent indicated.
welcome to nowcoder
代码
awk 'NF && !/this/ {print $0}' nowcoder.txt
题目:求平均值
描述
写一个bash脚本以实现一个需求,求输入的一个数组的平均值
第1行为输入的数组长度N
第2~N行为数组的元素,如以下为:
数组长度为4,数组元素为1 2 9 8
示例:
4
1
2
9
8
那么平均值为:5.000(保留小数点后面3位)
你的脚本获取以上输入应当输出:
5.000
示例
输入:
4
1
2
9
8
输出:
5.000
代码
awk '
BEGIN {
# 读取数组长度
getline n
# 初始化总和变量
sum = 0
# 循环读取数组元素并累加
for (i = 1; i <= n; i++) {
getline element
sum += element
}
# 计算平均值
average = sum / n
# 输出平均值,保留三位小数
printf "%.3f", average
}
' nowcoder.txt