天天看點

sed和awk常用案例

替換

sed 's/old/new/g'和vi裡面%s/old/new/g一樣

根據關鍵字查詢某行其上或其下插入一樣

sed '/關鍵字/i   添加内容'   檔案  

a

b

c

d

[root@localhost soft]# sed -i '/a/i new number' test.txt 

[root@localhost soft]# cat test.txt                      

new number

其中i上在其上插入一行a是在其下面插入一行\n添加的内容換行插入的意思

後續增加

%s/^  //g  ,%s/   $//g替換空格為空用sed一樣

sed  -i '/^/&  /g' test.txt  在行首加入空格或者也可以加入内容行尾的話就改成$

比對列印關鍵字的那行sed -n '/關鍵字/p' test.txt ,sed -n '1p',sed -n '1,5p' 列印某行或範圍

cat test.txt |sed 's/  /\n/g' | grep -v "^$" |sort -nr | sed -n '1;$p' 将空格的内容換行然後過濾空行并列印最大和最小也就是第一和最後一行sort -nr是從大到小排列

cat test.txt | grep "關鍵字" | sed ‘s/old/new/g’過濾然後替換

以上是列印ip的幾種方法,道理差不多,隻是表現方式不同

[root@localhost ~]# awk -F: '/^root/{print $1}' /etc/passwd

這句指令是列印以root開頭的第一列,可以變化着用

awk切割多個分割符:範例[]可以實作

[root@localhost data]# cat 1.txt                 

a:b:c:d

121/121213/325ds/sfdd

ews|asfdsa|afsds|xzcds|

[root@localhost data]# awk -F'[:/|]' '{print $1}' 1.txt 

121

ews

 sed '/關鍵字/s/old/new/g'查找關鍵字查詢替換

sed合并兩個檔案範例:[root@192_168_77_189 mnt]# cat a b

33 218.108.34.254

42 202.103.24.68

22 202.11.23.43

21 212.12.13.12

33 杭州

42 武漢

22 河南

21 江蘇

[root@192_168_77_189 mnt]# awk 'NR==FNR {a[$1]=$0} NR>FNR {print a[$1],$2}' a b 

33 218.108.34.254 杭州

42 202.103.24.68 武漢

22 202.11.23.43 河南

21 212.12.13.12 江蘇

檔案一和檔案二比對每行相同字段合并并列印出來:[root@192_168_77_189 mnt]# cat a b

25 江蘇

[root@192_168_77_189 mnt]# awk 'NR==FNR {a[$1]=$0} NR>FNR {for (i in a) if (i==$1) print a[i],$2}' a b

意思是讀取第一個檔案把每行内容存入數組a[$1]然後讀取第二個檔案判斷$1等于數組a裡面的内容然後列印數組a[i]和第二個檔案的$2

[root@192_168_77_189 mnt]# cat c d

1 aa

2 bb

3 ee

4 ss

1 ab

2 cd

3 ad

4 bd

5 de

[root@192_168_77_189 mnt]# awk 'NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$1]}' c d

1 ab aa

2 cd bb

3 ad ee

4 bd ss

[root@192_168_77_189 mnt]# cat a.txt b.txt 

韓海林 21歲

海林韓 23歲

韓林海 22歲

林海韓 24歲

lixi   22歲

李明   25歲

韓林海 男

海林韓 男

韓海林 男

林海韓 男

*** 女

[root@192_168_77_189 mnt]# awk 'NR==FNR{a[$1]=$0}NR>FNR{if($1 in a)print a[$1],$2}' a.txt b.txt

韓林海 22歲 男

海林韓 23歲 男

韓海林 21歲 男

林海韓 24歲 男

列印出相同的字段合并輸出,讀取第一個檔案數組等于$0全部内容,讀取第二個檔案的時候判斷$1在數組裡面然後列印出來

比對列印ip:[root@192_168_77_189 mnt]# cat test.txt 

user_id   ip                createtime

1001    202.103.24.68        72

1002    192.168.2.4          2

1003    232.2.234.24           4

1004    232.232.25.3          21

[root@192_168_77_189 mnt]# cat test.txt |awk '{print $2}'| grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}"

202.103.24.68

192.168.2.4

232.2.234.24

232.232.25.3

cat -n可以列印行号

egrep "a|b" test.txt

df -h | grep "/$"| awk '{print $5}'|sed 's/%//g' 列印出磁盤使用率

cat test.txt | awk '{print $NF}'|sed 's/^/&01: /' 列印出最後一列在前面開頭加個字元01cat test.txt | awk '{print "01: "$NF}'也可以

find . -maxdepth 1 -type f -name “*.txt” -mtime +30 -exec rm -rf {}\;查找一級目錄的檔案30天以前的-mtime -1 一天以内的

sed '1s/old/new/g' test.txt  換第一行可以指定任何一行,sed -n '$p'   列印最後一行

sed  '/^關鍵字/p' |sed '/關鍵字$/p' test.txt  過濾列印

sed '/關鍵字/s/old/new/g'根據關鍵字查詢替換

cat  text.txt |awk '{sum+=$1}END{print sum}' 求和

sed -n '/時間*/,/時間/p'

sed -n '/17 05:30:29/,/17 05:31:07/p' messages按時間查找

找到日志裡面通路最多的ip按次數統計出來從大到小排列,uniq -c去重統計次數

cat access.log | awk '{print $1}' | 

awk '{print $1}'|sort |uniq -c |sort -nr|head 10 /access.log

awk '{print $1}' access.log |sort |uniq -c |sort -nr |head 10 sort -nr 從大到小排列

df -h | sed 's/%//g'|awk '{if($5>20) {print $0}}'

netstat -an |awk '/^tcp/ {print $NF}' | sort |uniq -c |sort -nr統計目前連接配接請求

awk按序号排列從0開始就NR-1:[root@192_168_77_189 ~]# awk -F: '{print $1}' /etc/passwd |awk '{print NR,$NF}'|head -10

1 root

2 bin

3 daemon

4 adm

5 lp

6 sync

7 shutdown

8 halt

9 mail

10 uucp

[root@192_168_77_189 ~]# cat test.txt 排序

['-1,1,12,1,3,87,78888,232,52323,276,137']

[root@192_168_77_189 ~]# cat test.txt |sed "s/\[//g;s/'//g;s/\]//g;s/,/ /g"|sed 's/ /,\n/g'|sort -n|tr -d "\n" |sed "s/^/&[\'/g" |sed "s/,$/']/g"

['-1,1,1,3,12,87,137232,276,52323,78888']

 幾種去除^M的方法:cat 222.txt | tr -d "\r" > newfile 或者sed -e "s/^V^M//" filename > outputfilename

awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"}' ipnew.txt處理文本列的間隔

netstat -nlptu |awk '{print $4,$7}' | grep 80 根據端口檢視哪個程式再用

http://www.regexlab.com/

參考連結

後續總結在一一添加

本文轉自 Anonymous123 51CTO部落格,原文連結:http://blog.51cto.com/woshitieren/1678152

繼續閱讀