linux常用文本查看、操作命令

0.目录

1.文本查看命令

1.1 命令cat与tac

命令cat,用于显示文件内容。
选项“-n”,显示行号。

[root@localhost ~]# ls /boot > test
[root@localhost ~]# cat -n test
     1  config-2.6.32-642.el6.x86_64
     2  efi
     3  grub
     4  initramfs-2.6.32-642.el6.x86_64.img
     5  lost+found
     6  symvers-2.6.32-642.el6.x86_64.gz
     7  System.map-2.6.32-642.el6.x86_64
     8  vmlinuz-2.6.32-642.el6.x86_64

命令tac,用于逆序显示文件内容,与命令“cat”相反。它没有选项“-n”。

[root@localhost ~]# tac test
vmlinuz-2.6.32-642.el6.x86_64
System.map-2.6.32-642.el6.x86_64
symvers-2.6.32-642.el6.x86_64.gz
lost+found
initramfs-2.6.32-642.el6.x86_64.img
grub
efi
config-2.6.32-642.el6.x86_64

1.2 命令head

命令head,用于查看文件的前若干行,默认前10行。

[root@localhost ~]# head /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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

选项“- #”或“-n #”,显示文件前#行。

[root@localhost ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash

[root@localhost ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

1.3 命令tail

命令tail,用于显示文件的后若干行,默认后10行。

  • 选项“-#”或“-n #”,显示文件后#行。

    [root@localhost ~]# tail -1 /etc/passwd
    fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/sh
    
    [root@localhost ~]# tail -n 2 /etc/passwd
    gentoo:x:4001:4001::/home/gentoo:/bin/sh
    fedora:x:4002:4002:Fedora Core:/home/fedora:/bin/sh
  • 选项“-f”,执行后不退出,继续查看文件后来追加的内容。
    常用于查看日志等文件。

1.4 命令less、more

命令less
用于查看文件内容,常用快捷键:

按键意义
空格键/b向后/前翻一屏
ctrl + d/u向后/前翻半屏
回车/k向后/前翻一行
G跳至文档尾
#G跳至文件第#行。特别地1G表示跳至文件首
/keyword从所在行开始向文档尾逐行查找keyboard。“n”表示向后跳,“N”表示向前跳
?keyword从所在行开始向文档首逐行查找keyboard。“n”表示向前跳,“N”表示向后跳

命令more功能差不多,只是不能往前翻页。

1.5 命令wc

命令wc,即Word Count,用于显示某文件中的行数、单词数1、字节数。

[root@localhost ~]# echo "cat dog monkey" > test
[root@localhost ~]# wc test
 1  3 15 test
 # 显示该文件,以行数计为1行、以单词数计为3个、以字符数计为15个。
 # 因为命令“echo”默认有换行符,所以字符数是15个。

[root@localhost ~]# echo -n "cat dog monkey" > test
[root@localhost ~]# wc test
 0  3 14 test
 # 若行末不加换行符,即为显示出的14个字符。
 # 但行数变为了0,由此可知,命令“wc”是按换行符统计行数的。

各选项:

  • 选项“-l”,仅显示行(line)数。
  • 选项“-w”,仅显示单词(word)数。
  • 选项“-b”,仅显示字符(byte)数。

这些选项只是把其默认结果分别显示了。

2.文本处理

2.1 命令tr

命令tr,用于对位转换字符。
该命令无参数,从标准输入读取数据。

[root@localhost ~]# tr '123' '456'
123                         # 输入123
456                         # 对应转换后输出
367                         # 输入367
667                         # 6、7没有指定转换,所以不变

小写字母转换大写:

[root@localhost ~]# tr 'a-z' 'A-Z'
hello,how old are you
HELLO,HOW OLD ARE YOU

注意,此处使用‘a-z’、‘A-Z’表示小写、大写字母(也可使用双引号),此命令不支持使用如[a-z]、[[:upper:]]等表示字符集

选项“-d”,可用于删除指定的字符(可理解为将指定字符转换为空):

[root@localhost ~]# tr -d 'abc'
I have a black cat.
I hve  lk t.

[root@localhost ~]# echo "I have a black cat" | tr -d 'abc'
I hve  lk t

2.2 命令cut

命令cut,对文件逐行截取字段,输出至显示器。
一般连用如下两个选项:

  • 选项“-d”,用于指明文件以什么字符(必须是单个字符)为分隔符。若不指明,默认是tab。“-d”和它的参数之间可以没有空格。
  • 选项“-f”,用于指明显示哪些字段。多个不连续的字段号以逗号分开,多个连续的多个字段可用#-#表示。“-f”和它的参数之间可以没有空格。

    [root@localhost ~]# head -2 /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    [root@localhost ~]# head -2 /etc/passwd | cut -d: -f1,3-5,7
    root:0:0:root:/bin/bash
    bin:1:1:bin:/sbin/nologin
    
    # 以冒号为分隔符,取1、3、4、5、7字段。
    

2.3 命令sort

命令sort,用于把文件内容排序后,输出至显示器。排序不改变文件本身内容。

默认排序方式:从最左侧字符开始,逐位比较各字符依次排序,若某位字符相同则比较下一位字符:

[root@localhost ~]# ls / | sort
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
sbin
selinux
srv
sys
tmp
usr
var

各选项:

  • 选项“-n”,根据数值大小进行排序,而非使用默认的逐位比较。

    [root@localhost ~]# cat <<EOF > test
    > 1
    > 123
    > 21
    > EOF
    [root@localhost ~]# sort test
    1
    123
    21
    [root@localhost ~]# sort -n test
    1
    21
    123
  • 选项“-r”,逆序排序。

    [root@localhost ~]# sort test
    1
    123
    21
    [root@localhost ~]# sort -r test
    21
    123
    1
  • 选项“-t”、“-k”,前者用于指定分隔符,后者用于指定使用分隔后的哪个字段进行排序2

    [root@localhost ~]# head -3 /etc/passwd | sort
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    root:x:0:0:root:/root:/bin/bash
    [root@localhost ~]# head -3 /etc/passwd | sort -t: -k3
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
  • 选项“-u”,排序后,重复的行3仅显示一次。

    [root@localhost ~]# cat << EOF >test
    > 12
    > 1
    > 12
    > 31
    > EOF
    [root@localhost ~]# sort test
    1
    12
    12
    31
    [root@localhost ~]# sort -u test
    1
    12
    31

2.4 命令uniq

命令uniq,默认用于使文件重复的行仅显示一次;也可用于仅显示(或不显示)文件重复的行;也用于报告重复的行。

[root@localhost ~]# cat test
12
1
12
31
[root@localhost ~]# sort test
1
12
12
31
[root@localhost ~]# sort test | uniq           # 相当于“sort -u”
1
12
31
[root@localhost ~]# uniq test
12
1
12
31
# 若仅使用命令“uniq”,此文件就么有重复的行了,因为两个“12”不是连续的。命令“uniq”并没有排序功能

正因为命令“uniq”没有排序功能,所以它常与命令“sort”连用,否则对于那些内容相同、但位置不相连的行,“uniq”计数和处理都不会准确。如下例子也能说明此问题。
各选项:

  • 选项“-c”,对重复的行做统计,统计它们出现的次数。

    [root@localhost ~]# cat test
    12
    1
    12
    31
    [root@localhost ~]# uniq -c test           # 若不先排序,则两个“12”会被看做不同的内容各出现1次
          1 12
          1 1
          1 12
          1 31
    [root@localhost ~]# sort test | uniq -c
          1 1
          2 12
          1 31
    
    # 左边一列为出现的次数
    
  • 选项“-u”,仅显示没有重复出现的行
    接上例:

    [root@localhost ~]# sort test
    1
    12
    12
    31
    [root@localhost ~]# sort test | uniq -u
    1
    31
  • 选项“-d”,仅显示重复出现的行。
    接上例:

    [root@localhost ~]# sort test
    1
    12
    12
    31
    [root@localhost ~]# sort test | uniq -d
    12

2.5 命令diff

命令diff,用于逐行比较两文件的不同之处。

[root@localhost ~]# cat << EOF > test1
> hello
> how are you
> thank you
> EOF
[root@localhost ~]# cat << EOF >test2
> hi
> how old are you
> thank you
> EOF
[root@localhost ~]# diff test1 test2
1,2c1,2
< hello
< how are you
---
> hi
> how old are you

接上,可把比较结果重定向至某文件,作为一个补丁文件。

[root@localhost ~]# diff test1 test2 > test.patch

2.6 命令patch

命令patch,用于向老文件打补丁,使之和新文件相同。
选项“-i”,用于指定补丁文件。
以下为文件打补丁过程,使文件“test1”有与文件“test2”相同的内容。

[root@localhost ~]# cat test1
hello
how are you
thank you
[root@localhost ~]# cat test2
hi
how old are you
thank you
[root@localhost ~]# diff test1 test2 > test.patch  # 此处test1作为原始文件。
[root@localhost ~]# patch -i test.patch test1
patching file test1
[root@localhost ~]# cat test1                      # 仅能对原始文件test1打补丁,若参数为test2则会报错。
hi
how old are you
thank you

选项“-R”,还原补丁之前的内容。
接上例:

[root@localhost ~]# patch -i test.patch -R test1
patching file test1
[root@localhost ~]# cat test1
hello
how are you
thank you

也可使用输入重定向打补丁。
接上例:

[root@localhost ~]# patch test1 < test.patch 
patching file test1
[root@localhost ~]# cat test1
hi
how old are you
thank you

(完)


  1. 并不是指真正的单词,连续的不包含特殊字符的字符串即视为单词。
  2. 这个功能类似于命令“cut”。
    但如果使用cut,再管道sort排序,就只能显示排序的字段了,而使用sort的“-t、-k”选项,可以把其他字段也输出,而非仅有作为排序依据的字段。
  3. 连续且相同的行,不连续的不算重复,下同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值