Linux常用文本处理命令

文本查看

cat

cat 是一个文本文件(查看)和(连接)工具,通常与more搭配使用,与more不同的是cat可以合并文件。查看一个文件的内容,用cat比较简单,就是cat后面直接接文件名。

语法

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

选项

  • -A, --show-all 等价于 -vET
  • -b, --number-nonblank 对非空行输出行编号
  • -e 等价于 -vE
  • -E, --show-ends 在每行结束出显示$
  • -n, --number 对输出的所有行编号
  • -s, --squeeze-blank 不输出多行空行
  • -t 等价于 -vT
  • -T, --show-tabs 将TAB显示为 ^I
  • -u (ignored)
  • -v, --show-nonprinting 使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

例子

  1. 使用cat查看/etc/fstab,并显示非空行的行号

    [root@localhost ~]# cat -b /etc/fstab   
    
         1  #
         2  # /etc/fstab
         3  # Created by anaconda on Wed Jan 15 12:31:47 2020
         4  #
         5  # Accessible filesystems, by reference, are maintained under '/dev/disk'
         6  # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
         7  #
         8  UUID=edcd7459-580f-4622-b6cc-7e8c0893d59f /                       ext4    defaults        1 1
         9  UUID=113bc62d-f6b2-4671-a2cb-e04c4ed04cf6 /boot                   ext4    defaults        1 2
        10  UUID=69b69599-6670-409d-8761-991d44ec3d40 swap                    swap    defaults        0 0
        11  /dev/cdrom /media/cd    iso9660 auto,ro         0 0
    
  2. 用cat在/tmp目录创建test文件,以hhh作为结束符

    [root@localhost tmp]# cat>test<<hhh
    > 123
    > test
    > hh
    > hhh
    [root@localhost tmp]# cat test 
    123
    test
    hh
    
  3. 用cat向test追加内容,以gg作为结束符

    [root@localhost tmp]# cat>>test<<gg
    > kkkk
    > gg
    [root@localhost tmp]# cat test 
    123
    test
    hh
    kkkk
    
  4. 使用cat连接多个文件

    [root@localhost tmp]# cat>tes1<<jkl
    > 123
    > jkl
    [root@localhost tmp]# cat>test2<<jkl 
    > 456
    > jkl
    [root@localhost tmp]# cat tes1 test2 > test3
    [root@localhost tmp]# cat test3
    123
    456
    

more

more 是我们最常用的工具之一,最常用的就是显示输出的内容,然后根据窗口的大小进行分页显示,然后还能提示文件的百分比;

语法

more [options] file [...]

选项

  • -number 定义屏幕大小,为number行;
  • -d 提示Press space to continue, ‘q’ to quit.(按空格键继续,按q键退出),当非法的按键被按下时,不会响铃;
  • -l 忽略Ctrl+l (换页)字符;
  • -f 计算行数时,以实际上的行数,而非自动换行过后的行数(有些单行字数太长的会被扩展为两行或两行以上)
  • -p 通过清除窗口而不是滚屏来对文件进行换页。和-c参数有点相似;
  • -c 从顶部清屏然后显示;
  • -s 把连续的多个空行显示为一行;
  • -u 把文件内容中的下划线去掉
  • +/pattern 从pattern 前两行开始显示;
  • +number 从第number行开始显示;

常用动作指令

我们查看一个内容较大的文件时,要用到more的动作指令,比如ctrl+f(或空格键) 是向下显示一屏,ctrl+b是返回上一屏; Enter键可以向下滚动显示n行,默认为1行;

下面为几个常用的动作指令;

  • Enter 向下n行,需要定义,默认为1行;
  • Ctrl+f 向下滚动一屏;
  • 空格键 向下滚动一屏;
  • Ctrl+b 返回上一屏;
  • = 输出当前行的行号;
  • :f 输出文件名和当前行的行号;
  • v 调用vi编辑器;
  • ! 命令 调用Shell,并执行命令;
  • q 退出more当我们查看某一文件时,想调用vi来编辑它,不要忘记了v动作指令,这是比较方便的;

例子

使用more显示/etc/passwd文件,屏幕大小为1,从第2行开始,显示提示

