Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Linux filtering command

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Linux filtering commands". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Linux filtering commands.

There are many commands that can be used to filter in Linux systems, such as awk commands, sed commands, head commands, and so on. The specific usage is as follows: 1. Awk command

Awk is an excellent mode scanning and processing language that can be used to construct useful filters under Linux. You can start using it by reading parts 1 through 13 of our awk series.

You can also read awk's man manual for more information and options.

$man awk2, sed command

Sed is a powerful stream editor for filtering and transforming text. We have written two useful articles about sed, which you can learn from here:

How to use GNU sed commands to create, edit, and process files under Linux 15 useful sed command tips and tips for daily Linux system administrator tasks

Sed's man manual has added control options and instructions:

$man sed3, grep, egrep, fgrep, rgrep command line

These filters output rows that match the specified pattern. They read lines from a file or standard input and output all matching lines, default output to standard output.

Note: the main program is grep, and these variants are the same as grep with specific options, as shown below (they are still in use for backward compatibility):

$egrep = grep-E$ fgrep = grep-F$ rgrep = grep-r

Here are some basic grep commands:

Tecmint@TecMint ~ $grep "aaronkilik" / etc/passwdaaronkilik:x:1001:1001::/home/aaronkilik:tecmint@TecMint ~ $cat / etc/passwd | grep "aronkilik" aaronkilik:x:1001:1001::/home/aaronkilik:

What are the differences among grep, egrep and fgrep under Linux? You can learn more about it.

4. Head command

Head is used to display the first part of the file, and by default it outputs the first 10 lines. You can use the-n flag to specify the number of lines to display:

Tecmint@TecMint ~ $head / var/log/auth.log Jan 2 10:45:01 TecMint CRON [3383]: pam_unix (cron:session): session opened for user root by (uid=0) Jan 2 10:45:01 TecMint CRON [3383]: pam_unix (cron:session): session closed for user rootJan 2 10:51:34 TecMint sudo: tecmint: TTY=unknown; PWD=/home/tecmint; USER=root COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.pyJan 2 10:51:34 TecMint sudo: pam_unix (sudo:session): session opened for user root by (uid=0) Jan 2 10:51:39 TecMint sudo: pam_unix (sudo:session): session closed for user rootJan 2 10:55:01 TecMint CRON [4099]: pam_unix (cron:session): session opened for user root by (uid=0) Jan 2 10:55:01 TecMint CRON [4099]: pam_unix (cron:session): session closed for user RootJan 2 11:05:01 TecMint CRON [4138]: pam_unix (cron:session): session opened for user root by (uid=0) Jan 2 11:05:01 TecMint CRON [4138]: pam_unix (cron:session): session closed for user rootJan 2 11:09:01 TecMint CRON [4146]: pam_unix (cron:session): session opened for user root by (uid=0) tecmint@TecMint ~ $head-n 5 / var/log/auth.log Jan 2 10:45:01 TecMint CRON [3383]: pam _ unix (cron:session): session opened for user root by (uid=0) Jan 2 10:45:01 TecMint CRON [3383]: pam_unix (cron:session): session closed for user rootJan 2 10:51:34 TecMint sudo: tecmint: TTY=unknown PWD=/home/tecmint; USER=root; COMMAND=/usr/lib/linuxmint/mintUpdate/checkAPT.pyJan 2 10:51:34 TecMint sudo: pam_unix (sudo:session): session opened for user root by (uid=0) Jan 2 10:51:39 TecMint sudo: pam_unix (sudo:session): session closed for user root

Learn how to use the head command with the tail and cat commands for more efficient use under Linux.

5. Tail command

Tail outputs the later part of a file (the default is 10 lines). Use the-n option to specify the number of rows to display.

The following command outputs the last five lines of the specified file:

Tecmint@TecMint ~ $tail-n 5 / var/log/auth.logJan 6 13:01:27 TecMint sshd [1269]: Server listening on 0.0.0.0 port 22.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on:: port 22.Jan 6 13:01:27 TecMint sshd [1269]: Received SIGHUP; restarting.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on 0.0.0.0 port 22.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on:: port 22.

In addition, tail has a special option-f, which allows you to view changes in a file (especially log files) in real time.

The following command will enable you to monitor changes to the specified file:

