最近因為工作原因開始接觸Linux,需要做一些簡單的Shell開發。
在牛客網練習了一下,來記錄自己的學習過程。
第1題:統計文件的行數
題目:
寫一個 bash腳本以輸出一個文本文件 nowcoder.txt中的行數
示例:
假設 nowcoder.txt 內容如下:
1
2
3
4
5
6
7
8
9
|
#include <iostream>
using
namespace
std;
int
main()
{
int
a = 10;
int
b = 100;
cout <<
"a + b:"
<< a + b << endl;
return
0;
}
|
你的腳本應當輸出:
9
思路:
思路較簡單。
程序:
wc -l
第2題:打印文件的最后5行
題目:
經常查看日志的時候,會從文件的末尾往前查看,於是請你寫一個 bash腳本以輸出一個文本文件 nowcoder.txt中的最后5行
示例:
假設 nowcoder.txt 內容如下:
你的腳本應當輸出:
思路:
思路較簡單。
程序:
tail -5 nowcoder.txt
第3題:輸出7的倍數
題目:
寫一個 bash腳本以輸出數字 0 到 500 中 7 的倍數(0 7 14 21...)的命令
思路:
類似其他語言,構造一個循環。
程序:
for i in {0..500..7} do echo $i done
第4題:輸出第5行的內容
題目:
寫一個 bash腳本以輸出一個文本文件 nowcoder.txt 中第5行的內容。
示例:
假設 nowcoder.txt 內容如下:
welcome
to
nowcoder
this
is
shell
code
你的腳本應當輸出:
is
思路:
使用NR和$0
程序:
awk '{if (NR == 5) print $0}' nowcoder.txt
第5題:打印空行的行號
題目:
示例:
假設 nowcoder.txt 內容如下:
1
2
3
4
5
6
7
8
9
10
11
|
a
b
c
d
e
f
|
你的腳本應當輸出:
3
5
7
9
10
思路:
使用^$
程序:
awk '/^$/ {print NR}' nowcoder.txt
第6題:去掉空行
題目:
寫一個 bash腳本以去掉一個文本文件 nowcoder.txt中的空行
示例:
假設 nowcoder.txt 內容如下:
1
2
3
4
5
6
7
8
9
10
11
|
abc
567
aaa
bbb
ccc
|
你的腳本應當輸出:
abc
567
aaa
bbb
ccc
思路:
兩個思路,一個使用sed來,一個使用awk來。
程序1:
sed '/^$/d' nowcoder.txt
程序2:
awk '{if (length($1) >= 1) print $0}'
第7題:打印字母數小於8的單詞
題目:
寫一個 bash腳本以統計一個文本文件 nowcoder.txt中字母數小於8的單詞。
示例:
假設 nowcoder.txt 內容如下:
how they are implemented and applied in computer
你的腳本應當輸出:
how
they
are
in
說明:
不要擔心你輸出的空格以及換行的問題
思路:
使用awk中NF來確定字段數,並且使用判斷條件來進行判斷。
程序:
awk '{for(i=1;i<=NF;i++) if(length($i)<8) print $i}' nowcoder.txt
第8題:統計所有進程占用內存大小的和
題目:
假設 nowcoder.txt 內容如下:
root 2 0.0 0.0 0 0 ? S 9月25 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? I< 9月25 0:00 [kworker/0:0H]
web 1638 1.8 1.8 6311352 612400 ? Sl 10月16 21:52 test
web 1639 2.0 1.8 6311352 612401 ? Sl 10月16 21:52 test
tangmiao-pc 5336 0.0 1.4 9100240 238544 ?? S 3:09下午 0:31.70 /Applications
以上內容是通過ps aux | grep -v 'RSS TTY' 命令輸出到nowcoder.txt文件下面的
請你寫一個腳本計算一下所有進程占用內存大小的和:
思路:
使用累計的思想。
程序:
awk 'BEGIN{sum=0} {sum=sum+$6} END{print sum}' nowcoder.txt
第9題:去掉包含this的句子
題目:
寫一個 bash腳本以實現一個需求,去掉輸入中含有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
你的腳本獲取以上輸入應當輸出:
to the degree or extent indicated.
welcome to nowcoder
說明:
你可以不用在意輸出的格式,包括空格和換行
思路:
使用grep
程序:
grep -v this nowcoder.txt
第10題:求平均值
題目:
寫一個 bash腳本以實現一個需求,去掉輸入中含有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
你的腳本獲取以上輸入應當輸出:
to the degree or extent indicated.
welcome to nowcoder
說明:
你可以不用在意輸出的格式,包括空格和換行
思路:
使用NR找行,然后根據不同的情況來統計和計算數字。
程序:
awk ' BEGIN{sum=0} {if(NR==1) {num=$0} else {sum=sum+$0}} END{printf("%.3f",sum/num)}'
第11題:去掉不需要的單詞
題目:
寫一個 bash腳本以實現一個需求,去掉輸入中的含有B和b的單詞
示例:
假設輸入如下:
big
nowcoder
Betty
basic
test
你的腳本獲取以上輸入應當輸出:
nowcoder test
說明:
你可以不用在意輸出的格式,空格和換行都行
思路:
使用grep
程序:
grep -v 'B\|b'
第12題:轉置文件的內容
題目:
寫一個 bash腳本來轉置文本文件nowcoder.txt中的文件內容。
為了簡單起見,你可以假設:
你可以假設每行列數相同,並且每個字段由空格分隔
示例:
假設 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 (NR==1){ res[i]=$i } else{ res[i]=res[i]" "$i } } }END{ for(j=1;j<=NF;j++){ print res[j] } }' nowcoder.txt
第13題:第二列是否有重復
題目:
給定一個 nowcoder.txt文件,其中有3列信息,如下實例,編寫一個sheel腳本來檢查文件第二列是否有重復,且有幾個重復,並提取出重復的行的第二列信息:
實例:
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
思路:
使用uniq -c來統計信息。
程序:
awk '{print $2}' nowcoder.txt | sort| uniq -c| sort| grep -v '1'
第14題:統計每個單詞出現的個數
題目:
寫一個 bash腳本以統計一個文本文件 nowcoder.txt 中每個單詞出現的個數。
為了簡單起見,你可以假設:
nowcoder.txt只包括小寫字母和空格。
每個單詞只由小寫字母組成。
單詞間由一個或多個空格字符分隔。
示例:
假設 nowcoder.txt 內容如下:
welcome nowcoder
welcome to nowcoder
nowcoder
你的腳本應當輸出(以詞頻升序排列):
to 1
welcome 2
nowcoder 3
說明:
不要擔心個數相同的單詞的排序問題,每個單詞出現的個數都是唯一的。
思路:
使用sort -k來指定列進行排序。
程序:
awk '{ for (i=1;i<=NF;i++) { a[$i]++ }} END{ for(j in a) { print j,a[j] } }' nowcoder.txt |sort -k 2
今天的學習就到這里,那個原第12題有些難度,明天來研究。
懷挺~~~
第15題:打印每一行出現的數字個數
題目:
寫一個 bash腳本以統計一個文本文件 nowcoder.txt中每一行出現的1,2,3,4,5數字個數並且要計算一下整個文檔中一共出現了幾個1,2,3,4,5數字數字總數。
示例:
假設 nowcoder.txt 內容如下:
a12b8
10ccc
2521abc
9asf
你的腳本應當輸出:
line1 number: 2
line2 number: 1
line3 number: 4
line4 number: 0
sum is 7
說明:
不要擔心你輸出的空格以及換行的問題
思路:
看了別人的思路,用循環來做。
程序:
awk '{ count=0; len=length($0); for(i=1;i<=len;i++) {s=substr($0,i,1) if(0<s && s<6) {total++; count++}}; printf("line%d number:%d\n",NR,count);} END{printf("sum is %d\n",total)}'