zoukankan      html  css  js  c++  java
  • grep, sed 和 awk 学习总结

    以前粗略学习过,今天再复习一遍并做笔记,方便自己查阅。

    1. grep/egrep:

    grep 'root' test.txt 显示出 root 关键字所在行
    grep -c 'root' test.txt 显示存在关键字 root 的行的总数
    grep -n 'root' test.txt 显示出 root 关键字所在行以及行号,如: 1:root:x:0:0:root:/root:/bin/bash
    grep -n --color 'root' test.txt 显示出 root 关键字所在行以及行并且行号颜色标记,如:1:root:x:0:0:root:/root:/bin/bash

    alias grep='grep --color'

    grep -n 'root' test.txt

    同上。 alias 是设置别名,删除别名用:unalias grep
    grep -o 'root' test.txt|wc -l 显示出该文本中 root 关键字的总个数
    grep -o 'root' test.txt 显示出该文本中所有的 root 关键字
    grep -v 'root' test.txt 取反: 显示出所有不包含 root 关键字的行
    grep -A1 -n 'root' test.txt 显示包含关键字 root 的行以及后面1行,也可写A2,A3.....
    grep -B1 -n 'root' test.txt 显示包含关键字 root 的行以及前面1行,也可写B2,B3.....
    grep -C1 -n 'root' test.txt 显示包含关键字 root 的行以及前后各1行,也可写C1,C2.....
    grep -r 'root' /etc/ 遍历文件夹下所有文件中有关键字 root 的行
    grep '[0-9]' test.txt 过滤显示包含数字的行
    grep -v '[0-9]' test.txt 过滤显示不包含数字的行
    grep -v '^#' test.txt 显示不以#开头的行
    grep -n 'n$' test.txt 显示以n 结尾的行
    grep -v '^$' test.txt|grep -v '^#' 显示除空行和以“#”开头的行以外的行
    grep '^[a-zA-Z]' test.txt 显示以字母开头的行
    grep -v '^[0-9]' test.txt 或者 grep '^[^0-9]' test.txt 显示非数字开头的行
    grep 'ro*t' test.txt * 代表0/多个前面的字符,即rt,rot,rooooooooo....t
    grep -E 'ro+t' test.txt 或者egrep 'ro+t' test.txt + 代表1/多个前面的字符,即rot,rooooooooo....t
    grep 'ro.t' test.txt . 代表任意一个字符,即roat,robt,ro0t,....
    grep -E 'ro?t' test.txt 或者 egrep 'ro?t' test.txt ? 代表0/1个前面的字符,即rt,rot
    grep 'ro.*t' test.txt 显示有 ro 开头 t 结尾的关键字所在的行

    egrep 'root|mysql' test.txt 或者 grep 'root|mysql' test.txt

    或者 grep -E 'root|mysql' test.txt

    显示有root 或者mysql 关键字的行
    grep -E '(oo)+' test.txt 显示有1个或多个oo 的行
    grep -E '(oo){2}' test.txt 显示2个oo 的行

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    2. sed

    sed '1p' -n test.txt 打印第1行
    grep -n '.*' test.txt|sed '1p' -n 打印第一行并且显示行号
    sed '1,5p' -n test.txt 打印前5行
    sed '5,$p' -n test.txt 打印第5行到最后
    sed '/root/p' -n test.txt 打印包含关键字 root 的行
    sed -r '/ro?t/p' -n test.txt 或者sed '/ro?t/p' -n test.txt +,? 需要加-r 或者转义符号
    sed -e '/root/p' -e '/mysql/p' -n test.txt 打印包含 root 或者mysql 关键字的行
    sed '/root/p;/mysql/p' -n test.txt 打印包含 root 或者mysql 关键字的行
    sed '1,5d' test.txt 删除1到第5行
    sed -r '/root|mysql/d' test.txt 删除有root或mysql的行
    sed '1,10s/root/oooo/g' test.txt 1到10行替换root 为oooo; /g 表示全局
    sed 's/[0-9]//g' test.txt 数字全部删掉
    sed 's/[^0-9]//g' test.txt 非数字全部删掉
    sed -r 's/([^:]+)(:.*:)([^:]+$)/321/' 交换位置
    sed -i 's/root/oooo/g' test.txt 更改源文件所有的root 替换为oooo, 只有加了-i 才会更改源文件

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

    sed 有匹配的功能,但是它的主要功能还是替换,如果单纯过滤匹配用grep更方便,不加 -i 的情况下只是显示,只有加了 -i 才会真正更改文件

     

     

    3. awk

    awk -F 'root' '{print $2}' test.txt 以root为分隔符,打印第二段,不加-F 默认以空格隔开
    awk -F ':' '$1~/root/' test.txt 打印以 : 隔开,第一段包含关键字 root 的 行
    awk -F ':' '$1~/root/ {print $3,$4}' test.txt 打印以 : 隔开,第一段包含关键字 root 的行的第3和第4段
    awk -F ':' '{OFS="...";} $1~/root/ {print $3,$4}' 打印以 : 隔开,第一段包含关键字 root 的行的第3和第4段,并且显示结果以“...” 隔开
    awk -F ':' '$1=="root" {print $1,$2}' test.txt 打印以 : 隔开,第一段等于root的第1和第2段
    awk -F ':' '$1=="root" || NR >20 {print $1,$2}' test.txt 打印以 : 隔开,第一段等于root 或者行数大于20 的第1和第2段
    awk -F ':' '$1=="root" && NR >20 {print $1,$2}' test.txt 打印以 : 隔开,第一段等于root 并且行数大于20 的第1和第2段
    awk -F ':' 'NF>3 && NR<6 {print}' test.txt 打印段数大于3行数小于6的行
  • 相关阅读:
    java基本类型和引用做形参传递
    新阶段新开始
    给网页中的button加动画效果
    数组对象常用的几个函数总结
    微信小程序使用页面栈改变上一页面的数据
    pc端和移动端的“窗口”(viewport)故事(part1)
    js的for循环中出现异步函数,回调引用的循环值总是最后一步的值?
    macOS Sierra 如何安装任何来源的软件
    Box-shadow制作漂亮的外阴影输入框
    button标签与input type=button标签使用的差异
  • 原文地址:https://www.cnblogs.com/nothingc/p/7159778.html
Copyright © 2011-2022 走看看