簡介
每天的重複工作感覺自己沒有什麼進步,在社會中的時間越長感覺到的壓力越大,作爲一個無法逃脫的社畜,能怎麼辦呢
以前還沒有感覺到什麼壓力,隨着年齡 工作 社會環境 心態的變化吧,感覺壓力越來越大,自己也比較懶散
下面的shell題,也是從網上看到的,希望能增加自己的熟練度吧
shell
統計詞頻 將一個文本中的單詞的出現頻率統計出來
下面是從ssh配置文件中複製過來的一段,當作words.txt中的內容,
words.txt
The strategy used for options in the default sshd_config shipped with
OpenSSH is to specify options with their default value where
possible, but leave them commented. Uncommented options override the
default value.
你的腳本應當輸出(以詞頻降序排列):
options 3
default 3
with 2
the 2
where 1
value. 1
value 1
used 1
Uncommented 1
to 1
them 1
- 答案
awk '{for(i=1;i<= NF;i++){words[$i]++}}END{for(w in words){print w,words[w]}}' w.txt |sort -rn -k 2
for i in $(cat w.txt);do echo $i;done|sort |uniq -c|sort -rn|awk '{print $2,$1}'
統計合格的手機號碼
w.txt
987-123-4567
123 456 7890
(123) 456-7890
grep -P '^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$' w.txt
轉置文件
假設 file.txt 文件內容如下:
name age
alice 21
ryan 30
應當輸出:
name alice ryan
age 21 30
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]
}
}' file.txt
作者:gao-si-huang-bu
鏈接:https://leetcode-cn.com/problems/transpose-file/solution/awkming-ling-yong-shu-zu-chu-cun-dai-shu-chu-jie-g/
來源:力扣(LeetCode)