• Linux:文本处理


    常用命令

    tr命令

    删除一段文本信息中的某些文字。或者将其进行转换。

    用法:tr [选项]... SET1 [SET2]
    选项:
      -c, -C, --complement    use the complement of SET1
      -d, --delete            delete characters in SET1, do not translate
      -s, --squeeze-repeats   replace each sequence of a repeated character
                                that is listed in the last specified SET,
                                with a single occurrence of that character
      -t, --truncate-set1     first truncate SET1 to length of SET2
      
      SET 是一组字符串,一般都可按照字面含义理解。解析序列如下:
      \NNN	八进制值为NNN 的字符(1 至3 个数位)
      \\		反斜杠
      \a		终端鸣响
      \b		退格
      \f		换页
      \n		换行
      \r		回车
      \t		水平制表符
      \v		垂直制表符
      字符1-字符2	从字符1 到字符2 的升序递增过程中经历的所有字符
      [字符*]	在SET2 中适用,指定字符会被连续复制直到吻合设置1 的长度
      [字符*次数]	对字符执行指定次数的复制,若次数以 0 开头则被视为八进制数
      [:alnum:]	所有的字母和数字
      [:alpha:]	所有的字母
      [:blank:]	所有呈水平排列的空白字符
      [:cntrl:]	所有的控制字符
      [:digit:]	所有的数字
      [:graph:]	所有的可打印字符,不包括空格
      [:lower:]	所有的小写字母
      [:print:]	所有的可打印字符,包括空格
      [:punct:]	所有的标点字符
      [:space:]	所有呈水平或垂直排列的空白字符
      [:upper:]	所有的大写字母
      [:xdigit:]	所有的十六进制数
      [=字符=]	所有和指定字符相等的字符
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    删除 “hello shiyanlou” 中所有的’o’,‘l’,‘h’

    echo "hello shiyanlou" | tr -d "olh"
    
    • 1

    将"hello" 中的ll,去重为一个l

    echo 'hello' | tr -s 'l'
    
    • 1

    将输入文本,全部转换为大写或小写输出

    echo 'input some text here' | tr '[:lower:]' '[:upper:]'
    
    • 1

    col命令

    将Tab换成对等数量的空格键,或反转这个操作。

    用法: col [-bfhpx] [-l nline]
    
    -x	将Tab转换为空格
    -h	将空格转换为Tab(默认选项)
    
    • 1
    • 2
    • 3
    • 4

    使用 col -x 将 /etc/protocols 中的 Tab 转换为空格,然后再使用 cat 查看

    cat /etc/protocols | col -x | cat -A
    
    • 1

    join命令

    将两个文件中包含相同内容的那一行合并在一起

    用法:join [选项]... 文件1 文件2
    选项:
      -a FILENUM        also print unpairable lines from file FILENUM, where
                          FILENUM is 1 or 2, corresponding to FILE1 or FILE2
      -e EMPTY          replace missing input fields with EMPTY
      -i, --ignore-case  ignore differences in case when comparing fields
      -j FIELD          equivalent to '-1 FIELD -2 FIELD'
      -o FORMAT         obey FORMAT while constructing output line
      -t CHAR           use CHAR as input and output field separator
      -v 文件编号        	类似 -a 文件编号,但禁止组合输出行
      -1 域          	在文件1 的此域组合
      -2 域          	在文件2 的此域组合
      --check-order     	检查输入行是否正确排序,即使所有输入行均是成对的
      --nocheck-order   	不检查输入是否正确排序
      --header          	将首行视作域的头部,直接输出而不对其进行匹配
      -z, --zero-terminated     line delimiter is NUL, not newline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    将file1和file2两个文件内容合并在一起,输出"1 hello shiyanlou"

    echo '1 hello' > file1
    echo '1 shiyanlou' > file2
    join file1 file2
    
    • 1
    • 2
    • 3

    将file1和file2两个文件内容合并在一起,以"1"为分隔符,输出"1 hello1 shiyanlou"

    join -t "1" file1 file2 
    
    • 1

    paste命令

    在不对比数据的情况下,简单地将多个文件合并一起,以Tab隔开。与join命令类似。

    用法:paste [选项]... [文件]...
    如果没有指定文件,或者文件为"-",则从标准输入读取。
    
    必选参数对长短选项同时适用。
      -d, --delimiters=列表	改用指定列表里的字符替代制表分隔符
      -s, --serial		不使用平行的行目输出模式,而是每个文件占用一行
      -z, --zero-terminated    line delimiter is NUL, not newline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    合并文件内容并指定合并的分隔符为“:”,默认为Tab

    paste -d ':' file1 file2
    
    • 1

    不合并到一行,每个文件为一行

    paste -s file1 file2
    
    • 1
  • 相关阅读:
    力扣376. 摆动序列错误用例
    探究Presto SQL引擎(3)-代码生成
    torch - FloatTensor标签(boolean)数值转换(1/0)
    opencv的MinGW-W64编译
    『牛客|每日一题』走迷宫
    Python UI自动化 —— pytest常用运行参数解析、pytest执行顺序解析
    如何使用DBeaver连接Hive
    springboot+vue+elementui旅游景点门票预订网站java
    【Linux】Linux远程访问Windows下的MySQL数据库
    Cortex-M3/M4之SVC和PendSV异常
  • 原文地址:https://blog.csdn.net/weixin_49346755/article/details/125561200