[root@localhost tmp]# more -1 +2 -d /etc/passwd  
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
--More--(9%)[Press space to continue, 'q' to quit.]

less

less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大;您是初学者,我建议您用less。由于less的内容太多,我们把最常用的介绍一下;

语法格式

less [options] file
常用选项

  • -c 从顶部(从上到下)刷新屏幕,并显示文件内容。而不是通过底部滚动完成刷新;
  • -f 强制打开文件,二进制文件显示时,不提示警告;-i 搜索时忽略大小写;除非搜索串中包含大写字母;
  • -I 搜索时忽略大小写,除非搜索串中包含小写字母;
  • -m 显示读取文件的百分比;
  • -M 显法读取文件的百分比、行号及总行数;
  • -N 在每行前输出行号;
  • -p pattern 搜索pattern;比如在/etc/profile搜索单词MAIL,就用 less -p MAIL /etc/profile
  • -s 把连续多个空白行作为一个空白行显示;
  • -Q 在终端下不响铃;

常用的动作命令

进入less后,我们得学几个动作,这样更方便 我们查阅文件内容;最应该记住的命令就是q,这个能让less终止查看文件退出;

  • Enter 向下移动一行;
  • y 向上移动一行;
  • 空格键 向下滚动一屏;
  • b 向上滚动一屏;
  • d 向下滚动半屏;
  • h less的帮助;
  • u 向上洋动半屏;
  • w 可以指定显示哪行开始显示,是从指定数字的下一行显示;比如指定的是6,那就从第7行显示;
  • g 跳到第一行;
  • G 跳到最后一行;
  • p n% 跳到n%,比如 10%,也就是说比整个文件内容的10%处开始显示;
  • /pattern 搜索pattern ,比如 /MAIL表示在文件中搜索MAIL单词;
  • v 调用vi编辑器;
  • q 退出less
  • !command 调用SHELL,可以运行命令;比如!ls 显示当前列当前目录下的所有文件;

就less的动作来说,内容太多了,用的时候查一查man less是最好的。

head

head 将每个文件的前10行打印到标准输出。如果有多个文件,则在每个文件前面加上给出文件名的头。如果没有文件,或者文件为-,则读取标准输入。

语法

head [OPTION]... [FILE]...

选项

  • -c, --bytes=[-]K 打印每个文件的前K个字节;以“-”开头,打印每个文件的最后K个字节以外的所有字节
  • -n, --lines=[-]K 打印前K行而不是前10行;以“-”打头,列出除最后K行以外的所有内容
  • -q, --quiet, --silent 从不打印文件名
  • -v, --verbose 总是打印文件名

例子

比如我们显示/etc/profile的前10行内容,应该是:
[root@localhost ~]# head -n 10 /etc/profile

tail

tail将每个文件的最后10行打印到标准输出。如果有多个文件,则在每个文件之前加上给出该文件的头

的名字。如果没有文件,或者文件为-,则读取标准输入。

语法

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

选项

  • -c, --bytes=K 输出最后K个字节;或者使用-c +K以每个文件的第K位开始输出字节
  • -f, --follow[={name|descriptor}] 该参数用于监视File文件增长;缺少选项参数意味着“描述符”
  • -F 使用–follow=name,重新打开一个还没有打开的文件,在N(默认5)次迭代后更改大小,以查看是否已取消链接或重命名(这是通常的情况旋转日志文件);对于inotify,这个选项很少有用
  • -n, --lines=K 输出最后的K行,而不是最后的10行;或者使用-n +K从第K行开始输出
  • –pid=PID 与-f合用,表示在进程ID,PID死掉之后结束
  • -s, --sleep-interval=N 与-f合用,表示在每次反复的间隔休眠S秒
  • -v, --verbose 总是打印文件名

例子

显示文件 syslog 的后十行内容并在文件内容增加后,且自动显示新增的文件内容。

tail -f /var/log/syslog

vi/vim/nano

使用vi/vim/nano这些文本编辑器也能实现文本查看功能

文本搜索

find

在目录层次结构中搜索文件

语法

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

