[me@linuxbox ~]$ cat > foo.txt The quick brown fox jumped over the lazy dog. [me@linuxbox ~]$ cat -ns foo.txt 1 The quick brown fox 2 3 jumped over the lazy dog. [me@linuxbox ~]$
[me@linuxbox ~]$ sort > foo.txt c b a [me@linuxbox ~]$ cat foo.txt a b c
选项 | 长选项 | 说明 |
---|---|---|
-b | --ignore-leading-blanks | 默认情况下,从行的第一个字符开始对整行执行排序。 此选项会导致排序忽略行中的前导空格,并根据行中的第一个非空白字符计算排序。 |
-f | --ignore-case | 使排序不区分大小写 |
-n | --numeric-sort | 根据字符串的数值计算执行排序。 使用此选项可以对数值而不是字母值进行排序。 |
-r | --reverse | 按相反顺序排序。 结果按降序而不是升序排列。 |
-k | --key= |
按 |
-m | --merge | 将每个参数视为预排序文件的名称。将多个文件合并到一个排序结果中,无需执行任何其他排序。 |
-o | --output= |
将结果发送到文件,而不是标准输出。 |
-t | --filed-separator= |
定义字段分隔符字符。默认情况下,字段由空格或制表符分隔。 |
[me@linuxbox ~]$ du -s /usr/share/* | head 252 /usr/share/aclocal 96 /usr/share/acpi-support 8 /usr/share/adduser 196 /usr/share/alacarte 344 /usr/share/alsa 8 /usr/share/alsa-base 12488 /usr/share/anthy 8 /usr/share/apmd 21440 /usr/share/app-install 48 /usr/share/application-registry
[me@linuxbox ~]$ du -s /usr/share/* | sort -nr | head 509940 /usr/share/locale-langpack 242660 /usr/share/doc 197560 /usr/share/fonts 179144 /usr/share/gnome 146764 /usr/share/myspell 144304 /usr/share/gimp 135880 /usr/share/dict 76508 /usr/share/icons 68072 /usr/share/apps 62844 /usr/share/foomatic
[me@linuxbox ~]$ ls -l /usr/bin | head total 152948 -rwxr-xr-x 1 root root 34824 2016-04-04 02:42 [ -rwxr-xr-x 1 root root 101556 2007-11-27 06:08 a2p -rwxr-xr-x 1 root root 13036 2016-02-27 08:22 aconnect -rwxr-xr-x 1 root root 10552 2007-08-15 10:34 acpi -rwxr-xr-x 1 root root 3800 2016-04-14 03:51 acpi_fakekey -rwxr-xr-x 1 root root 7536 2016-04-19 00:19 acpi_listen -rwxr-xr-x 1 root root 3576 2016-04-29 07:57 addpart -rwxr-xr-x 1 root root 20808 2016-01-03 18:02 addr2line -rwxr-xr-x 1 root root 489704 2016-10-09 17:02 adept_batch
[me@linuxbox ~]$ ls -l /usr/bin | sort -nrk 5 | head -rwxr-xr-x 1 root root 8234216 2016-04-07 17:42 inkscape -rwxr-xr-x 1 root root 8222692 2016-04-07 17:42 inkview -rwxr-xr-x 1 root root 3746508 2016-03-07 23:45 gimp-2.4 -rwxr-xr-x 1 root root 3654020 2016-08-26 16:16 quanta -rwxr-xr-x 1 root root 2928760 2016-09-10 14:31 gdbtui -rwxr-xr-x 1 root root 2928756 2016-09-10 14:31 gdb -rwxr-xr-x 1 root root 2602236 2016-10-10 12:56 net -rwxr-xr-x 1 root root 2304684 2016-10-10 12:56 rpcclient -rwxr-xr-x 1 root root 2241832 2016-04-04 05:56 aptitude -rwxr-xr-x 1 root root 2202476 2016-10-10 12:56 smbcacls
SUSE 10.2 12/07/2006 Fedora 10 11/25/2008 SUSE 11.0 06/19/2008 Ubuntu 8.04 04/24/2008 Fedora 8 11/08/2007 SUSE 10.3 10/04/2007 Ubuntu 6.10 10/26/2006 Fedora 7 05/31/2007 Ubuntu 7.10 10/18/2007 Ubuntu 7.04 04/19/2007 SUSE 10.1 05/11/2006 Fedora 6 10/24/2006 Fedora 9 05/13/2008 Ubuntu 6.06 06/01/2006 Ubuntu 8.10 10/30/2008 Fedora 5 03/20/2006
[me@linuxbox ~]$ sort distros.txt Fedora 10 11/25/2008 Fedora 5 03/20/2006 Fedora 6 10/24/2006 Fedora 7 05/31/2007 Fedora 8 11/08/2007 Fedora 9 05/13/2008 SUSE 10.1 05/11/2006 SUSE 10.2 12/07/2006 SUSE 10.3 10/04/2007 SUSE 11.0 06/19/2008 Ubuntu 6.06 06/01/2006 Ubuntu 6.10 10/26/2006 Ubuntu 7.04 04/19/2007 Ubuntu 7.10 10/18/2007 Ubuntu 8.04 04/24/2008 Ubuntu 8.10 10/30/2008
[me@linuxbox ~]$ sort --key=1,1 --key=2n distros.txt Fedora 5 03/20/2006 Fedora 6 10/24/2006 Fedora 7 05/31/2007 Fedora 8 11/08/2007 Fedora 9 05/13/2008 Fedora 10 11/25/2008 SUSE 10.1 05/11/2006 SUSE 10.2 12/07/2006 SUSE 10.3 10/04/2007 SUSE 11.0 06/19/2008 Ubuntu 6.06 06/01/2006 Ubuntu 6.10 10/26/2006 Ubuntu 7.04 04/19/2007 Ubuntu 7.10 10/18/2007 Ubuntu 8.04 04/24/2008 Ubuntu 8.10 10/30/2008
[me@linuxbox ~]$ sort -k 3.7nbr -k 3.1nbr -k 3.4nbr distros.txt Fedora 10 11/25/2008 Ubuntu 8.10 10/30/2008 SUSE 11.0 06/19/2008 Fedora 9 05/13/2008 Ubuntu 8.04 04/24/2008 Fedora 8 11/08/2007 Ubuntu 7.10 10/18/2007 SUSE 10.3 10/04/2007 Fedora 7 05/31/2007 Ubuntu 7.04 04/19/2007 SUSE 10.2 12/07/2006 Ubuntu 6.10 10/26/2006 Fedora 6 10/24/2006 Ubuntu 6.06 06/01/2006 SUSE 10.1 05/11/2006 Fedora 5 03/20/2006
[me@linuxbox ~]$ head /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh
[me@linuxbox ~]$ sort -t ':' -k 7 /etc/passwd | head me:x:1001:1001:Myself,,,:/home/me:/bin/bash root:x:0:0:root:/root:/bin/bash dhcp:x:101:102::/nonexistent:/bin/false gdm:x:106:114:Gnome Display Manager:/var/lib/gdm:/bin/false hplip:x:104:7:HPLIP system user,,,:/var/run/hplip:/bin/false klog:x:103:104::/home/klog:/bin/false messagebus:x:108:119::/var/run/dbus:/bin/false polkituser:x:110:122:PolicyKit,,,:/var/run/PolicyKit:/bin/false pulse:x:107:116:PulseAudio daemon,,,:/var/run/pulse:/bin/false
[me@linuxbox ~]$ cat > foo.txt a b c a b c [ctrl-d] [me@linuxbox ~]$ uniq foo.txta b c a b c [me@linuxbox ~]$ sort foo.txt | uniq a b c [me@linuxbox ~]$ sort -u foo.txt a b c uniq 只删除相邻的重复行
选项 | 长选项 | 描述 |
---|---|---|
-c | --count | 输出一个重复列表,前面是行出现的次数 |
-d | --repeated | 只输出重复的行,而不输出唯一的行 |
-f |
--skip-fields= |
忽略每行中的n个前导字段。像在 |
-i | --ignor-case | 进行行比较时忽略大小写 |
-s |
--skip-chars= |
忽略每行前导n个字符 |
-u | --unique | 仅输出唯一的行。忽略具有重复项的行。 |
[me@linuxbox ~]$ sort foo.txt | uniq -c 2 a 2 b 2 c
选项 | 长选项 | 描述 |
---|---|---|
-c |
--characters= |
提取 |
-f |
--fields= |
从 |
-d |
--delimeter= |
当指定了 默认情况下,字段必须由单个制表符分隔。 |
--complement | 提取整行文本,但 |
[me@linuxbox ~]$ cat -A distros.txt SUSE^I10.2^I12/07/2006$ Fedora^I10^I11/25/2008$ SUSE^I11.0^I06/19/2008$ Ubuntu^I8.04^I04/24/2008$ Fedora^I8^I11/08/2007$ SUSE^I10.3^I10/04/2007$ Ubuntu^I6.10^I10/26/2006$ Fedora^I7^I05/31/2007$ Ubuntu^I7.10^I10/18/2007$ Ubuntu^I7.04^I04/19/2007$ SUSE^I10.1^I05/11/2006$ Fedora^I6^I10/24/2006$ Fedora^I9^I05/13/2008$ Ubuntu^I6.06^I06/01/2006$ Ubuntu^I8.10^I10/30/2008$ Fedora^I5^I03/20/2006$
[me@linuxbox ~]$ cut -f 3 distros.txt 12/07/2006 11/25/2008 06/19/2008 04/24/2008 11/08/2007 10/04/2007 10/26/2006 05/31/2007 10/18/2007 04/19/2007 05/11/2006 10/24/2006 05/13/2008 06/01/2006 10/30/2008 03/20/2006
[me@linuxbox ~]$ cut -f 3 distros.txt | cut -c 7-10 2006 2008 2008 2008 2007 2007 2006 2007 2007 2007 2006 2006 2008 2006 2008 2006
[me@linuxbox ~]$ expand distros.txt|cat -A SUSE 10.2 12/07/2006$ Fedora 10 11/25/2008$ SUSE 11.0 06/19/2008$ Ubuntu 8.04 04/24/2008$ Fedora 8 11/08/2007$ SUSE 10.3 10/04/2007$ Ubuntu 6.10 10/26/2006$ Fedora 7 05/31/2007$ Ubuntu 7.10 10/18/2007$ Ubuntu 7.04 04/19/2007$ SUSE 10.1 05/11/2006$ Fedora 6 10/24/2006$ Fedora 9 05/13/2008$ Ubuntu 6.06 06/01/2006$ Ubuntu 8.10 10/30/2008$ Fedora 5 03/20/2006$以上输出结果中已经没有^I了 [me@linuxbox ~]$ expand distros.txt | cut -c 23- 2006 2008 2008 2008 2007 2007 2006 2007 2007 2007 2006 2006 2008 2006 2008 2006
[me@linuxbox ~]$ cut -d ':' -f 1 /etc/passwd | head root daemon bin sys sync games man lp mail news
[me@linuxbox ~]$ cut -f 1,2 distros-by-date.txt > distros-versions.txt [me@linuxbox ~]$ head distros-versions.txt Fedora 10 Ubuntu 8.10 SUSE 11.0 Fedora 9 Ubuntu 8.04 Fedora 8 Ubuntu 7.10 SUSE 10.3 Fedora 7 Ubuntu 7.04
[me@linuxbox ~]$ cut -f 3 distros-by-date.txt > distros-dates.txt [me@linuxbox ~]$ head distros-dates.txt 11/25/2008 10/30/2008 06/19/2008 05/13/2008 04/24/2008 11/08/2007 10/18/2007 10/04/2007 05/31/2007 04/19/2007
[me@linuxbox ~]$ paste distros-dates.txt distros-versions.txt 11/25/2008 Fedora 10 10/30/2008 Ubuntu 8.10 06/19/2008 SUSE 11.0 05/13/2008 Fedora 9 04/24/2008 Ubuntu 8.04 11/08/2007 Fedora 8 10/18/2007 Ubuntu 7.10 10/04/2007 SUSE 10.3 05/31/2007 Fedora 7 04/19/2007 Ubuntu 7.04 12/07/2006 SUSE 10.2 10/26/2006 Ubuntu 6.10 10/24/2006 Fedora 6 06/01/2006 Ubuntu 6.06 05/11/2006 SUSE 10.1 03/20/2006 Fedora 5
CUSTNUM FNAME LNAME ======== ===== ====== 4681934 John Smith
ORDERNUM CUSTNUM QUAN ITEM ======== ======= ==== ==== 3014953305 4681934 1 Blue Widget
FNAME LNAME QUAN ITEM ===== ===== ==== ==== John Smith 1 Blue Widget
[me@linuxbox ~]$ cut -f 1,1 distros-by-date.txt > distros-names.txt [me@linuxbox ~]$ paste distros-dates.txt distros-names.txt > distros-key-names.txt [me@linuxbox ~]$ head distros-key-names.txt 11/25/2008 Fedora 10/30/2008 Ubuntu 06/19/2008 SUSE 05/13/2008 Fedora 04/24/2008 Ubuntu 11/08/2007 Fedora 10/18/2007 Ubuntu 10/04/2007 SUSE 05/31/2007 Fedora 04/19/2007 Ubuntu
[me@linuxbox ~]$ cut -f 2,2 distros-by-date.txt > distros-vernums.txt [me@linuxbox ~]$ paste distros-dates.txt distros-vernums.txt > distros-key-vernums.txt [me@linuxbox ~]$ head distros-key-vernums.txt 11/25/2008 10 10/30/2008 8.10 06/19/2008 11.0 05/13/2008 9 04/24/2008 8.04 11/08/2007 8 10/18/2007 7.10 10/04/2007 10.3 05/31/2007 7 04/19/2007 7.04
[me@linuxbox ~]$ join distros-key-names.txt distros-key-vernums.txt | head 11/25/2008 Fedora 10 10/30/2008 Ubuntu 8.10 06/19/2008 SUSE 11.0 05/13/2008 Fedora 9 04/24/2008 Ubuntu 8.04 11/08/2007 Fedora 8 10/18/2007 Ubuntu 7.10 10/04/2007 SUSE 10.3 05/31/2007 Fedora 7 04/19/2007 Ubuntu 7.04
[me@linuxbox ~]$ cat > file1.txt a b c d [me@linuxbox ~]$ cat > file2.txt b c d e
[me@linuxbox ~]$ comm file1.txt file2.txt a b c d e
[me@linuxbox ~]$ comm -12 file1.txt file2.txt b c d
[me@linuxbox ~]$ diff file1.txt file2.txt 1d0 < a 4a4 > e
更改 | 描述 |
---|---|
将第二个文件中位置r2处的行附加到第一个文件中位置r1处。 | |
将第二个文件中位置r1处的行更改(替换)为位置r2处的行。 | |
删除第一个文件中位置r1处的行,该行将出现在第二个文件中的范围r2处 |
[me@linuxbox ~]$ diff -c file1.txt file2.txt *** file1.txt2008-12-23 06:40:13.000000000 -0500 --- file2.txt2008-12-23 06:40:34.000000000 -0500 *************** *** 1,4 **** - a b c d --- 1,4 ---- b c d + e
指示信号 | 含义 |
---|---|
blank(空) | 为上下文显示的一行。这并不表明这两个文件之间存在差异。 |
- | 删除了一行。这一行将出现在第一个文件中,但不会出现在第二个文件中。 |
+ | 添加了一行。这一行将出现在第二个文件中,但不会出现在第一个文件中。 |
! | 改变了一行。将显示该行的两个版本,每个版本位于更改组的相应部分。 |
[me@linuxbox ~]$ diff -u file1.txt file2.txt --- file1.txt2008-12-23 06:40:13.000000000 -0500 +++ file2.txt2008-12-23 06:40:34.000000000 -0500 @@ -1,4 +1,4 @@ -a b c d +e
指示信号 | 含义 |
---|---|
blank(空) | 两个文件共有的行 |
- | 这一行已从第一个文件中删除。 |
+ | 这一行已添加到第一个文件中。 |
[me@linuxbox ~]$ diff -Naur file1.txt file2.txt > patchfile.txt [me@linuxbox ~]$ patch < patchfile.txt patching file file1.txt [me@linuxbox ~]$ cat file1.txt b c d e
[me@linuxbox ~]$ echo "lowercase letters" | tr a-z A-Z LOWERCASE LETTERS
[me@linuxbox ~]$ echo "lowercase letters" | tr [:lower:] A AAAAAAAAA AAAAAAA
echo "secret text" | tr a-zA-Z n-za-mN-ZA-M frperg grkg echo "frperg grkg" | tr a-zA-Z n-za-mN-ZA-M secret text
echo "This is a secret text." | tr a-zA-Z n-za-mN-ZA-M Guvf vf n frperg grkg. echo "This is a secret text." | tr a-zA-Z n-za-mN-ZA-M | tr a-zA-Z n-za-mN-ZA-M This is a secret text.
[me@linuxbox ~]$ echo "aaabbbccc" | tr -s ab abccc [me@linuxbox ~]$ echo "abcabcabc" | tr -s ab abcabcabc [me@linuxbox ~]$ echo "aaaaaabcabbbbbcaaaaabc" | tr -s ab abcabcabc
[me@linuxbox ~]$ echo "front" | sed 's/front/back/' back
[me@linuxbox ~]$ echo "front" | sed 's_front_back_' back
[me@linuxbox ~]$ echo "front" | sed '1s/front/back/' back [me@linuxbox ~]$ echo -e "front\nfront" | sed '2s/front/back/' front back [me@linuxbox ~]$ echo -e "front\nfront" | sed 's/front/back/' back back
地址 | 解释 |
---|---|
行号。 |
|
$ | 最后一行 |
/ |
与POSIX基本正则表达式匹配的行。 注意,正则表达式由斜杠字符分隔。 或者,正则表达式可以用替换字符分隔,方法是使用\ |
从 地址可以是前面列出的任何一种单一地址格式。 |
|
首先匹配 例如,1~2表示每个奇数行;5~5表示从第五行开始,然后每隔武行。 |
|
匹配 |
|
匹配除了 |
[me@linuxbox ~]$ sed -n '1,5p' distros.txt SUSE 10.2 12/07/2006 Fedora 10 11/25/2008 SUSE 11.0 06/19/2008 Ubuntu 8.04 04/24/2008 Fedora 8 11/08/2007
[me@linuxbox ~]$ sed -n '/SUSE/p' distros.txt SUSE 10.2 12/07/2006 SUSE 11.0 06/19/2008 SUSE 10.3 10/04/2007 SUSE 10.1 05/11/2006
[me@linuxbox ~]$ sed -n '/SUSE/!p' distros.txt Fedora 10 11/25/2008 Ubuntu 8.04 04/24/2008 Fedora 8 11/08/2007 Ubuntu 6.10 10/26/2006 Fedora 7 05/31/2007 Ubuntu 7.10 10/18/2007 Ubuntu 7.04 04/19/2007 Fedora 6 10/24/2006 Fedora 9 05/13/2008 Ubuntu 6.06 06/01/2006 Ubuntu 8.10 10/30/2008 Fedora 5 03/20/2006
命令 | 解释 |
---|---|
= | 输出当前行号。 |
a | 在当前行后追加文本。 |
d | 删除当前行。 |
i | 在当前行前面插入文本。 |
p | 打印当前行。 默认情况下, |
q | 退出 如果没有指定 |
Q | 退出 |
s/ |
将匹配 此外, 替换后的尾部斜杠之后,可以指定一个可选标志来修改 |
y/ |
通过将 |
[me@linuxbox ~]$ sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/' distros.txt SUSE 10.2 2006-12-07 Fedora 10 2008-11-25 SUSE 11.0 2008-06-19 Ubuntu 8.04 2008-04-24 Fedora 8 2007-11-08 SUSE 10.3 2007-10-04 Ubuntu 6.10 2006-10-26 Fedora 7 2007-05-31 Ubuntu 7.10 2007-10-18 Ubuntu 7.04 2007-04-19 SUSE 10.1 2006-05-11 Fedora 6 2006-10-24 Fedora 9 2008-05-13 Ubuntu 6.06 2006-06-01 Ubuntu 8.10 2008-10-30 Fedora 5 2006-03-20
[me@linuxbox ~]$ echo "aaabbbccc" | sed 's/b/B/' aaaBbbccc [me@linuxbox ~]$ echo "aaabbbccc" | sed 's/b/B/g' aaaBBBccc
# sed script to produce Linux distributions report 1 i\ \ Linux Distributions Report\ s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/ y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
[me@linuxbox ~]$ sed -f distros.sed distros.txt Linux Distributions Report SUSE 10.2 2006-12-07 FEDORA 10 2008-11-25 SUSE 11.0 2008-06-19 UBUNTU 8.04 2008-04-24 FEDORA 8 2007-11-08 SUSE 10.3 2007-10-04 UBUNTU 6.10 2006-10-26 FEDORA 7 2007-05-31 UBUNTU 7.10 2007-10-18 UBUNTU 7.04 2007-04-19 SUSE 10.1 2006-05-11 FEDORA 6 2006-10-24 FEDORA 9 2008-05-13 UBUNTU 6.06 2006-06-01 UBUNTU 8.10 2008-10-30 FEDORA 5 2006-03-20
[me@linuxbox ~]$ cat -n distros.sed 1 # sed script to produce Linux distributions report 2 3 1 i\ 4 \ 5 Linux Distributions Report\ 6 7 s/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/ 8 y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/
[me@linuxbox ~]$ cat > foo.txt The quick brown fox jimped over the laxy dog. [me@linuxbox ~]$ aspell check foo.txt The quick brown fox jimped over the laxy dog. 1) jumped 6) wimped 2) gimped 7) camped 3) comped 8) humped 4) limped 9) impede 5) pimped 0) umped i) Ignore I) Ignore all r) Replace R) Replace all a) Add l) Add Lower b) Abort x) Exit ?