美文网首页
linux文本处理工具

linux文本处理工具

作者: 小小的小 | 来源:发表于2021-05-27 17:32 被阅读0次

cat  [options]...[file]...

options

  -E  显示行结束符

  -A  显示所有控制符

  -n  对显示出的每一行进行编号

  -b  只对非空号进行编号,有空格的行也是非空行,相当于nl命令

  -s  将连续的空行压缩成一行显示

diff命令, 比较两个文本文件的不同

cmp  compore 比较两个二进制文件的不同

hexdump

    -s # 从#字节开始

    -C 二进制文件名或/dev/sda....

    -n  显示多少个

hexdump  -C /dev/sdb  -s  0  -n  446

[root@centos7-test test]# hexdump  -C /bin/ls  -s 0  -n 100

00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|

00000010  02 00 3e 00 01 00 00 00  24 43 40 00 00 00 00 00  |..>.....$C@.....|

00000020  40 00 00 00 00 00 00 00  e8 c3 01 00 00 00 00 00  |@...............|

00000030  00 00 00 00 40 00 38 00  09 00 40 00 1e 00 1d 00  |....@.8...@.....|

00000040  06 00 00 00 05 00 00 00  40 00 00 00 00 00 00 00  |........@.......|

00000050  40 00 40 00 00 00 00 00  40 00 40 00 00 00 00 00  |@.@.....@.@.....|

00000060  f8 01 00 00                                      |....|

00000064

2021.2.7 update

正则表达式  处理文本字符 

支持的命令 less,grep

  基本正则表达式

  扩展正则表达式

基本正则表达式

字符匹配

  . 任意一个字符

  * 表示所有

  [] 任意范围内单个字符

  [^] 除括号中的任意单个字符

匹配次数

  * 星号前字符任意次,包括0次

  .* 任意长度任意字符

  \? 前一个字符出现0或1次

  \+ 至少出现1次

  \{n\}    前面字符精准匹配n次

  \{,m\}  前面字符最多m次

  \{n,m\}  前面字符n到m次

  \{n,\}  前面字符至少n次

位置锚定

    ^ 行首锚定,用于模式的最左侧

    $  行尾锚定,用于模式的最右侧

    ^pattern$ 匹配模式的整行

    ^$ 空行

    ^[[:space:]]*$ 空白行

    \< 或\b 词首锚定  词首是roo \<roo 或 \broo

    \> 或\b 词尾锚定

    \<pattern\> 匹配整个单词

    [root@centos7-test test]# echo  rooottt  |grep  '\<rooottt\>' 锚定单词

      rooottt

      [root@centos7-test test]# echo  rooottt  |grep  '\<rooo'  锚定词首

      rooottt

 

 

分组其他

      将多个字符捆在一起,作为一个整体

      [root@centos7-test test]# echo aoaoaoao  |grep  '\(ao\)\{4\}'

        aoaoaoao

        \(str\)  \1

        \|或者

          a\|b

扩展正则表达式

  把基本正则表达式中的"\"删除,词首词尾单词锚定除外

  命令

    grep  -E  或 egrep 

grep

  grep [option]  pattern  file ...

  [option]

        -i 忽略大小写

        -n 显示行号

        -q str  查找str字符串,静默模式,不输出,找到则echo $?结果为0

        -o  仅显示匹配到的字符串

        -A # 后#行

        -C  # 前#行

        -B  前后各 #行

        -v  取反

        -E 使用扩展的正则表达式,相当于egrep

        -w  匹配完整单词

        -r  递归目录,不处理软链接

        -R  递归目录,处理软链接

        -e  pattern1  -e pattern2,....

                    [root@centos7-test test]# grep  -e  root  -e  bash /etc/passwd

                    root:x:0:0:root:/root:/bin/bash

                    operator:x:11:0:operator:/root:/sbin/nologin

        -f file  以一个文件作为模式,处理另一个文件,  grep -f  file  file2  ....

         

获取ip地址

[root@centos7-test test]# cat ippatt.txt

([0-9]{1,3}\.){3}[0-9]{1,3}

[root@centos7-test test]# ifconfig |egrep  -of  ippatt.txt

192.168.2.4

255.255.255.0

192.168.2.255

192.168.186.142

255.255.255.0

192.168.186.255

127.0.0.1

255.0.0.0

如下为何出不来

[root@centos7-test test]# echo  33.44.55.76.790 |grep [0-9]\?

[root@centos7-test test]# echo  33.44.55.76.790 |grep [0-9]\+

相关文章

网友评论

      本文标题:linux文本处理工具

      本文链接:https://www.haomeiwen.com/subject/dgjdsltx.html