zoukankan      html  css  js  c++  java
  • Linux文本处理工具

    grep

    正则表达式

    扩展的正则表达式

    VIM

    抽取文本的工具

    1.文件内容:less和cat

    cat [OPTION]... [FILE]...

    -n, --number
                  number all output lines

    -E, --show-ends
                  display $ at end of each line

     -A, --show-all
                  equivalent to -vET

    more命令:分页查看文件

    less命令:一页一页的查看文件

    2.文件截取:head和tail

    head - output the first part of files

    -n, --lines=[-]K
                  print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file

    tail [OPTION]... [FILE]...

    -f, --follow[={name|descriptor}]

                  output appended data as the file grows;
                  an absent option argument means 'descriptor'

    3.按列抽取:cut

     cut OPTION... [FILE]...

    -d, --delimiter=DELIM
                  use DELIM instead of TAB for field delimite
    -f, --fields=LIST
                  select only these fields;  also print any line that contains no delimiter character, unless the -s option is speci‐
                  fied
    #:第#个字段
    #,#:离散的多个字段
    #-#:连续的多个字段,例如1-6
    混合使用1-3,7
    cut -d: -f1,3 /etc/passwd|sort -t: -k2 -nr|head -10

    剪切passwd中的第一个和第三个字段,并按照第三个字段逆序排序

    4.按关键字抽取:grep

    grep, egrep, fgrep - print lines matching a pattern

    grep [OPTIONS] PATTERN [FILE...]
    grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]

    -v, --invert-match
                  Invert the sense of matching, to select non-matching lines.  (-v is specified by POSIX.)

    -i, --ignore-case
                  Ignore case distinctions in both the PATTERN and the input files.  (-i is specified by POSIX.)

    -o, --only-matching
                  Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

     -A NUM, --after-context=NUM
                  Print NUM lines of trailing context after matching lines.  Places a line containing a  group  separator  (described
                  under  --group-separator) between contiguous groups of matches.  With the -o or --only-matching option, this has no
                  effect and a warning is given.
    -B NUM, --before-context=NUM
                  Print NUM lines of leading context before matching lines.  Places a line containing a  group  separator  (described
                  under  --group-separator) between contiguous groups of matches.  With the -o or --only-matching option, this has no
                  effect and a warning is given.
    -C NUM, -NUM, --context=NUM
                  Print NUM lines of output context.  Places a line containing a group separator (described under  --group-separator)
                  between  contiguous  groups of matches.  With the -o or --only-matching option, this has no effect and a warning is
                  given.

     正则表达式(REGEXP):由一类特殊字符及文本字符所编写的模式,其中有些字符(元字符)不表示字符字面意义,而表示控制或统配的功能

    程序支持:grep、sed、awk、vim、less、nginx、varnish

    分两类:

      基本正则表达式:BRE

      扩展正则表达式:ERE

    元字符分类:字符匹配、匹配次数、位置锚定、分组及引用

    man 7 regex

    1.字符匹配

    .:匹配任意单个字符

    .*:匹配任意长度的任意字符

    []:匹配指定范围的任意单个字符

    [^]:匹配指定范围外的任意单个字符

    [:alnum:]:字母和数字

    [:alpha:]:代表任何英文大小写字符

    [:lower:]:小写字母

    [:upper:]:大写字母

    [:blank:]:空白字符

    [:space:]:水平和垂直的空白字符

    [:digit:]:十进制数字

    [:punct:]:标点符号

    2.匹配次数:用在要指定次数的字符后面,用于指定前面的字符要出现的次数

    *:匹配前面的字符任意次,包括0次

        贪婪模式:尽可能长的匹配

    ?:匹配其前面的字符0次或1次

    +:匹配其前面的字符至少1次

    {n}:匹配前面的字符n次

    {m,n}:匹配前面的字符至少mci,最多n次

    {,n}:匹配其前面额字符最多n次

    {n,}:匹配其前面的字符至少n次

     [root@localhost ~]# cat > google.txt<<EOF
    > google
    > gooooooooooooooooooogle
    > gogle
    > ggle
    > gooooooooo000000000gle
    > EOF

    grep "go.gle" google.txt

    google

    grep "go.*gle" google.txt

    google
    gooooooooooooooooooogle
    gogle
    gooooooooo000000000gle

    grep "go*gle" google.txt

    google
    gooooooooooooooooooogle
    gogle
    ggle

    grep "go?gle" google.txt

    gogle
    ggle

    grep "go+gle" google.txt

    google
    gooooooooooooooooooogle
    gogle

    grep "go{0}gle" google.txt

    ggle

    3.位置锚定

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

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

    ^PATTERN$:用于匹配整行

      ^$ 空行

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

    过滤空行

    grep -v ^[[:space:]*]$

    <或 词首锚定,用于单词模式的左侧

    >或 词尾锚定,用于单词模式的右侧

    <PATTERN> 匹配整个单词

    4.分组及引用

    分组:()将一个或多个字符捆绑在一起,当作一个整体进行处理,如:(root)+

    分组括号中的模式匹配到的内容会被正则表达式引擎记录于内部的变量中,这些变量的命名方式为:1 ,2 ,3,......

    1表示从左侧第一个左括号以及与之匹配的右括号之间的模式所匹配到的字符

    示例:(string1+(string2)*)

       1:string1+(string2)*

       2:string2

    后向引用:引用前面的分组括号中的模式所匹配字符,而非模式本身

      He loves his lover.
           He likes his lover.
           She likes her liker.
           She loves her liker.

    grep -E "(l..e).*1" f1

    或:|

    示例:a|b:a或b  C|cat:C或者cat

    (C|c)at:Cat或cat

    扩展的正则表达式

    1.匹配次数

    *:匹配前面的字符任意次

    ?:匹配前面的字符0次或1次

    +:匹配前面的字符1次或多次

    {m}:匹配m次

    {m,n}:至少m次,最多n次

    2.位置锚定

    ^:行首

    $:行尾

    <、:词首

    >、:词尾

    3.分组

    ()

    后向引用:1  2  3

    或者:C|cat————C或cat

       (C|c)at——Cat或cat

    练习题

    1.显示/etc/meminfo文件中以大小s开头的行(要求:使用两种方法)

    2.显示/etc/passwd文件中不以/bin/bash结尾的行

    3.显示用户rpc默认的shell程序

    4.找出/etc/passwd中的两位或三位数

    5.显示Centos7的/etc/grub2.cfg文件中,至少以一个空白字符开头的且后面有非空白字符的行

    6.找出“netstat-tan”命令结果中以LISTEN后跟任意多个空白字符结尾的行

    7.显示Centos7上所有系统用户的用户名和UID

    8.添加用户bash、testbash、basher、sh、nologin(其shell为/sbin/nologin),找出/etc/psswd用户名和shell同名的行

    9.利用df和grep,取出磁盘各分区利用率,并从大到小排序

    分析文本的工具

    1.文本数据统计:wc

    wc - print newline, word, and byte counts for each file

     wc [OPTION]... [FILE]...

    -c, --bytes
                  print the byte counts

    -m, --chars
                  print the character counts
    -l, --lines
                  print the newline counts
     -w, --words
                  print the word counts

    2.整理文本:sort

    sort - sort lines of text files

     sort [OPTION]... [FILE]...

    -n, --numeric-sort
                  compare according to string numerical value

    -r, --reverse
                  reverse the result of comparisons

    -t, --field-separator=SEP
                  use SEP instead of non-blank to blank transition

    -k, --key=KEYDEF
                  sort via a key; KEYDEF gives location and type

    uniq - report or omit repeated lines

     uniq [OPTION]... [INPUT [OUTPUT]]

    -c, --count
                  prefix lines by the number of occurrences
    -d, --repeated
                  only print duplicate lines, one for each group
    -u, --unique
                  only print unique lines
    常与sort结合使用
    sort userlist.txt | uniq -c

    3.比较文件:diff和patch 

    diff - compare files line by line

    diff [OPTION]... FILES

    -u, -U NUM, --unified[=NUM]
                  output NUM (default 3) lines of unified context

    diff -u hjm.conf hjm2.conf > foo.patch

    patch -b foo.conf foo.patch

     VIM

    模式转换

    1.命令模式--------->插入模式

    i:insert,在光标所在处输入

    I:在当前光标所在行的行首输入

    a:append,在光标所在处的后面输入

    A:在当前光标所在处的行尾输入

    o:在当前光标所在行的下方打开一个新行

    O:在当前光标所在处的上方打开一个新行

    插入模式-------------->命令模式

        ESC

    命令模式--------------->扩展命令模式

        :

    扩展命令模式----------->命令模式

        ESC

    2.关闭文件

    :q:退出

    :q!:强制退出,丢弃做出的修改

    :wq:保存退出

    扩展模式

    按“:”进入扩展模式

    命令:

      :w  写(存入)磁盘文件

      :q  退出

      :r filename  读文件内容到当前文件中

      :w filename  将文件内容写入到另一个文件

      :!command 执行命令

      :r!command 读入命令的输出

    命令模式光标跳转

    单词间跳转:

    w:下一个单词的词首

    e:当前或下一个单词的词尾

    b:当前或钱一个单词的词首

    #COMMAND:由#指定一次跳转的单词数

    行首行尾跳转:

    ^:跳转至行首的第一个非空白字符

    0:跳转至行首

    $:跳转至行尾

    行间移动:

    #G:跳转至由#指定行

    G:最后一行

    gg:第一行

    命令模式操作:

    字符编辑:

    x:删除光标处的字符

    #x:删除光标处起的#个字符

    p:粘贴到光标所在处的后面

    xp:交换光标所在处的字符及其后面字符的位置

    ~:转换大小写

    J:删除当前行的后的换行符

    替换命令(r,replace)

    r:替换光标所在处的字符

    R:切换成REPLACE模式(替换模式)

    删除命令:

      d:删除命令,可结合光标跳转字符,实现范围删除

      d$     d^   d0   dw    de   db  dG  dgg

      #COMMAND

    dd:删除光标所在的行

      #dd:多行删除

    复制命令(y,yank):

      y:复制,行为类似于d命令

      yy:复制行

      #yy:复制多行

    粘贴命令(p,paste)

    p:缓冲区存的如果为整行,则粘贴当前光标所在行的下方;否则,粘贴至当前光标所在处的后面

    P:缓冲区存的如果为整行,则粘贴在当前光标所在行的上方;否则,粘贴至当前光标所在处的前面

    改变命令(c,change)

      c:修改后切换到插入模式

    命令模式-->插入模式

      行为于d命令相似 c$  c^等

    cc:删除当前行并输入新内容

     扩展命令模式

    扩展命令模式:查找

    /PATTERN:从当前光标所在处向文件尾部查找

    ?PATTERN:从当前光标所在处向文件首部查找

    n:与命令同方向

    N:与命令反方向

    扩展命令模式:地址定界

    :start_pos,end_pos

    #:具体第#行

    #,#:从第几行到第几行

    %:表示全文   %s#/etc#/var#g  全文都替换

    扩展命令模式:查找并替换

    s:在扩展模式下完成查找替换操作

      格式:s/要查找的内容/替换为的内容/修饰符

      要查找的内容:可使用PATTERN

      替换为的内容:不能使用PATTERN,但可以使用1  2等后向引用符号

      修饰符:

      i:忽略大小写

      g:全局替换;默认情况下,每一行只替换第一次出现

      gc:全局替换,每次替换前询问

    查找替换中的分隔符/可替换为其它字符,如:

      s@/etc@/var@g

      s#/etc/#/var#g

  • 相关阅读:
    [luoguP2221] [HAOI2012]高速公路(线段树)
    SICP:对数步数内迭代计算幂的函数
    python__tkinter之listbox&button
    C陷阱与缺陷 之 各种知识技巧
    ACM && Find Minimum in Rotated Sorted Array
    windows编程一些小知识
    Linux_C pthread 关于多线程一个简单的程序
    ACM&贪心算法
    Linux_C socket 数据报之client, server.c
    Linux_C socket 数据报之一些辅助函数
  • 原文地址:https://www.cnblogs.com/hjmLinux/p/11715577.html
Copyright © 2011-2022 走看看