-H、-L和-P:用于决定find是如何对待字符链接文件。默认find采取-P选项,不追踪字符链接文件。

-D debugoptions:这个是find的调试模式,当我们执行find后的命令输出,与我们所期望的不同时,使用该选项。

-Olevel:启用查询优化(query optimization)。

上述三种选项,新手都可以忽略,保持其默认即可。简化后的结果为。

find [path...] [expression]

path:表示find查找文件的搜索目录。find只会在给出的目录下查找。可以有多个。

expression:表达式

常用expression

  • -name “PATTERN” 根据文件名来查找文件,pattern支持globbing字符。

    [root@localhost ~]# find /etc -name *tab
    /etc/statetab
    /etc/fstab
    /etc/selinux/targeted/active/modules/100/updfstab
    /etc/inittab
    /etc/rwtab
    /etc/crontab
    /etc/anacrontab
    /etc/mtab
    /etc/crypttab
    [root@localhost ~]# 
    
  • -iname “PATTERN”:类似-name,区别在于该选项是忽略字母大小写。

  • -size SIZE 根据文件的大小查找

    #查找/etc下大于1M的文件
    [root@localhost ~]# find /etc -size +1M
    /etc/brltty/zh-tw.ctb
    /etc/selinux/targeted/active/policy.linked
    /etc/selinux/targeted/active/policy.kern
    /etc/selinux/targeted/contexts/files/file_contexts.bin
    /etc/selinux/targeted/policy/policy.31
    /etc/udev/hwdb.bin
    [root@localhost ~]# 
    
  • -regex PATTERN 正则表达式匹配文件

    [root@localhost ~]# find /etc -regex ".*tab" 
    /etc/statetab
    /etc/fstab
    /etc/selinux/targeted/active/modules/100/updfstab
    /etc/inittab
    /etc/rwtab
    /etc/crontab
    /etc/anacrontab
    /etc/mtab
    /etc/crypttab
    [root@localhost ~]# 
    
  • -print0 文件名输出到一行中

  • -exec 对找到的文件调用其他命令处理,它的终止是以“;”为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

    #对搜索到的文件进行ls -l
    [root@localhost ~]# find /etc -name "*tab" -exec ls -l {} \;
    -rw-r--r--. 1 root root 212 Aug  4  2017 /etc/statetab
    -rw-r--r--. 1 root root 543 Feb 13 16:20 /etc/fstab
    total 20
    -rw-------. 1 root root  2142 Feb  6 17:41 cil
    -rw-------. 1 root root 10360 Feb  6 17:41 hll
    -rw-------. 1 root root     2 Feb  6 17:41 lang_ext
    -rw-r--r--. 1 root root 511 Aug  4  2017 /etc/inittab
    -rw-r--r--. 1 root root 966 Aug  4  2017 /etc/rwtab
    -rw-r--r--. 1 root root 451 Jun 10  2014 /etc/crontab
    -rw-------. 1 root root 541 Aug  3  2017 /etc/anacrontab
    lrwxrwxrwx. 1 root root 17 Jan 15 12:31 /etc/mtab -> /proc/self/mounts
    -rw-------. 1 root root 0 Jan 15 12:31 /etc/crypttab
    
  • -type 根据文件的类型查找

    [root@localhost ~]# find /etc/sysconfig/ -type l     
    /etc/sysconfig/selinux
    /etc/sysconfig/network-scripts/ifdown
    /etc/sysconfig/network-scripts/ifdown-isdn
    /etc/sysconfig/network-scripts/ifup-isdn
    /etc/sysconfig/network-scripts/ifup
    /etc/sysconfig/grub
    
  • -user NAME:根据文件的所有者查找,可以是username,也可以是UID。

    [root@localhost ~]# find /home -user alice
    /home/alice
    /home/alice/.mozilla
    /home/alice/.mozilla/plugins
    /home/alice/.mozilla/extensions
    /home/alice/.bashrc
    /home/alice/.bash_logout
    /home/alice/.bash_profile
    [root@localhost ~]# 
    
  • -group NAME:根据文件的所有组查找,可以是groupname,也可以是GID。

    [root@localhost ~]# find /home -group bobby
    /home/bobby
    /home/bobby/.mozilla
    /home/bobby/.mozilla/plugins
    /home/bobby/.mozilla/extensions
    /home/bobby/.bashrc
    /home/bobby/.bash_logout
    /home/bobby/.bash_profile
    [root@localhost ~]# 
    

