1、vim自動添加注釋及智能換行
1
2
3
4
5
6
7
8
9
10
11
12
13
<code># vi /etc/vimrc</code>
<code>"</code><code>set</code> <code>autoindent</code>
<code>set</code> <code>tabstop=4</code>
<code>set</code> <code>shiftwidth=4</code>
<code>function</code> <code>addtitle()</code>
<code>call setline(1,</code><code>"#!/bin/bash"</code><code>)</code>
<code>call append(1,</code><code>"#===================================================="</code><code>)</code>
<code>call append(2,</code><code>"# author: lizhenliang - email:[email protected]"</code><code>)</code>
<code>call append(3,</code><code>"# create date: "</code> <code>. strftime(</code><code>"%y-%m-%d"</code><code>))</code>
<code>"call append(4,"</code><code># filename: " . expand("%")) call append(4,"# description: ")</code>
<code>call append(5,</code><code>"#===================================================="</code><code>)</code>
<code>endf</code>
<code>map <f4> :call addtitle()<cr></code>
# vi test.sh #打開一個測試shell腳本,按f4就會自動添加注釋,省了不少時間:
2、查找并删除/data這個目錄7天前建立的檔案
# find /data -ctime +7 -exec rm -rf {} \;
# find /data -ctime +7 | xargs rm -rf
3、tar指令壓縮排除某個目錄
# tar zcvf data.tar.gz /data --exclude=tmp #--exclude參數為不包含某個目錄或檔案,後面也可以跟多個
4、檢視tar包存檔檔案,不解壓
# tar tf data.tar.gz #t是列出存檔檔案目錄,f是指定存檔檔案
5、使用stat指令檢視一個檔案的通路時間(access)、修改時間(modify)、狀态改變時間(change)
stat index.php
access: 2013-11-10 02:37:44.169014602 -0500
modify: 2013-06-18 10:53:14.395999032 -0400
change: 2013-06-18 10:53:38.855999002 -0400
6、批量解壓.tar.gz
方法1:# find . -name "*.tar.gz" -exec tar zxf {} \;
方法2:# for tar in *.tar.gz; do tar zxvf $tar; done
方法3:# ls *.tar.gz | xargs tar zxvf
7、篩除出檔案中的注釋和空格
方法1:grep -v "^#" httpd.conf |grep -v "^$"
方法2:# sed -e ‘/^$/d’ -e ‘/^#/d’ httpd.conf > http.conf
或者 # sed -e '/^#/d;/^$/d' #-e 執行多條sed指令
方法3:# awk '/^[^#]/|/"^$"' httpd.conf
或者 # awk '!/^#|^$/' httpd.conf
8、篩選/etc/passwd檔案中所有的使用者
方法1:# cat /etc/passwd |cut -d: -f1
方法2:# awk -f ":" '{print $1}' /etc/passwd
9、iptables網站跳轉
# iptables -a input -p tcp -m multiport --dport 22,80 -j accept
# echo 1 >/proc/sys/net/ipv4/ip_forward
# iptables -t nat -a prerouting -d 10.0.0.10 -p tcp --dport 80 -j dnat --to-destination 192.168.0.10:80
#将來自10.0.0.10的網站通路請求轉發到目标伺服器192.168.0.10。另外,内網伺服器要配置防火牆内網ip為網關,否則資料包回不來。另外,這裡不用配置snat,因為系統服務會根據資料包來源再傳回去。
10、iptables将本機80端口轉發到本地8080端口
# iptables -t nat -a prerouting -p tcp --dport 80 -j redirect --to-ports 8080
11、find指令查找檔案并複制到/opt目錄
方法1:# find /etc -name httpd.conf -exec cp -rf {} /opt/ \;: #-exec執行後面指令,{}代表前面輸出的結果,\;結束指令
方法2:# find /etc -name httpd.conf |xargs -i cp {} /opt #-i表示輸出的結果由{}代替
12、檢視根目錄下大于1g的檔案
# find / -size +1024m
預設機關是b,可以使用其他機關如,c、k、m
13、檢視伺服器ip連接配接數
# netstat -tun | awk '{print $5}' | cut -d: -f1 |sort | uniq -c | sort -n
-tun:-tu是顯示tcp和udp連接配接,n是以ip位址顯示
cut -d:-f1:cut是一個選擇性顯示一行的内容指令,-d指定:為分隔符,-f1顯示分隔符後的第一個字段。
uniq -c:報告或删除文中的重複行,-c在輸出行前面加上出現的次數
sort -n:根據不同類型進行排序,預設排序是升序,-r參數改為降序,-n是根據數值的大小進行排序
14、插入一行到391行,包括特殊符号"/"
# sed -i "391 s/^/addtype application\/x-httpd-php .php .html/" httpd.conf
15、列出nginx日志通路最多的ip
awk '{print $1}' access.log | sort | uniq -c | sort -nr | head -n 10
16、顯示通路量最多的前10位ip
# awk '{print $1}' access.log |sort |uniq -c|sort -nr |head -n 10
sort 排序
uniq -c 合并重複行,并記錄重複次數
sort -nr 按照數字進行降序排序
17、顯示nginx日志一天通路量最多的前10位ip
# awk '$4>="[16/may/2015:00:00:01" && $4<="[16/may/2015:23:59:59"' access_test.log |sort |uniq -c |sort-nr |head -n 10
# awk '$4>="[16/oct/2015:00:00:01" && $4<="[16/oct/2015:23:59:59"{a[$1]++}end{for(i in a){print a[i],i|"sort -k1 -nr |head -n 10"}}' access.log
18、擷取目前時間前一分鐘日志通路量
# date=`date +%d/%b/%y:%h:%m --date="-1 minute"` ; awk -vd=$date '$0~d{c++}end{print c}' access.log
# date=`date +%d/%b/%y:%h:%m --date="-1 minute"`; awk -vd=$date '$4>="["d":00" && $4<="["d":59"{c++}end{print c}' access.log
# grep `date +%d/%b/%y:%h:%m --date="-1 minute"` access.log |awk 'end{print nr}'
# start_time=`date +%d/%b/%y:%h:%m:%s --date="-5 minute"`;end_time=`date +%d/%b/%y:%h:%m:%s`;awk -vstart_time="[$start_time" -vend_time="[$end_time" '$4>=start_time && $4<=end_time{count++}end{print count}' access.log
19、找出1-255之間的整數
方法1:# ifconfig |grep -o '[0-9]\+' #+号比對前一個字元一次或多次
方法2:# ifconfig |egrep -o '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
20、找出ip位址
# ifconfig |grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' #-o隻顯示比對字元
21、給文檔增加開頭和結尾說明資訊
# awk ‘begin{print "開頭顯示資訊"}{print $1,$nf} end{print "結尾顯示資訊"}’/etc/passwd
# awk 'begin{printf " date ip\n------------------\n"} {print $3,$4} end{printf "------------------\nend...\n"}' /var/log/messages
date ip
------------------
03:13:01 localhost
10:51:45 localhost
end...
22、檢視網絡狀态指令
# netstat -antp #檢視所有網絡連接配接
# netstat -lntp #隻檢視監聽的端口資訊
# lsof -p pid #檢視程序打開的檔案句柄
# lsof -i:80 #檢視端口被哪個程序占用
23、生成8位随機字元串
方法1:# echo $random |md5sum |cut -c 1-8
方法2:# openssl rand -base64 4
方法3:# cat /proc/sys/kernel/random/uuid | cut -c 1-8
24、while死循環
while true; do #條件精确等于真,也可以直接用條件[ "1" == "1" ],條件一直為真
ping -c 2 www.baidu.com
done
25.awk格式化輸出
将文本列進行左對齊或右對齊。
左對齊:
# awk '{printf "%-15s %-10s %-20s\n",$1,$2,$3}' test.txt
右對齊:
# awk '{printf "%15s %10s %20s\n",$1,$2,$3}' test.txt
26.整數運算保留小數點
方法1:# echo 'scale=2; 10/3;'|bc #scale參數代表取小數點位數
方法2:# awk begin'{printf "%.2f\n",10/3}'
27.數字求和
# cat a.txt
23
53
56
方法1:
#!/bin/bash
while read num;
do
sum=`expr $sum + $num`
done < a.txt
echo $sum
方法2:# cat a.txt |awk '{sum+=$1}end{print sum}'
28、判斷是否為數字(字元串判斷也如此)
# [[ $num =~ ^[0-9]+$ ]] && echo yes || echo no #[[]]比[]更加通用,支援模式比對=~和字元串比較使用通配符
^ $:從開始到結束是數字才滿足條件
=~:一個操作符,表示左邊是否滿足右邊(作為一個模式)正規表達式
29、删除換行符并将空格替換别的字元
# cat a.txt |xargs echo -n |sed 's/[ ]/|/g' #-n 不換行
# cat a.txt |tr -d '\n' #删除換行符
30、檢視文本中20至30行内容(總共100行)
方法1:# awk '{if(nr > 20 && nr < 31) print $0}' test.txt
方法2:# sed -n '20,30p' test.txt
方法3:# head -30 test.txt |tail
31、文本中兩列位置替換
60.35.1.15 www.baidu.com
45.46.26.85 www.sina.com.cn
# cat a |awk '{print $2"\t"$1}'
32、監控目錄,新建立的檔案名追加到日志中
#要安裝inotify-tools軟體包
mon_dir=/opt
inotifywait -mq --format %f -e create $mon_dir |\
while read files; do
echo $files >> test.log
33、find一次查找多個指定檔案類型
# find ./ -name '*.jpg' -o -name '*.png'
# find ./ -regex ".*\.jpg\|.*\.png"
34、字元串拆分
# echo "hello" |awk -f '' '{for(i=1;i<=nf;i++)print $i}'
# echo "hello" |sed 's/./&\n/g'
# echo "hello" |sed -r 's/(.)/\1\n/g'
35、實時監控指令運作結果
# watch -d -n 1 'ifconfig'
36、解決郵件亂碼問題
# echo `echo "content" | iconv -f utf8 -t gbk` | mail -s "`echo "title" | iconv -f utf8 -t gbk`" [email protected]
注:通過iconv工具将内容字元集轉換
37、在文本中每隔三行添加一個換行或内容
# sed '4~3s/^/\n/' file
# awk '$0;nr%3==0{print "\n"}' file
# awk '{print nr%3?$0:$0 "\n"}' file
38、删除比對行及後一行或前一行
# sed '/abc/,+1d' file #删除比對行及後一行
# sed '/abc/{n;d}' file #删除後一行
# tac file |sed '/abc/,+1d' |tac #删除前一行
39、統計總行數
效率1 # wc -l file
效率2 # grep -c . file
效率3 # awk 'end{print nr}' file
效率4 # sed -n '$=' file
40、去除文本開頭和結尾空格
sed -i 's/^[ \t]*//;s/[ \t]*$//' file
41、給單個ip加單引号
# echo '10.10.10.1 10.10.10.2 10.10.10.3' |sed -r 's/[^ ]+/"&"/g'
# echo '10.10.10.1 10.10.10.2 10.10.10.3' |awk '{for(i=1;i<=nf;i++)printf "\047"$i"\047"}'
42、腳本中列印等待時間
wait(){
echo -n "wait 3s"
for ((i=1;i<=3;i++)); do
echo -n "."
sleep 1
echo
}
wait
43、删除指定行
# awk 'nr==1{next}{print $0}' file #$0可省略
# awk 'nr!=1{print}' file
# awk 'nr!=1{print $0}' 或删除比對行:awk '!/test/{print $0}'
# sed '1d' file
# sed -n '1!p' file
44、在指定行前後加一行
在第二行前一行加txt:
# awk 'nr==2{sub('/.*/',"txt\n&")}{print}' a.txt
# sed'2s/.*/txt\n&/' a.txt
在第二行後一行加txt:
# awk 'nr==2{sub('/.*/',"&\ntxt")}{print}' a.txt
# sed'2s/.*/&\ntxt/' a.txt
45、通過ip擷取網卡名
# ifconfig |awk -f'[: ]' '/^eth/{nic=$1}/192.168.18.15/{print nic}'
46、浮點數運算(數字46保留小數點)
# awk 'begin{print 46/100}'
0.46
# echo 46|awk '{print $0/100}'
# awk 'begin{printf "%.2f\n",46/100}'
# echo 'scale=2;46/100' |bc|sed 's/^/0/'
# printf "%.2f\n" $(echo "scale=2;46/100" |bc)
47、浮點數比較
if [ $(echo "4>3"|bc) -eq 1 ]; then
echo yes
else
echo no
fi
if [ $(awk 'begin{if(4>3)print 1;else print 0}') -eq 1 ]; then
48、替換換行符為逗号
$ cat a.txt
替換後:1,2,3
$ tr '\n' ',' < a.txt
$ sed ':a;n;s/\n/,/;$!b a' a.txt
$ sed ':a;$!n;s/\n/,/;t a' a.txt
while read line; do
a+=($line)
echo ${a[*]} |sed 's/ /,/g'
awk '{s=(s?s","$0:$0)}end{print s}' a.txt
#三目運算符(a?b:c),第一個s是變量,s?s","$0:$0,第一次處理1時,s變量沒有指派為假,結果列印1,第二次處理2時,s值是1,為真,結果1,2。以此類推,小括号可以不寫。
awk '{if($0!=3)printf "%s,",$0;else print $0}' a.txt
49、windows下文本到linux下隐藏格式去除
:set fileformat=unix
:%s/\r*$// #^m可用\r代替
sed -i 's/^m//g' a.txt #^m的輸入方式是ctrl+v,然後ctrl+m
dos2unix a.txt
50、xargs巧用
xargs -n1 #将單個字段作為一行
1 2
3 4
# xargs -n1 < a.txt
xargs -n2 #将兩個字段作為一行
$ cat b.txt
string
number
a
b
$ xargs -n2 < a.txt
string number
a 1
b 2
$ awk 'ors=nr%2?"\t":"\n"' b.txt #把奇數行換行符去掉