Shell--grep Command + Regular expression + basic syntax

Source: Internet
Author: User
Tags egrep

What is a regular

A regular is a way to describe a character or string by combining symbols with special meanings, called regular Expressions . Or, the regular is the rule used to describe a class of things.

in Linux, wildcards are interpreted by the shell, while regular expressions are interpreted by commands , and there are three types of text processing tools/commands that can be interpreted as regular: grep, sed, awk, and today we're going to be familiar with grep.

Python also uses regular expressions, which are slightly different from this regular expression, which can deepen our understanding.

---------------------------------------------------------------------------------

grep parameters

-N: Show line numbers
-O: Show only matching content
-Q: Silent mode, no output, you have to use $? To determine the success of the execution, that is, there is no filtering to the desired content

-L: If the match succeeds, only the file name is printed, the failure is not printed, usually-rl together,grep-rl ' root '/etc
-A: If the match is successful, the matching row and the subsequent n rows are printed together
-B: If the match succeeds, the matching row and its first n rows are printed together
-C: If the match succeeds, the matching row and its n rows are printed together
--color
-C: If the match succeeds, the number of rows to match is printed
-e: Equals Egrep, extended
-I: Ignore case

-V: Inverse, mismatch
-W: Match word

Here are some exercises we can do:

-------------------------

The regular introduction

^ Beginning of the line
$ End of line
. Any single character other than the line break
* 0 or more of the leading characters
. * All Characters
[] Any character within a group of characters
[^] reverse each character within a character group (does not match each character in a group of characters)
^[^] lines that begin with characters within a non-character group
[A-z] lowercase letter
[A-z] capital letters
[A-z] lowercase and uppercase letters
[0-9] Number
\< words are usually separated by spaces or special characters, and successive strings are treated as words.
\> Word Tail

? A leading character 0 or one
+ Leading character one or more

X{M} X appears m times
X{m,} X appears m times to multiple times (at least m times)
X{m,n} X appears m times to N times

Abc|def means ABC or DEF
A (Bc|de) F means abcf or adef

Extended Regular: Grep-e or Egrep

Here are some exercises:

------------------------------------------------------------

Basic syntax what is shell script

Stacking OS commands into executable files and executing the OS commands in the text from top to bottom is a script .
With some intelligent (conditional/flow control) controls, it becomes an intelligent script.

Variable part1 Why should there be a variable

The operation of a program is a variable of some column state, which is represented by the change of variable value.

Part2 Variable Naming conventions

start with a letter or underscore, and the rest can be: letters, numbers, underscores.

It is advisable to follow the following specifications:

1. Start with a letter
2. Connect the words with an underscore or an underscore
3. Differentiation of the same type with numbers
4. The best name for the file plus extension
Example: sql_bak.tar.gz,log_bak.tar.bz2

PART3 System Variables

Set and env differences
Set: Show All variables
ENV: Environment variables

Part4 variable Assignment

Varname=value
Echo $VARNAME
Delete variable unset VARNAME

Part5 Common system Variables

PATH
Pwd
Lang
HOME
Histsize
PS1
Ifs
The field separator is a space, wrapping, TAB key collection

PART6 global variables and local variables

[Email protected] ~]# gender= ' Male ' #在爹这个位置定义一个局部变量gender
[Email protected] ~]# export money=1000 #在爹这个位置定义一个全局变量money
[Email protected] ~]#
[Email protected] ~]#
[Email protected] ~]# bash #切换到子bash
[[email protected] ~]# echo $gender #在儿子这里看它爹的局部变量gender, results are empty, not seen

[[email protected] ~]# echo $money #在儿子这里看它爹的全局变量money, you can see
1000
[Email protected] ~]#
[Email protected] ~]# export hobby= ' Piao ' #在儿子这里定义一个全局变量hobby
[[Email protected] ~]# exit #退出, go into Dad's bash environment
Exit
[Email protected] ~]# echo $hobby #爹是看不到儿子的export的, son's son can see

[Email protected] ~]#

Part6 defining the bounds of variable names

[Email protected] ~]# rest_mem=20
[Email protected] ~]# echo ${rest_mem}%
20%

Part 7 data type

The variables in bash do not have to be declared, they are used. The default variables are character types, and you can have numeric types, normal scripts, both of which are sufficient.

operator Part1 arithmetic operator

+

-

*

/

%

Part2 Relationship Operations

With (())

<

>

<=

>=

==

!=

&&

||

Test command related, [] can achieve the same effect
[Email protected] ~]# x=1
[Email protected] ~]# [$x-gt 1]
[[email protected] ~]# echo $?
0

Shell--grep Command + Regular expression + basic syntax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.