locate

locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案。其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了。在一般的 distribution 之中,数据库的建立都被放在 crontab 中自动执行。

语法

locate [OPTION]... PATTERN...

常用选项

  • -A, --all 只打印匹配所有模式的条目,而不是只需要其中一个模式匹配。
  • -b, --basename 只匹配指定模式的基名称。
  • -c, --count 不要在标准输出中写入文件名,只写入匹配项的数量。
  • -d, --database DBPATH 指定特定数据库,缺省时使用/var/lib/mlocate/mlocate.db文件,在特定需求下可以通过-d执行搜索对象数据库。
  • -e, --existing 显示当前存在的文件或者目录(在没有使用updatedb进行信息同步更新时,可能会搜索出已经删除的文件或者临时文件)
  • -i, --ignore-case 不区分大小写
  • -l, --limit, -n LIMIT 仅输出几行的意思,例如输出5行则是-l 5;
  • -S, --statistics 将每个读数据库的统计信息写入标准输出,而不是搜索文件并成功退出。
  • -q, --quiet 不要写关于读取和处理数据库时遇到的错误的消息
  • -r, --regexp REGEXP 根据基本的正则表达式寻找
  • –regex 根据扩展的正则表达式寻找

例子:

  1. 查找匹配net的文件,显示4行

    [root@localhost ~]# locate -l 4 net
    /boot/grub2/i386-pc/net.mod
    /data/centos7/Packages/dracut-network-033-502.el7.x86_64.rpm
    /data/centos7/Packages/glib-networking-2.50.0-1.el7.x86_64.rpm
    /data/centos7/Packages/libnetfilter_conntrack-1.0.6-1.el7_3.x86_64.rpm
    
  2. 将每个读数据库的统计信息写入标准输出

    [root@localhost ~]# locate -S
    Database /var/lib/mlocate/mlocate.db:
            10,388 directories
            141,958 files
            6,898,873 bytes in file names
            3,172,981 bytes used to store database
    
  3. 刚创建的文件在更新数据库之前查找,locate会提示找不到,这时可以使用updatedb更新数据库

    [root@localhost ~]# touch swm
    [root@localhost ~]# locate swm
    /usr/share/doc/xorg-x11-proto-devel-2018.4/COPYING-windowswmproto
    [root@localhost ~]# updatedb
    [root@localhost ~]# locate swm
    /root/swm
    /usr/share/doc/xorg-x11-proto-devel-2018.4/COPYING-windowswmproto
    
    

文本处理

grep

grep的作用是按行查找字符,输出包含字符的行。

语法

grep [options] 'PATTERN' FILE

常用选项

  • –color=auto 高亮显示匹配到内容
  • -E, --extended-regexp 支持扩展正则表达式
  • -P, --perl-regexp 支持PERL正则表达式
  • -f FILE, --file=FILE 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容 (-f is specified by POSIX.)。
  • -n, --line-number 显示行号
  • -i, --ignore-case 忽略大小写
  • -c, --count #计算符合样式的列数。
  • -o, --only-matching 只输出匹配到内容
  • -v, --invert-match 反向匹配,显示不包含匹配文本的所有行。
  • -A NUM, --after-context=NUM after,匹配到的行的后NUM行
  • -B NUM, --before-context=NUM before,匹配到的行的前NUM行
  • -C NUM, -NUM, --context=NUM both,匹配到的行的前后NUM行

