In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use regular expression commands in Linux, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.
Regular expressions use a single string to describe and match a series of strings that match a syntactic rule. Regular expressions are cumbersome, but they are powerful. After learning them, they will give you an absolute sense of accomplishment in addition to improving your efficiency.
grep command
First up was the grep utility, an acronym for Global Regular Expression Print, a powerful command-line tool handy for searching for specific strings or patterns in files.
For example, to search the file sshd_conf.txt for lines containing the ssh string regardless of case, run the following command:
[root@localhost ~]# grep -i ssh sshd_conf.txt
To learn more about how to use the grep command, check out the grep manual: man grep
sed command
sed is an abbreviation for Stream Editor, used to manipulate text in text files. sed searches, filters, and replaces strings in a given file in a non-interactive manner.
By default, the sed command prints output to standard output, which means that the results of execution are printed on the terminal rather than saved to a file.
For example, to change SELINUX=enforcing to SELINUX=disabled in/etc/selinux/config, call the following command:
[root@localhost ~]# sed -i '/^SELINUX=/s/enforcing/disabled/g' /etc/selinux/config
After modification, save exit, restart the operating system, so that selinux can be turned off.
To learn more about how to use the sed command, check out sed's manual: man sed.
ack command
Ack is a fast and portable command line tool written in Perl. Ack is considered a substitute for grep and outputs the results in a visually appealing way. The Ack command searches a file or directory for lines that match the search criteria. Then highlight matching strings in each row.
Installation method (Centos7 system):
[root@localhost ~]# yum -y install ack
For example, to find lines containing ssh in sshd_conf.txt, case insensitive, run:
[root@localhost ~]# ack -i ssh sshd_conf.txt
Search keywords will be highlighted.
The search tool is smart enough to search the current directory and subdirectories if the user doesn't provide a file or directory. In the following example, no files or directories are provided, but ack automatically detects lines in files matching strings in directories and subdirectories:
[root@localhost ~]# ack -i ssh
awk command
Awk is a full-fledged scripting language and a text processing and data processing tool. Let us take the simplest example. Suppose you are printing system dates and want to print only the first value, which is the day of the week. In this case, the output is piped into awk as follows:
[root@localhost ~]# dateMon Nov 23 10:47:42 CST 2020[root@localhost ~]# date |awk '{print $1}'Mon
To display the latter values, separate them with commas, as follows:
[root@localhost ~]# date |awk '{print $6,$2,$3}'2020 Nov 23
Silver Searcher Tool
Silver Searcher is a cross-platform and open-source code search tool similar to ack, but focused on speed. It makes it easy to search for specific strings in files in the shortest possible time:
Installation method (Centos7):
[root@localhost ~]# yum -y install epel-release && yum makecache[root@localhost ~]# yum -y install the_silver_searcher
For example, search for the string "nologin" in the/etc/passwd file using the following command:
[root@localhost ~]# ag nologin /etc/passwd
ripgrep tool
Ripgrep command-line tool. Ripgrep is a cross-platform utility for regular expression patterns. It is much faster than all the search tools mentioned earlier and can recursively search the catalog to find matching patterns. In terms of speed and performance, no other tool is better than Ripgrep.
By default, ripgrep skips binaries/hidden files and directories. Also, by default, it does not search for files ignored by.gitignore / .ignore / .rgignore files.
To install ripgrep(Centos7):
[root@localhost ~]# yum -y install yum-utils[root@localhost ~]# sudo yum-config-manager --add-repo=https://copr.fedorainfracloud.org/coprs/carlwgeorge/ripgrep/repo/epel-7/carlwgeorge-ripgrep-epel-7.repo[root@localhost ~]# yum -y install ripgrep
For example, to search for the string "Linux" in files within the current directory, run the following command:
[root@localhost ~]# rg Linux
About "how to use regular expression commands in Linux" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.