統計詞頻
寫一個 bash 腳本以統計一個文本文件 words.txt 中每個單詞出現的頻率。
爲了簡單起見,你可以假設:
- words.txt只包括小寫字母和 ' ' 。
- 每個單詞只由小寫字母組成。
- 單詞間由一個或多個空格字符分隔。
- 示例:
假設 words.txt 內容如下:
the day is sunny the the
the sunny is is
你的腳本應當輸出(以詞頻降序排列):
the 4
is 3
sunny 2
day 1
cat t.txt | xargs -n1 | sort | uniq -c | awk '{print $2 " " $1}' | sort -r -k2
用xargs去除換行符的影響,sort和uniq -c來去除重複,最後逆序輸出再排序
轉置文件
給定一個文件 file.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){ a[i] = $i; } else { a[i] = a[i]" "$i; } } }END{ for(i=1;i<=NF;i++){ print a[i]; } }' file.txt
利用awk變量輸出
輸出文件第十行
給定一個文本文件 file.txt,請只打印這個文件中的第十行。
示例:
假設 file.txt 有如下內容:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
你的腳本應當顯示第十行:
Line 10
awk 'NR==10' file.txt
awk 'BEGIN{i=0} {i++;if(i==10)print} ' file.txt
awk '{if(NR==10)print}' file.txt
tail -n+10 file.txt|head -1 tail -n +10表示從第10行開始輸出
sed -n 10p file.txt -n表示只輸出匹配行,p表示Print
排序
用shell處理以下內容
1、按單詞出現頻率降序排序!
2、按字母出現頻率降序排序!
zimu=`echo 'the squid project provides a number of resources toassist users design,implement and sup port squid installations. Please browsethe documentation and support sections for more infomation'|sed -r 's#[.,]# #g'` echo $zimu|xargs -n1|awk '{array[$1]++}END{for(j in array)print j,array[j]}'|sort -k2 -rn|column -t echo $zimu|grep -o "[a-Z]"|sort|uniq -c |sort -nr |column -t
column -t用來將輸出排列更整齊
匹配md5
已知下面的字符串是通過RANDOM隨機數變量md5sum|cut-c 1-8截取後的結果,請破解這些字符串對應的md5sum前的RANDOM對應數字?
21029299
00205d1c
a3da1677
1f6d12dd
#!/bin/bash a=(21029299 00205d1c a3da1677 1f6d12dd) for n in {0..32767} do random=`echo $n|md5sum|cut -c1-8` for((i=0;i<=${#a[@]};i++)) do if [ "$random" == "${a[i]}" ];then echo "$n" "${a[i]}" fi done done
使用for循環在/oldboy目錄下批量創建10個html文件
mkdir ./oldboy && cd ./oldboy && for i in `seq 10`;do a=`echo $RANDOM|md5sum|tr "0-9" "j-z"|cut -c1-10`;touch ${a}_oldboy.html;done
請用至少兩種方法實現
將以上文件名中的oldboy全部改成oldgirl(用for循環實現),並且html改成大寫。
rename的方法
cd /oldboy && rename _oldboy.html _oldgirl.HTML *
for循環
cd /oldboy for i in `find /oldboy -type f -name "*_oldboy.html"` do b=`echo $i|cut -c 1-19` mv ${b}oldboy.html ${b}oldgirl.HTML done cd /oldboy for i in `find /oldboy -type f -name "*_oldboy.html"` do b=`echo $i |sed -r 's#(.*)oldboy.html#\1#g'` mv ${b}oldboy.html ${b}oldgirl.HTML done
寫一個腳本,實現判斷10.0.0.0/24網絡裏,當前在線用戶的IP有哪些
IP=10.0.0. for j in {1..254} do ping -c 1 -w 2s $IP$j &>/dev/null if [ $? -eq 0 ];then action "$IP$j " else action "$IP$j" /bin/false fi done
請用至少兩種方法實現!循環、數組方式
for循環打印下面這句話中字母數不大於6的單詞(崑崙萬維面試題)。
I am oldboy teacher welcome to oldboy training class.
#!/bin/sh for i in I am oldboy teacher welcome to oldboy training class. do if [ ${#i} -lt 6 ];then echo $i fi done
echo "I am oldboy teacher welcome to oldboy training class."|xargs -n1|awk '{if(length<6)print}'
echo "I am oldboy teacher welcome to oldboy training class."|awk '{for(i=1;i<=NF;i++)if(length($i)<6)print $i}'
計算從1加到100之和
seq 100|awk '{i=i+$1}END{print i}'
監控web站點目錄下所有文件
是否被惡意篡改(文件內容被改了)
web站點目錄(/var/html/www)
find /var/html/www/ -type f -name "*.thml"|xargs md5sum >/tmp/a.log md5sum -c /tmp/a.log
以腳本傳參以及read讀入的方式比較2個整數大小
#!/bin/bash read -p "Please input two Number: " -a Arr_str echo ${Arr_str[*]} | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit if [ ${#Arr_str[*]} -eq 2 ];then if [ ${Arr_str[0]} -eq ${Arr_str[1]} ];then echo "${Arr_str[0]} == ${Arr_str[1]}" elif [ ${Arr_str[0]} -gt ${Arr_str[1]} ];then echo "${Arr_str[0]} > ${Arr_str[1]}" else echo "${Arr_str[0]} < ${Arr_str[1]}" fi else echo "Please input two Number" fi
echo $1 | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit echo $2 | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit if [ $# -eq 2 ];then if [ $1 -eq $2 ];then echo "$1 == $2" elif [ $1 -gt $2 ];then echo "$1 > $2" else echo "$1 < $2" fi else echo "Please input two Number" fi