In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use Shell regular expressions. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
What is a regular expression?
Regular expression is a grammatical rule used to describe the arrangement and matching patterns of characters. It is mainly used for string pattern segmentation, matching, search and replacement operations.
Regular expressions and wildcards
1. Regular expression
Used to match strings that match the criteria in the file, the regular expression is "include match". Commands such as grep, awk, sed, and so on can support regular expressions.
two。 Regular expression metacharacter
Regular expressions match strings through metacharacters. For more information, please see http://www.cnblogs.com/refine1017/p/5011522.html
3. Wildcard character
Used to match the file name that matches the criteria, and the wildcard character is "exact match". The commands ls, find, and cp do not support regular expressions, so you have to use shell's own wildcards to match.
4. Wildcards include
* match any character
? Match any character
[] matches any character in parentheses
3. Cut command
The cut command cuts bytes, characters, and fields from each line of the file and writes them to standard output.
1. Common parameters
-b: split in bytes. These byte positions ignore multibyte character boundaries unless the-n flag is also specified.
-c: split on a character basis.
-d: custom delimiter, default to tab.
-f: used with-d to specify which area to display.
-n: unsplit multibyte characters. Used only with the-b flag.
two。 Example 1: print out a line of a file split with tabs
[root@localhost shell] # cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 90 [root@localhost shell] # cut-f 4 student.txt Mark85707590
3. Example 2: print a line of a csv file
[root@localhost shell] # cat student.csv ID,Name,Gender,Mark1,ming,F,852,zhang,F,703,wang,M,754,li,M,90 [root@localhost shell] # cut-d ","-f 4 student.csv Mark85707590
4. Example 3: print the number of characters in a string
[root@localhost shell] # echo "abcdef" | cut-c 3c
5. Example 4: intercept a text of Chinese characters
[root@localhost shell] # echo "Shell programming" | cut-nb 1s [root@localhost shell] # echo "Shell programming" | cut-nb 2h [root@localhost shell] # echo "Shell programming" | cut-nb 3e [root@localhost shell] # echo "Shell programming" | cut-nb 4l [root@localhost shell] # echo "Shell programming" | cut-nb 5l [root@localhost shell] # echo "Shell programming" | cut-nb 8 [root@localhost shell] # echo Shell programming | cut-nb 11 programs
4. Printf command
1. Command format
Printf 'output type output format' output content
two。 Output type
% ns: output string. N represents several characters output, and n ellipsis represents all characters.
% ni: output integers. N means to output several numbers, and n omission represents all numbers.
% m.nf: outputs floating-point numbers. M and n are numbers that refer to the number of integer and decimal places of the output. For example,% 8.2f represents a total output of 8 digits, of which 2 bits are small trees and 6 bits are integers.
3. Output format
\ a: output warning sound
\ b: output Backspace key (Backspace)
\ f: clear the screen
\ n: line break
\ r: enter (Enter)
\ t: horizontal output backspace key
\ v: vertical output backspace key
4. Example
[root@localhost ~] # printf'% I% s% I\ n' 1 "+" 2 "=" 31 + 2 = 3 [root@localhost ~] # printf'% i-%i-%i% i:%i:%i\ n' 2015 12 3 21 56 302015-12-3 21:56:30
5. Awk command
1. Command format
Awk 'condition 1 {Action 1} condition 2 {Action 2}. File name
Condition: generally use relational expressions as conditions, such as x > 10
Actions: formatted output, flow control statements
two。 Example 1: extract a line of a file split by tabs
[root@localhost shell] # cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 90 [root@localhost shell] # awk'{print $1 "\ t" $4} 'student.txt ID Mark1 852 703 754 90
3. Example 2: get disk utilization
[root@localhost shell] # df-hFilesystem Size Used Avail Use% Mounted on/dev/sda2 18G 2.4G 14G 15% / / dev/sda1 289M 16M 258M 6% / boottmpfs 411M 0411M 0% / dev/shm [root@localhost shell] # df-h | grep "sda1" | awk'{print $5}'6%
VI. Sed command
Sed is a lightweight stream editor that is included in almost all UNIX platforms, including Linux. Sed is mainly used to select, replace, delete and add commands for data.
1. Command format
Sed [option]'[Action] 'file name
two。 Option
-n: normally, the sed command will output all the data to the screen. If you add this option, only the lines processed by the sed command will be output to the screen.
-e: allows multiple sed command edits to be applied to input data.
-I: use the modification result of sed to directly modify the file that reads the data, rather than output it from the screen.
3. action
A: append to add one or more lines after the current line
C: line substitution, replacing the original data row with the string after c
I: insert, insert one or more rows before the current row.
D: delete, delete the specified line
P: print, output the specified line
S: string substitution, replacing one string with another. The format is "line range / s / old string / new string / g" (similar to the replacement format in vim)
4. Example
[root@localhost shell] # cat student.txt ID Name Gender Mark1 ming F 852 zhang F 703 wang M 754 li M 9 Test-n Parameter [root@localhost shell] # sed-n'2p 'student.txt 1 ming F 8 Test single Line deletion [root@localhost shell] # sed' 2d' student.txt ID Name Gender Mark2 zhang F 703 wang M 754 li M 9 Test multiple Line deletion [root@localhost shell] ] # sed'2 sed'4d 'student.txtID Name Gender Mark4 li M 9 Test add [root@localhost shell] # sed' 2a test append' student.txtID Name Gender Mark1 ming F 85test append2 zhang F 703 wang M 754 li M 9 benchmark Test insert [root@localhost shell] # sed'2i test insert' student.txtID Name Gender Marktest insert1 ming F 852 zhang F 703 wang M 754 li M 9 substitute Test Line [root@localhost shell] # sed'2c test replace' student.txtID Name Gender Marktest replace2 zhang F 703 wang M 754 li M 9 replace [root@localhost shell] # sed '2s wang li M 90
Let's take a look at the matching examples of simple regular expressions. Through these examples, I believe you can master the use of basic regular expressions more skillfully:
HelloWorld matches 10 letters anywhere on any line: HelloWorld
^ HelloWorld matches the 10 letters that appear at the beginning of the line: HelloWorld
HelloWorld$ matches the 10 letters that appear at the end of the line: HelloWorld
^ HelloWorld$ match includes only these 10 letters: a line of HelloWorld
[Hh] elloWorld matches HelloWorld or helloworld
The Hello.World match contains the five letters Hello, plus any character, plus world
The Hello*World match contains the five letters Hello, plus any letter, plus world
Use "." in the above example. Or "*", you can match 0 or more characters, but if the character to be matched is a range, use "{}". Because the "{" and "}" in shell have a special meaning, you need to use the transfer character "\", for example:
[kouyang@kouyang kouyang] # grep-n'o\ {2\} 'hello.txt
Find the line in the hello.txt file where there are two consecutive "o"
[kouyang@kouyang kouyang] # grep-n'go\ {2,5\} g 'hello.txt
Find the line in the hello.txt file followed by 2'5 "o" followed by the word "g" after go.
This is the end of this article on "how to use Shell regular expressions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to 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.