linux常用命令-文本处理cut,sort,uniq,wc,tr

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux常用命令-文本处理cut,sort,uniq,wc,tr相关的知识,希望对你有一定的参考价值。

cut:截取文本特定字段

NAME
       cut - remove sections from each line of files


-d, --delimiter=DELIM(指定字段分隔符,默认是空格)

              use DELIM instead of TAB for field delimiter


-f, --fields=LIST(指定要显示的字段)

              select  only  these  fields;  also print any line that contains no delimiter character,
              unless the -s option is specified
  -f 1,3
  -f 1-3


[[email protected] ~]$ cat /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

[[email protected] ~]$ cut -d : -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown


sort:文本排序,以ascii

NAME
       sort - sort lines of text files

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


[[email protected] ~]$ cat sort.txt
3
5
1
4
9
[[email protected] ~]$ sort sort.txt
1
3
4
5
9
[[email protected] ~]$


-n, --numeric-sort(数值排序)
              compare according to string numerical value


-r, --reverse(反向排序)
              reverse the result of comparisons

[[email protected] ~]$ sort -r sort.txt
9
5
4
3
1
[[email protected] ~]$


-t, --field-separator=SEP(字段分隔符)
              use SEP instead of non-blank to blank transition

-k, --key=POS1[,POS2](关键字进行排序)

              start a key at POS1 (origin 1), end it at POS2 (default end of line)

[[email protected] ~]$ 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
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin


-u, --unique(删除重复项)
              with -c, check for strict ordering; without -c, output only the first of an equal run

[[email protected] ~]$ cat sort.txt
3
5
1
4
43
9
4
11
[[email protected] ~]$ sort -u sort.txt
1
11
3
4
43
5
9


uniq:显示或者忽略重复行

NAME
       uniq - report or omit repeated lines


-d, --repeated(只显示重复行)
              only print duplicate lines

[[email protected] ~]$ uniq sort.txt
3
5
1
4
43
9
4
11
[[email protected] ~]$ uniq -d sort.txt
3
1
[[email protected] ~]$


-D, --all-repeated[=delimit-method](显示所有重复)
              print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is
              done with blank lines.

[[email protected] ~]$ uniq -D sort.txt
3
3
1
1
[[email protected] ~]$


-c, --count(显示文件行重复的次数)
              prefix lines by the number of occurrences

[[email protected] ~]$ uniq -c sort.txt
      2 3
      1 5
      2 1
      1 4
      1 43
      1 9
      1 4
      1 11
[[email protected] ~]$


wc:显示文件中行数/单词数/字节数

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

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


-l, --lines
              print the newline counts

[[email protected] ~]$ wc /etc/fstab
 15  78 805 /etc/fstab
[[email protected] ~]$ man wc
[[email protected] ~]$ wc -l /etc/fstab
15 /etc/fstab


-w, --words
              print the word counts

[[email protected] ~]$ wc /etc/fstab
 15  78 805 /etc/fstab

[[email protected] ~]$ wc -w /etc/fstab
78 /etc/fstab
[[email protected] ~]$


-c, --bytes
              print the byte counts

[[email protected] ~]$ wc /etc/fstab
 15  78 805 /etc/fstab

[[email protected] ~]$ wc -c /etc/fstab
805 /etc/fstab


-L, --max-line-length(最长行字符数)
              print the length of the longest line

[[email protected] ~]$ wc -L /etc/fstab
93 /etc/fstab


tr:转换或者删除字符

NAME
       tr - translate or delete characters

SYNOPSIS
       tr [OPTION]... SET1 [SET2]


[[email protected] ~]$ tr ‘ab‘ ‘AB‘
abc
ABc
able
ABle
again
AgAin
^C
[[email protected] ~]$


[[email protected] ~]$ tr ‘a-z‘ ‘A-Z‘ < /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


-d, --delete(删除字符集)
              delete characters in SET1, do not translate

[[email protected] ~]$ tr -d ‘ab‘
alb^H^H^[[3~^[[3~


able
le
a


enable
enle




以上是关于linux常用命令-文本处理cut,sort,uniq,wc,tr的主要内容,如果未能解决你的问题,请参考以下文章

Linux常用文本操作命令整理

grep,cut,wc,sort,diff,uniq,patch命令

Linux学习总结(十八)几个简单的文本处理工具cut sort tr split

Linux 基础-文本处理命令

Linux Shell处理文本的命令大全

Linux学习笔记——文本管理命令及相关选项