In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Two parts of shell regular expression
1. Printf command:
1. Format the output. (print format)
two。 Command format:
Printf' print format 'actual content
1. Print format:
1)\ a (warning sound output)
1)\ b (Backspace key)
1)\ n (output a new line)
1)\ r (frame is at the beginning of the line, that is, enter key)
\ t (horizontal table key)
1)\ v (vertical table key)
1)\ xNN (x is the hexadecimal representation of ASCII code; NN is a two-digit number; convertible numbers into characters)
1)% ns (n is a number; s represents string, that is, how many characters)
1)% ni (n is a number; I represents integer, that is, how many integer characters)
1)% N.nf (n, N are numbers; f represents floating floating point, decimal number is decimal point)
1. Example:
1) output 23 as integers and line breaks, and hello as strings and line breaks
Output 23 as a 2-bit integer and line wrap, and output hello as a 7-bit string
Case text
The output is not aligned
As above, fields can be formatted to align (string length, integer and decimal point length)
2. Awk command:
Third, awk is more likely to be divided into several fields in a row.
The most basic function of .awk language is to decompose and extract information based on specified rules in a file or string, or to output data based on specified rules.
1) Command line mode
Awk [- F field-separator] 'commands' input-files
[- F field-separator], domain delimiter, default is space or tab
Commands is the awk command; input-files is the file to be processed.
The Linux system uses the environment variable IFS to store delimiters
View the default delimiter
OldIFS=$IFS (save the environment variable IFS to oldIFS)
IFS= "," (defines the delimiter as,)
The environment variable is then returned to its original value (space / tab).
1) shell scripting method
1) insert the awk command into a separate file, and then call
Awk-f awk-script-file input-files
Where the-f option loads the awk script in awk-script-file, and the input-files is the same as above.
1) regular expression is used as awk_pattern: / regexp/
For example: awk'/ ^ [amurz] / 'input_file
2) the Boolean expression is used as an awk_pattern. When the expression is established, the corresponding actions execution is triggered.
Variables (such as field variables $1 and / regexp/) can be used in ① expressions.
Operators in ② Boolean expressions:
Relational operator:
< >=! =
Match operator: value ~ / regexp/ returns true if value matches / regexp/,
Value! ~ / regexp/ returns true if value does not match / regexp/,
For example: awk'$2 > 10 {print "ok"} 'input_file
Awk'$3 ~ / ^ d / {print "ok"} 'input_file
③ & (and) and | (or) can join two / regexp/ or Boolean expressions to form a mixed expression. ! (non) can be used before a Boolean expression or / regexp/.
A) regular expression (/ regexp/)
Start with the lowercase letters a to d of passwd.
A) Boolean expression
Variables (such as field variables $1 $2) and / regexp/
Relational operation
Chars:
< >=! =
It is excellent to extract chinese greater than 75 from test.txt.
Match operator: value ~ / regexp/ returns true if value matches / regexp/,
Value! ~ / regexp/ returns true if value does not match / regexp/,
Extract the display ok that matches the beginning of d in the passwd.
A) & (and), | | (or)
The pattern includes two special fields, BEGIN and END. Use the begin statement to set the count and print head. The BEGIN statement is used before any text browsing action, after which the text browsing action starts to execute based on the input text. The END statement is used to print out the total number of text and the ending status flag after the awk completes the text browsing action.
1) Action:
The actual action is indicated in curly braces {}. Most of the actions are used for printing.
1. The running process of awk:
If the BEGIN block exists, the awk executes the actions it specifies.
Awk reads a line from the input file, which is called an input record. (if the input file is omitted, it will be read from standard input)
Awk splits the read record into fields, putting the first field in the variable $1, the second field in $2, and so on. $0 represents the entire record.
Compare the current input record in turn with the awk_pattern in each awk_cmd to see if it matches, and if so, execute the corresponding actions. If there is no match, the corresponding actions is skipped until all the awk_cmd are compared.
When an input record compares all the awk_cmd, awk reads the next line of the input and repeats steps ③ and ④, which continues until awk reads to the end of the file.
When awk has read all the input lines, if there is an END, execute the corresponding actions.
1) / etc/passwd file displays the user name and login shel in the file
Displays the user name and login shell in the file, separated by commas
Displays the username and login shell of all users with UID greater than 500 in the file
Show the user name and login shell with UID greater than 500in the / etc/passwd file, while the account and shell are separated by a comma, and the column name name,shell is added on all lines, and "blue,/bin/nosh" on the last line.
Comprehensive Application of function and script
1. Shell function: shell allows you to form a set of command sets or statements into an available block, called the shell function.
The function consists of two parts: the function title and the function body. The title is the function name. A function body is a collection of commands within a function. The title name should be unique; if not, it will confuse the result, because the script will first search for the function call to the appropriate shell before viewing the calling script.
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.