例子

  1. 查找指定进程(第3条结果是grep进程本身,并非真正要找的进程。)

    [root@localhost ~]# ps -ef | grep "sshd"
    root       1075      1  0 10:49 ?        00:00:00 /usr/sbin/sshd -D
    root       1510   1075  0 10:51 ?        00:00:00 sshd: root@pts/0
    root       4885   1522  0 16:46 pts/0    00:00:00 grep --color=auto sshd
    
  2. 从文件中读取关键词进行搜索 且显示行号

    [root@localhost tmp]# cat test1
    da.cnblogs.com
    ubuntu
    ubuntu linux
    redhat
    Redhat
    linuxmint
    [root@localhost tmp]# cat test2
    linux
    Redhat
    [root@localhost tmp]# cat test1 | grep -nf test2
    3:ubuntu linux
    5:Redhat
    6:linuxmint
    [root@localhost tmp]# 
    
  3. 从多个文件中查找关键词

    [root@localhost tmp]# grep "linux" test1 test2
    test1:ubuntu linux
    test1:linuxmint
    test2:linux
    [root@localhost tmp]# 
    

cut

从文件的每一行截取一部分内容

语法

cut OPTION... [FILE]...

选项

  • -b, --bytes=LIST 以字节为单位进行分割。
  • -c, --characters=LIST 以字符为单位进行分割
  • -d 指定字段的分隔符,默认的字段分隔符为“TAB”;
  • -f 与-d一起使用,指定显示哪个区域。
  • -n 与“-b”选项连用,不分割多字节字符;
  • –complement 补足被选择的字节、字符或字段;
  • –out-delimiter=<字段分隔符> 使用字符串作为输出分隔符
  • –help 显示指令的帮助信息;
  • –version 显示指令的版本信息。

例子

  1. 使用 : 作为分隔符对/etc/passwd进行切分,并输出字段1

    [root@localhost tmp]# cut -d: -f1 /etc/passwd
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    ftp
    nobody
    systemd-network
    dbus
    polkitd
    postfix
    sshd
    ...
    
  2. 在1的基础上再输出字段2,并以%作为输出分隔符

    cut -d: -f1,2 --output-delimiter='%' /etc/passwd
    root%x
    bin%x
    daemon%x
    adm%x
    lp%x
    sync%x
    shutdown%x
    halt%x
    mail%x
    operator%x
    games%x
    ftp%x
    nobody%x
    systemd-network%x
    dbus%x
    polkitd%x
    postfix%x
    sshd%x
    

sort

将文本文件内容加以排序,sort可针对文本文件的内容,以行为单位来排序。

语法

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

sort [OPTION]... --files0-from=F

–files0-from=F
从文件F中以null结尾的名称指定的文件中读取输入;如果F是-则从标准输入中读取名称

常用选项

-n, --numeric-sort 以整数类型比较字段

-r, --reverse 倒置排序的顺序为 由大至小(descending),而非默认的由小至大(ascending)

-t, --field-separator=SEP 使用单个字符SEP作为字段分割字符,取代默认的空白字符。

-k, --key=KEYDEF 通过一个键排序;KEYDEF给出位置和类型

-u, --unique 只有唯一的记录,丢弃所有具有相同键值的记录,只留其中的第一条。

例子

以:为分隔符,通过第3个字段以整数类型进行排序

[root@localhost tmp]# sort -t: -k3 -n /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
...

uniq

用于报告或忽略文件中的重复行,一般与sort命令结合使用。

语法

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

选项

  • -c, --count 在每行前加上表示相应行目出现次数的前缀编号
  • -d, --repeated 只输出重复的行
  • -D, --all-repeated[=METHOD] 显示所有重复的行
    METHOD{none(default),prepend,separate}
  • -f, --skip-fields=N 比较时跳过前N 列
  • -i, --ignore-case 在比较的时候不区分大小写
  • -s, --skip-chars=N 比较时跳过前N 个字符
  • -u, --unique 只显示唯一的行
  • -z, --zero-terminated 使用’\0’作为行结束符,而不是新换行
  • -w, --check-chars=N 对每行第N 个字符以后的内容不作对照
  • –help 显示此帮助信息并退出
  • –version 显示版本信息并退出

例子

统计/etc/passwd各种shell的数量

[root@localhost tmp]# cut -d: -f7 /etc/passwd | sort | uniq -c  
      6 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     44 /sbin/nologin
      1 /sbin/shutdown

wc

word counting,单词,字符数,字节数,行数统计

语法

