• Chapter 8 Intermediate Shell Tools II


    1.1 Sorting Your Output

    Use the sort utility. You can sort one or more files by putting the file names on the command line.

    1. [maxwell@MaxwellDBA sample]$ sort a.txt b.txt c.txt d.txt
    2. It can be handly to have your output in sorted order, and handier still not have to add sorting code to every program you write.
    3. Use the sort utility
    4. With no filenames on the command,sort will read from standard input you can pipe the output from a previous command into sort.
    5. You can sort one or more files by putting the file names on the command line
    6. [maxwell@MaxwellDBA sample]$ ls -ltr
    7. total 16
    8. -rw-rw-r-- 1 maxwell maxwell 21 Jul 28 16:09 a.txt
    9. -rw-rw-r-- 1 maxwell maxwell 77 Jul 28 16:10 b.txt
    10. -rw-rw-r-- 1 maxwell maxwell 127 Jul 28 16:10 c.txt
    11. -rw-rw-r-- 1 maxwell maxwell 129 Jul 28 16:12 d.txt
    12. [maxwell@MaxwellDBA sample]$

    With no filenames on the command, sort will read from standard input so you can pipe the output from a previous command into sort.

    [maxwell@MaxwellDBA sample]$ somecommands | sort
    • sort -r 

    to reverse the order of the sort.

    • sort -f

    to "fold" lower- and uppercase characters together.

    • sort --ignore-case
    1. [maxwell@MaxwellDBA sample]$ sort -r a.txt
    2. Use the sort utility
    3. [maxwell@MaxwellDBA sample]$ sort -r a.txt b.txt c.txt d.txt
    4. You can sort one or more files by putting the file names on the command line
    5. With no filenames on the command,sort will read from standard input you can pipe the output from a previous command into sort.
    6. Use the sort utility
    7. It can be handly to have your output in sorted order, and handier still not have to add sorting code to every program you write.
    8. [maxwell@MaxwellDBA sample]$ sort -f a.txt b.txt c.txt d.txt
    9. It can be handly to have your output in sorted order, and handier still not have to add sorting code to every program you write.
    10. Use the sort utility
    11. With no filenames on the command,sort will read from standard input you can pipe the output from a previous command into sort.
    12. You can sort one or more files by putting the file names on the command line
    13. [maxwell@MaxwellDBA sample]$
    14. [maxwell@MaxwellDBA sample]$ sort --ignore-case a.txt b.txt c.txt d.txt
    15. It can be handly to have your output in sorted order, and handier still not have to add sorting code to every program you write.
    16. Use the sort utility
    17. With no filenames on the command,sort will read from standard input you can pipe the output from a previous command into sort.
    18. You can sort one or more files by putting the file names on the command line
    19. [maxwell@MaxwellDBA sample]$

    1.2 Sorting Numbers

    1. [maxwell@MaxwellDBA sample]$ sort somedata
    2. 2
    3. 200
    4. 21
    5. 250
    6. [maxwell@MaxwellDBA sample]$ sort -n somedata
    7. 2
    8. 21
    9. 200
    10. 250
    11. [maxwell@MaxwellDBA sample]$

    cut -d':' -f7 /etc/passwd isolates the shell from the /etc/passwd file. Then we have to do an initial sort so that uniq will work. uniq -c counts consecutive, duplicate lines, which is why we need the pre-sort. Then sort -rn gives us a reverse, numerical sort, with the most popular shell at the top.

    1. [maxwell@MaxwellDBA sample]$ cut -d':' -f7 /etc/passwd | sort | uniq -c | sort -rn
    2. 27 /sbin/nologin
    3. 4 /bin/bash
    4. 1 /sbin/shutdown
    5. 1 /sbin/halt
    6. 1 /bin/sync
    7. [maxwell@MaxwellDBA sample]$ cut -d':' -f7 /etc/passwd | sort -u
    8. /bin/bash
    9. /bin/sync
    10. /sbin/halt
    11. /sbin/nologin
    12. /sbin/shutdown
    13. [maxwell@MaxwellDBA sample]$

    1.3 Sorting IP Addresses

    To sort the entire address as you would expect(POSIX syntax):

    1. [maxwell@MaxwellDBA sample]$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n ipaddr.list
    2. 10.0.0.2
    3. 10.0.0.5
    4. 10.0.0.20
    5. 118.178.233.224
    6. 192.168.0.1
    7. 192.168.0.2
    8. 192.168.0.4
    9. 192.168.0.12
    10. [maxwell@MaxwellDBA sample]$

    The -t option indicates the character to use as a separator between fields (in our case, a period) so that we can also specify which fields to sort first.

    In this case, -k 1,1n means “start sorting at the beginning of field one (1) and (,) stop sorting at the end of field one (1) and do a numerical sort (n).

    1. [maxwell@MaxwellDBA sample]$ sort -t. -k4n ipaddr.list
    2. 192.168.0.1
    3. 10.0.0.2
    4. 192.168.0.2
    5. 192.168.0.4
    6. 10.0.0.5
    7. 192.168.0.12
    8. 10.0.0.20
    9. 118.178.233.224
    10. [maxwell@MaxwellDBA sample]$

    1.4 Cutting Out Parts of Your Output

    Use the cut command with the -c option to take particular columns: Note that our example 'ps' command only works with certain systems

    1. [maxwell@MaxwellDBA sample]$ ps -l | cut -c12-15
    2. P
    3. 2004
    4. 2005
    5. 2005
    6. [maxwell@MaxwellDBA sample]$ ps -elf | cut -c58-
    7. IME TTY TIME CMD
    8. n28 ? 00:00:45 /usr/lib/systemd/systemd --system --deserialize 21
    9. n28 ? 00:00:01 [kthreadd]
    10. n28 ? 00:00:00 [rcu_gp]
    11. n28 ? 00:00:00 [rcu_par_gp]
    12. n28 ? 00:00:00 [kworker/0:0H-kblockd]
    13. n28 ? 00:00:00 [mm_percpu_wq]
    14. n28 ? 00:00:07 [ksoftirqd/0]
    15. n28 ? 00:14:11 [rcu_sched]
    16. n28 ? 00:00:00 [migration/0]
    17. n28 ? 00:00:01 [watchdog/0]
    18. n28 ? 00:00:00 [cpuhp/0]
    19. n28 ? 00:00:00 [cpuhp/1]
    20. n28 ? 00:00:01 [watchdog/1]

    Using cut to print out fields rather than columns is possible, though more limited than other choices such as awk. The default delimiter between fields is the Tab character, but you can specify a different delimiter with the -d option. Here is an example of a cut command using fields:

    1. [maxwell@MaxwellDBA sample]$ cut -d'#' -f2 < ipaddr.list
    2. 10.0.0.2
    3. 118.178.233.224
    4. 192.168.0.2
    5. 192.168.0.1
    6. 192.168.0.4
    7. 10.0.0.5
    8. 192.168.0.12
    9. 10.0.0.20
    1. [maxwell@MaxwellDBA sample]$ cut -d'[' -f2 delimited_data | cut -d']' -f1
    2. l1
    3. l2
    4. l3
    5. [maxwell@MaxwellDBA sample]$ cat delimited_data
    6. Line [l1].
    7. Line [l2].
    8. Line [l3].
    9. [maxwell@MaxwellDBA sample]$

    1.5 Removing Duplicate Lines

    somesequence | sort -u

     If you aren't running sort, just pipe the output into uniq.

    1. $ somesequence > myfile
    2. $ uniq myfile

    1.6 Compressing Files

    1. $ tar cf tarball_name.tar directory_of_files
    2. $ gzip tarball_name.tar
    1. [maxwell@MaxwellDBA sample]$ ls -ltr
    2. total 28
    3. -rw-rw-r-- 1 maxwell maxwell 21 Jul 28 16:09 a.txt
    4. -rw-rw-r-- 1 maxwell maxwell 77 Jul 28 16:10 b.txt
    5. -rw-rw-r-- 1 maxwell maxwell 127 Jul 28 16:10 c.txt
    6. -rw-rw-r-- 1 maxwell maxwell 129 Jul 28 16:12 d.txt
    7. -rw-rw-r-- 1 maxwell maxwell 13 Jul 28 16:22 somedata
    8. -rw-rw-r-- 1 maxwell maxwell 93 Jul 28 16:32 ipaddr.list
    9. -rw-rw-r-- 1 maxwell maxwell 33 Jul 28 16:45 delimited_data
    10. [maxwell@MaxwellDBA sample]$ tar cf tarball_somedata.tar somedata
    11. [maxwell@MaxwellDBA sample]$ ls -ltr
    12. total 40
    13. -rw-rw-r-- 1 maxwell maxwell 21 Jul 28 16:09 a.txt
    14. -rw-rw-r-- 1 maxwell maxwell 77 Jul 28 16:10 b.txt
    15. -rw-rw-r-- 1 maxwell maxwell 127 Jul 28 16:10 c.txt
    16. -rw-rw-r-- 1 maxwell maxwell 129 Jul 28 16:12 d.txt
    17. -rw-rw-r-- 1 maxwell maxwell 13 Jul 28 16:22 somedata
    18. -rw-rw-r-- 1 maxwell maxwell 93 Jul 28 16:32 ipaddr.list
    19. -rw-rw-r-- 1 maxwell maxwell 33 Jul 28 16:45 delimited_data
    20. -rw-rw-r-- 1 maxwell maxwell 10240 Jul 29 08:12 tarball_somedata.tar
    21. [maxwell@MaxwellDBA sample]$ gzip tarball_somedata.tar
    22. [maxwell@MaxwellDBA sample]$ ls -ltr
    23. total 32
    24. -rw-rw-r-- 1 maxwell maxwell 21 Jul 28 16:09 a.txt
    25. -rw-rw-r-- 1 maxwell maxwell 77 Jul 28 16:10 b.txt
    26. -rw-rw-r-- 1 maxwell maxwell 127 Jul 28 16:10 c.txt
    27. -rw-rw-r-- 1 maxwell maxwell 129 Jul 28 16:12 d.txt
    28. -rw-rw-r-- 1 maxwell maxwell 13 Jul 28 16:22 somedata
    29. -rw-rw-r-- 1 maxwell maxwell 93 Jul 28 16:32 ipaddr.list
    30. -rw-rw-r-- 1 maxwell maxwell 33 Jul 28 16:45 delimited_data
    31. -rw-rw-r-- 1 maxwell maxwell 157 Jul 29 08:12 tarball_somedata.tar.gz
    32. [maxwell@MaxwellDBA sample]$

    1.7 Uncompressing Files

     

     1.8 Checking a tar Archive for Unique Directories

    Use an awk script to parse off the directory names from the tar archieve's table of contents, then use sort -u to leave you with just the unique directory names:

    tar tf some.tar | awk -F/ '{print $1}' | sort -u

    1.9 Translating Characters

    Use the tr command to translate one character to another.

    1. [maxwell@MaxwellDBA sample]$ tr ";" ',' after.txt
    2. [maxwell@MaxwellDBA sample]$ ls -ltr
    3. total 40
    4. -rw-rw-r-- 1 maxwell maxwell 21 Jul 28 16:09 a.txt
    5. -rw-rw-r-- 1 maxwell maxwell 77 Jul 28 16:10 b.txt
    6. -rw-rw-r-- 1 maxwell maxwell 127 Jul 28 16:10 c.txt
    7. -rw-rw-r-- 1 maxwell maxwell 129 Jul 28 16:12 d.txt
    8. -rw-rw-r-- 1 maxwell maxwell 13 Jul 28 16:22 somedata
    9. -rw-rw-r-- 1 maxwell maxwell 93 Jul 28 16:32 ipaddr.list
    10. -rw-rw-r-- 1 maxwell maxwell 33 Jul 28 16:45 delimited_data
    11. -rw-rw-r-- 1 maxwell maxwell 45 Jul 29 08:15 tarball_somedata.tar.gz
    12. -rw-rw-r-- 1 maxwell maxwell 342 Jul 30 20:32 before.txt
    13. -rw-rw-r-- 1 maxwell maxwell 342 Jul 30 20:33 after.txt
    14. [maxwell@MaxwellDBA sample]$ cat after.txt
    15. In its simplest form, a tr command replaces occurrences of the first (and only) character of, the first argument with the first (and only) character of the second argument.
    16. In the example solution, we redirected input from the file named before and sent the
    17. output into the file named after and we translated all occurrences of a semicolon,
    18. [maxwell@MaxwellDBA sample]$ cat before.txt
    19. In its simplest form; a tr command replaces occurrences of the first (and only) character of; the first argument with the first (and only) character of the second argument.
    20. In the example solution; we redirected input from the file named before and sent the
    21. output into the file named after and we translated all occurrences of a semicolon;
    22. [maxwell@MaxwellDBA sample]$

    The tr command can do more that one translation at a time by putting the several characters to be translated in the first argument and their corresponding resultant characters in the second argument.

     tr ';:.!?' ',' commas.all

    1.10 Converting Uppercase to Lowercase

    You can translate all uppercase characters(A-Z) to lowercase(a-z) using the tr command and specifying a range of characters.as in:

    tr 'A-Z' 'a-z' af.ter
    

    There is also special syntax in tr for specifying this sort of range for upper- and lowercase conversions:

    tr '[:upper:]' '[:lower:]' af.ter

    Here’s a very simplistic encoding of a text message using a simple substitution cypher that offsets each character by 13 places (i.e., ROT13). An interesting characteristic of ROT13 is that the same process is used to both encipher and decipher the text:

    1. [maxwell@MaxwellDBA tmp]$ cat /tmp/joke
    2. Q: Why did the chicken cross the road?
    3. A: To get to the other side.
    4. [maxwell@MaxwellDBA tmp]$ tr 'A-Za-z' 'N-ZA-Mn-za-m' < /tmp/joke
    5. D: Jul qvq gur puvpxra pebff gur ebnq?
    6. N: Gb trg gb gur bgure fvqr.
    7. [maxwell@MaxwellDBA tmp]$ tr 'A-Za-z' 'N-ZA-Mn-za-m' < /tmp/joke | tr 'A-Za-z' 'N-ZA-Mn-za-m'
    8. Q: Why did the chicken cross the road?
    9. A: To get to the other side.
    10. [maxwell@MaxwellDBA tmp]$

    1.11 Converting DOS Files to Linux Format

    Use the -d option on tr to delete the character(s) in the supplied list. For example, to delete all DOS carriage returns (\r), use the command:

    tr -d '\r' file.txt

     1.13 Counting Lines,Words,or Characters in a File

    1. [maxwell@MaxwellDBA sample]$ wc before.txt
    2. 3 58 342 before.txt
    3. [maxwell@MaxwellDBA sample]$ # Line only
    4. [maxwell@MaxwellDBA sample]$ wc -l before.txt
    5. 3 before.txt
    6. [maxwell@MaxwellDBA sample]$ # Words only
    7. [maxwell@MaxwellDBA sample]$ wc -w before.txt
    8. 58 before.txt
    9. [maxwell@MaxwellDBA sample]$ # Characters only
    10. [maxwell@MaxwellDBA sample]$ wc -c before.txt
    11. 342 before.txt
    12. [maxwell@MaxwellDBA sample]$ ls -l before.txt
    13. -rw-rw-r-- 1 maxwell maxwell

     1.14 Rewrapping Paragraphs

    Use the fmt command, optionally with a goal and maximum line length:

    1. $ fmt mangled_text
    2. $ fmt 55 60 mangled_text
  • 相关阅读:
    Git覆盖本地项目代码
    三秋农忙,自动驾驶农机保驾护航
    【超全面】机器学习中的超参优化方法总结
    typescript76-在react中使用ts语法
    黑马C++ 03 提高9 —— STL常用算法2_替换和拷贝算法/算术生成算法/集合算法
    Java | sleep、wait、yield、join、notify、notifyAll
    ruoyi-vue前后端分离版本验证码实现思路
    d盘不见了怎么恢复?数据恢复,一键操作
    【网络编程】一文带你搞懂HTTPS协议
    【AI视野·今日Sound 声学论文速览 第二十二期】Tue, 10 Oct 2023
  • 原文地址:https://blog.csdn.net/u011868279/article/details/126037288