sed和awk常用案例

简介:
  1. 替换

    sed 's/old/new/g'和vi里面%s/old/new/g一样

  2. 根据关键字查询某行其上或其下插入一样

    sed '/关键字/i   添加内容'   文件  

    案例:root@localhost soft]# cat test.txt                   
    a
    b
    c
    d
    [root@localhost soft]# sed -i '/a/i new number' test.txt 
    [root@localhost soft]# cat test.txt                      
    new number
    a
    b
    c
    d

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

    后续增加

  3. %s/^  //g  ,%s/   $//g替换空格为空用sed一样

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

  5. 匹配打印关键字的那行sed -n '/关键字/p' test.txt ,sed -n '1p',sed -n '1,5p' 打印某行或范围

  6. cat test.txt |sed 's/  /\n/g' | grep -v "^$" |sort -nr | sed -n '1;$p' 将空格的内容换行然后过滤空行并打印最大和最小也就是第一和最后一行sort -nr是从大到小排列

  7. cat test.txt | grep "关键字" | sed ‘s/old/new/g’过滤然后替换

  8. root@localhost ~]# ifconfig | grep "Bcast" | awk '{print $2}'|sed 's/addr://g'                   

     root@localhost ~]# ifconfig | grep "Bcast" | awk '{print $2}'|cut -d: -f2

    root@localhost ~]# ifconfig | grep "Bcast" | awk '{print $2}'|awk -F: '{print $2}'

    以上是打印ip的几种方法,道理差不多,只是表现方式不同

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

    这句命令是打印以root开头的第一列,可以变化着用

  10. 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 
    a
    121
    ews

  11.  sed '/关键字/s/old/new/g'查找关键字查询替换

  12. 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 江苏

  13. 文件一和文件二匹配每行相同字段合并并打印出来:[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 河南
    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
    33 218.108.34.254 杭州
    42 202.103.24.68 武汉
    22 202.11.23.43 河南

    意思是读取第一个文件把每行内容存入数组a[$1]然后读取第二个文件判断$1等于数组a里面的内容然后打印数组a[i]和第二个文件的$2

  14. [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
    5 de

  15. [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在数组里面然后打印出来

  16. 匹配打印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

  17. cat -n可以打印行号

  18. egrep "a|b" test.txt

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

  20. cat test.txt | awk '{print $NF}'|sed 's/^/&01: /' 打印出最后一列在前面开头加个字符01cat test.txt | awk '{print "01: "$NF}'也可以

  21. find . -maxdepth 1 -type f -name “*.txt” -mtime +30 -exec rm -rf {}\;查找一级目录的文件30天以前的-mtime -1 一天以内的

  22. sed '1s/old/new/g' test.txt  换第一行可以指定任何一行,sed -n '$p'   打印最后一行

  23. sed  '/^关键字/p' |sed '/关键字$/p' test.txt  过滤打印

  24. sed '/关键字/s/old/new/g'根据关键字查询替换

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

  26. sed -n '/时间*/,/时间/p'
    sed -n '/17 05:30:29/,/17 05:31:07/p' messages按时间查找

  27. 找到日志里面访问最多的ip按次数统计出来从大到小排列,uniq -c去重统计次数
    cat access.log | awk '{print $1}' | 
    awk '{print $1}'|sort |uniq -c |sort -nr|head 10 /access.log

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

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

  30. netstat -an |awk '/^tcp/ {print $NF}' | sort |uniq -c |sort -nr统计当前连接请求

  31. 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

  32. [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']

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

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

  35. netstat -nlptu |awk '{print $4,$7}' | grep 80 根据端口查看哪个程序再用

 http://www.92csz.com/study/linux/13.htm

http://www.regexlab.com/

参考链接

后续总结在一一添加

 

本文转自 Anonymous123 51CTO博客,原文链接:http://blog.51cto.com/woshitieren/1678152


相关文章
|
机器学习/深度学习 Perl 5G