wc [OPTION]... [FILE]...
wc [OPTION]... --files0-from=F

选项

  • -c, --bytes 统计字节数
  • -m, --chars 统计字符数
  • -l, --lines 统计行数
  • –files0-from=F 从文件F中以null结尾的名称指定的文件中读取输入;如果F是-则从标准输入中读取名称
  • -L, --max-line-length 打印最长行的长度
  • -w, --words 打印单词数,一个单词被定义为由空白、跳格或换行字符分隔的字符串
  • –help 显示此帮助信息并退出
  • –version 显示版本信息并退出

例子

统计进程数

[root@localhost tmp]# ps aux | wc -l
102

tee

tee命令读取标准输入,把这些内容同时输出到标准输出和(多个)文件中,tee命令可以重定向标准输出到多个文件。要注意的是:在使用管道线时,前一个命令的标准错误输出不会被tee读取。

语法

tee [OPTION]... [FILE]...

选项

  • -a, --append 输出到标准输出的同时,追加到文件file中。如果文件不存在,则创建;如果已经存在,就在末尾追加内容,而不是覆盖。
  • -i, --ignore-interrupts 忽略中断信号。
  • –help 显示此帮助信息并退出
  • –version 显示版本信息并退

例子

tee与重定向的差别

[root@localhost tmp]# seq 5 > 1.txt 
[root@localhost tmp]# cat 1.txt 
1
2
3
4
5
[root@localhost tmp]# cat 1.txt > 2.txt
[root@localhost tmp]# cat 1.txt | tee 3.txt 
1
2
3
4
5
[root@localhost tmp]# cat 2.txt 
1
2
3
4
5
[root@localhost tmp]# cat 3.txt  
1
2
3
4
5

xargs

将上一个命令的输出每一行作为参数传递给xargs后面的命令,xargs后面的命令默认是echo。

语法

somecommand |xargs -item command

选项

  • -d 更改分隔符,默认情况下,xargs将换行符和空格作为分隔符,把标准输入分解成一个个命令行参数。

    [root@localhost tmp]# echo -n "one%two%three" | xargs -d% mkdir
    [root@localhost tmp]# ls
    one  three  two
    [root@localhost tmp]# 
    
  • -p 打印出要执行的命令,询问用户是否要执行。

    [root@localhost tmp]# echo -n "one two three" | xargs -p touch
    touch one two three ?...yes
    [root@localhost tmp]# ls
    one  three  two
    [root@localhost tmp]# 
    
  • -t 则是打印出最终要执行的命令,然后直接执行,不需要用户确认

    [root@localhost tmp]# echo -n "one two three" | xargs -t touch
    touch one two three 
    [root@localhost tmp]# ls
    one  three  two
    [root@localhost tmp]# 
    
  • -0 由于xargs默认将空格作为分隔符,所以不太适合处理文件名,因为文件名可能包含空格。

    find命令有一个特别的参数-print0,指定输出的文件列表以null分隔。然后,xargs命令的-0参数表示用null当作分隔符。

    [root@localhost tmp]# find . -print0  | xargs -0  ls -l
    -rw-r--r--. 1 root root    0 Feb 23 21:15 ./one
    -rw-r--r--. 1 root root    0 Feb 23 21:15 ./three
    -rw-r--r--. 1 root root    0 Feb 23 21:15 ./two
    
    .:
    total 0
    -rw-r--r--. 1 root root 0 Feb 23 21:15 one
    -rw-r--r--. 1 root root 0 Feb 23 21:15 three
    -rw-r--r--. 1 root root 0 Feb 23 21:15 two
    
    ./.font-unix:
    total 0
    
    ./.ICE-unix:
    total 0
    
    ./.Test-unix:
    total 0
    
    ./.X11-unix:
    total 0
    
    ./.XIM-unix:
    total 0
    [root@localhost tmp]# 
    
  • -L 如果标准输入包含多行,-L参数指定多少行作为一个命令行参数。

  • -n 指定每次将多少项,作为命令行参数。

  • -I 如果xargs要将命令行参数传给多个命令,可以使用-I参数。

    [root@localhost tmp]#  echo -n '/etc/fstab:/etc/inittab'  | xargs -t -d: -n1 -I{}  cp -a {}  /tmp
    cp -a /etc/fstab /tmp 
    cp -a /etc/inittab /tmp 
    [root@localhost tmp]# 
    
  • –max-procs xargs默认只用一个进程执行命令。如果命令要执行多次,必须等上一次执行完,才能执行下一次。–max-procs参数指定同时用多少个进程并行执行命令。