Tecmint@TecMint ~ $tail-f / var/log/auth.logJan 6 12:58:01 TecMint sshd [1269]: Server listening on:: port 22.Jan 6 12:58:11 TecMint sshd [1269]: Received SIGHUP; restarting.Jan 6 12:58:12 TecMint sshd [1269]: Server listening on 0.0.0.0 port 22.Jan 6 12:58:12 TecMint sshd [1269]: Server listening on:: port 22.Jan 6 13:01:27 TecMint sshd [1269]: Received SIGHUP Restarting.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on 0.0.0.0 port 22.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on:: port 22.Jan 6 13:01:27 TecMint sshd [1269]: Received SIGHUP; restarting.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on 0.0.0.0 port 22.Jan 6 13:01:27 TecMint sshd [1269]: Server listening on:: port 22.

Read tail's man manual for complete usage options and instructions:

$man tail6, sort command

Sort is used to sort the lines of text files or standard input.

Here is the contents of a file called domain.list:

Tecmint@TecMint ~ $cat domains.listtecmint.comtecmint.comnews.tecmint.comnews.tecmint.comlinuxsay.comlinuxsay.comwindowsmint.comwindowsmint.com

You can run a simple sort command like this to sort the contents of the file:

Tecmint@TecMint ~ $sort domains.listlinuxsay.comlinuxsay.comnews.tecmint.comnews.tecmint.comtecmint.comtecmint.comwindowsmint.comwindowsmint.com

You can use the sort command in many ways. Please refer to some useful articles about the sort command below.

14 useful examples of Linux's' sort' command (1) seven interesting examples of Linux's' sort' command (2) how to find and sort files based on modification date and time 7, uniq command

The uniq command is used to report or ignore duplicate lines. It filters lines from standard input and writes the results to standard output.

After running sort on an input stream, you can use uniq to delete duplicate lines, as shown in the following example.

To show the number of lines that appear, use the-c option, and to ignore the case difference when comparing, use the-I option:

Tecmint@TecMint ~ $cat domains.listtecmint.comtecmint.comnews.tecmint.comnews.tecmint.comlinuxsay.comlinuxsay.comwindowsmint.comtecmint@TecMint ~ $sort domains.list | uniq-c 2 linuxsay.com2 news.tecmint.com2 tecmint.com1 windowsmint.com

Read uniq's man manual for further usage information and options:

$man uniq8, fmt command line

Fmt is a simple optimized text formatter that reformats the paragraphs of the specified file and prints the results to standard output.

The following is the content extracted from the file domain-list.txt:

1.tecmint.com 2.news.tecmint.com 3.linuxsay.com 4.windowsmint.com

To reformat the above into a standard list, run the following command, using the-w option to define the maximum line width:

Tecmint@TecMint ~ $cat domain-list.txt1.tecmint.com2.news.tecmint.com3.linuxsay.com 4.windowsmint.comtecmint@TecMint ~ $fmt-w 1 domain-list.txt1.tecmint.com2.news.tecmint.com3.linuxsay.com4.windowsmint.com9, pr command

The pr command converts a text file or standard input and prints it out. For example, on a Debian system, you can display all the installation packages as follows:

$dpkg-l

To organize the list to be printed in the pages and columns, use the following command.

Tecmint@TecMint ~ $dpkg-l | pr-- columns 3-l 20 2017-01-06 13:19 Page 1Desired=Unknown/Install ii adduser ii apg | Status=Not/Inst/Conf- ii adwaita-icon-theme ii app-install-data | / Err?= (none) / Reinst-r ii adwaita-icon-theme- ii apparmor | | / Name ii alsa-base Ii apt+++-= ii alsa-utils ii apt-cloneii accountsservice ii anacron ii apt-transport-httpsii acl ii apache2 ii apt-utilsii acpi-support ii apache2-bin ii apt-xapian-indexii acpid ii apache2-data ii aptdaemonii add-apt-key ii apache2-utils ii aptdaemon-data2017-01 -06 13:19 Page 2ii aptitude ii avahi-daemon ii bind9-hostii aptitude-common ii avahi-utils ii binfmt-supportii apturl ii aview ii binutilsii apturl-common ii banshee ii bisonii archdetect-deb ii baobab ii bltii Aspell ii base-files ii blueberryii aspell-en ii base-passwd ii bluetoothii at-spi2-core ii bash ii bluezii attr ii bash-completion ii bluez-cupsii avahi-autoipd ii bc ii bluez-obexd.