join

合并文件内容

语法

join [OPTION]... FILE1 FILE2

选项

  • -a FILENUM:除了显示匹配好的行另外将指定序号(1或2)文件里部匹配的行显示出来
  • -e EMPTY:将须要显示可是文件里不存在的域用此选项指定的字符取代
  • -i :忽略大写和小写
  • -j FIELD :等同于 -1 FIELD -2 FIELD,-j指定一个域作为匹配字段
  • -o FORMAT:以指定格式输出
  • -t CHAR :以指定字符作为输入输出的分隔符。join 默认以空白字符做分隔符(空格和\t),能够使用 join -t $’\t’来指定使用tab做分隔符
  • -v FILENUM:与-a相似 但值显示文件里没匹配上的行
  • -1 FIELD:以file1中FIELD字段进行匹配
  • -2 FIELD:以file2中FIELD字段进行匹配
  • –help :打印命令帮助文件

例子

[root@localhost tmp]# cat > test1 <<HH
> aa 12345
> bb 23456
> cc 58795
> dd 12457
> HH
[root@localhost tmp]# cat > test2 <<HH
> aa qwert
> bb uiopy
> cc hutfj
> ee hufvb
> HH
[root@localhost tmp]# join test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
[root@localhost tmp]# join -a1 test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
dd 12457
[root@localhost tmp]# join -a2 test1 test2  
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
ee hufvb
[root@localhost tmp]# join -o 1.2,2.2 test1 test2   
12345 qwert
23456 uiopy
58795 hutfj
[root@localhost tmp]# 

tr

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

语法

tr [OPTION]... SET1 [SET2]

选项

  • -c, -C, --complement \取代所有不属于第一字符集的字符
  • -d, --delete 删除所有属于第一字符集的字符
  • -s, --squeeze-repeats 把连续重复的字符以单独一个字符表示
  • -t, --truncate-set1 先删除第一字符集较第二字符集多出的字符

例子

[root@localhost tmp]# echo "abc1235a12bc" | tr 'abc' '<>?'     
<>?1235<12>?
[root@localhost tmp]# echo "abc1235a12bc" | tr -c 'abc' '<>?'  
abc????a??bc?[root@localhost tmp]# echo "abc1235a12bc" | tr -d 'abc'
123512

st tmp]# join test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
[root@localhost tmp]# join -a1 test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
dd 12457
[root@localhost tmp]# join -a2 test1 test2
aa 12345 qwert
bb 23456 uiopy
cc 58795 hutfj
ee hufvb
[root@localhost tmp]# join -o 1.2,2.2 test1 test2
12345 qwert
23456 uiopy
58795 hutfj
[root@localhost tmp]#




#### tr

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

**语法**

`tr [OPTION]... SET1 [SET2]`

**选项**

- -c, -C, --complement	\取代所有不属于第一字符集的字符
- -d, --delete             删除所有属于第一字符集的字符
- -s, --squeeze-repeats   把连续重复的字符以单独一个字符表示
- -t, --truncate-set1    先删除第一字符集较第二字符集多出的字符

**例子**

[root@localhost tmp]# echo “abc1235a12bc” | tr ‘abc’ ‘<>?’
<>?1235<12>?
[root@localhost tmp]# echo “abc1235a12bc” | tr -c ‘abc’ ‘<>?’
abc???a??bc?[root@localhost tmp]# echo “abc1235a12bc” | tr -d ‘abc’
123512




文本处理命令还有功能强大的sed和awk,由于功能强大且复杂,过段时间会另开一篇专门介绍
发布了22 篇原创文章 · 获赞 2 · 访问量 600

猜你喜欢

转载自blog.csdn.net/weixin_42955452/article/details/104468691