Among them, the signs used are as follows:

-column defines the number of columns created in the output. -l specifies the length of the page (the default is 66 lines). 10. Tr command line

This command converts or deletes characters from standard input, and then outputs the results to standard output.

The syntax for using tr is as follows:

$tr options set1 set2

Take a look at the following example. In the first command, set1 ([: upper:]) represents the case (all uppercase characters) of the specified input characters. Set2 ([: lower:]) represents the case of the desired result character. The second example has a similar meaning, with the escape character / n indicating printout on a new line:

Tecmint@TecMint ~ $echo "WWW.TECMINT.COM" | tr [: upper:] [: lower:] www.tecmint.comtecmint@TecMint ~ $echo "news.tecmint.com" | tr [: lower:] [: upper:] NEWS.TECMINT.COM11, more command

The more command is a useful file filter originally built to view certificates. It displays the contents of the file page by page, and the user can display more information by pressing enter.

You can use it to display large files like this:

Tecmint@TecMint ~ $dmesg | more [0.000000] Initializing cgroup subsys cpuset [0.000000] Initializing cgroup subsys cpu [0.000000] Initializing cgroup subsys cpuacct [0.000000] Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2)) # 37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 (Ubuntu 4.4.0-21.37-generic4.4. 6) [0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-21-generic root=UUID=bb29dda3-bdaa-4b39-86cf-4a6dc9634a1b ro quiet splash vt.handoff=7 [0.000000] KERNEL supported cpus: [0.000000] Intel GenuineIntel [0.000000] AMD AuthenticAMD [0.000000] Centaur CentaurHauls [0.000000] x86/fpu: xstate_offset [2]: 576 Xstate_sizes [2]: 256 [0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers' [0.000000] x86/fpu: Supporting XSAVE feature 0x02:' SSE registers' [0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers' [0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes Using 'standard' format. [0.000000] x86/fpu: Using' eager' FPU context switches. [0.000000] e820: BIOS-provided physical RAM map: [0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable [0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved [0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved [0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable [ 0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved [0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable--More--12, Less command

Less is the opposite of the more command above, but it provides additional features, and it's faster for large files.

Use it in the same way as the more command:

Tecmint@TecMint ~ $dmesg | less [0.000000] Initializing cgroup subsys cpuset [0.000000] Initializing cgroup subsys cpu [0.000000] Initializing cgroup subsys cpuacct [0.000000] Linux version 4.4.0-21-generic (buildd@lgw01-21) (gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2)) # 37-Ubuntu SMP Mon Apr 18 18:33:37 UTC 2016 (Ubuntu 4.4.0-21.37-generic4.4. 6) [0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.4.0-21-generic root=UUID=bb29dda3-bdaa-4b39-86cf-4a6dc9634a1b ro quiet splash vt.handoff=7 [0.000000] KERNEL supported cpus: [0.000000] Intel GenuineIntel [0.000000] AMD AuthenticAMD [0.000000] Centaur CentaurHauls [0.000000] x86/fpu: xstate_offset [2]: 576 Xstate_sizes [2]: 256 [0.000000] x86/fpu: Supporting XSAVE feature 0x01: 'x87 floating point registers' [0.000000] x86/fpu: Supporting XSAVE feature 0x02:' SSE registers' [0.000000] x86/fpu: Supporting XSAVE feature 0x04: 'AVX registers' [0.000000] x86/fpu: Enabled xstate features 0x7, context size is 832 bytes Using 'standard' format. [0.000000] x86/fpu: Using' eager' FPU context switches. [0.000000] e820: BIOS-provided physical RAM map: [0.000000] BIOS-e820: [mem 0x0000000000000000-0x000000000009d3ff] usable [0.000000] BIOS-e820: [mem 0x000000000009d400-0x000000000009ffff] reserved [0.000000] BIOS-e820: [mem 0x00000000000e0000-0x00000000000fffff] reserved [0.000000] BIOS-e820: [mem 0x0000000000100000-0x00000000a56affff] usable [ 0.000000] BIOS-e820: [mem 0x00000000a56b0000-0x00000000a5eaffff] reserved [0.000000] BIOS-e820: [mem 0x00000000a5eb0000-0x00000000aaabefff] usable: thank you for reading The above is the content of "how to use Linux filtering commands". After the study of this article, I believe you have a deeper understanding of how to use Linux filtering commands, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report