Showing posts with label Shell. Show all posts
Showing posts with label Shell. Show all posts

Sunday, 27 September 2015

LINUX GREP 进阶用法以及 regular expression, globbing

Regular expressions
Regular expressions, called regexp or regex, are an essential support for many UNIX.
A regular expression is a pattern that describes a set of strings. Regular expressions are constructed as arithmetic: they use different operators to combine smaller expressions.
Builtins
The builtins are commands integrated to the shell and executed by the shell itself.
To check whether a command is a builtin or not, we can use the builtin builtin.

Quoting
double quotes "
It is strongly recommended to use double quotes at the use of variable containing a string of characters.

simple quotes '
 The single quotes have the same behavior as double quotes, but they inhibit the expansion of variables.

Back-quotes `
They allow to run a command in a sub-shell.


RE 字符意义与范例
^word意义:待搜寻的字串(word)在行首!
范例:搜寻行首为 # 开始的那一行,并列出行号
grep -n '^#' regular_express.txt
word$意义:待搜寻的字串(word)在行尾!
范例:将行尾为 ! 的那一行列印出来,并列出行号
grep -n '!$' regular_express.txt
.意义:代表『一定有一个任意字节』的字符!
范例:搜寻的字串可以是 (eve) (eae) (eee) (e e), 但不能仅有 (ee) !亦即 e 与 e 中间『一定』仅有一个字节,而空白字节也是字节!
grep -n 'e.e' regular_express.txt
\意义:跳脱字符,将特殊符号的特殊意义去除!
范例:搜寻含有单引号 ' 的那一行!
grep -n \' regular_express.txt
*意义:重复零个到无穷多个的前一个 RE 字符
范例:找出含有 (es) (ess) (esss) 等等的字串,注意,因为 * 可以是 0 个,所以 es 也是符合带搜寻字串。另外,因为 * 为重复『前一个 RE 字符』的符号, 因此,在 * 之前必须要紧接著一个 RE 字符喔!例如任意字节则为 『.*』 !
grep -n 'ess*' regular_express.txt
[list]意义:字节集合的 RE 字符,里面列出想要撷取的字节!
范例:搜寻含有 (gl) 或 (gd) 的那一行,需要特别留意的是,在 [] 当中『谨代表一个待搜寻的字节』, 例如『 a[afl]y 』代表搜寻的字串可以是 aay, afy, aly 即 [afl] 代表 a 或 f 或 l 的意思!
grep -n 'g[ld]' regular_express.txt
[n1-n2]意义:字节集合的 RE 字符,里面列出想要撷取的字节范围!
范例:搜寻含有任意数字的那一行!需特别留意,在字节集合 [] 中的减号 - 是有特殊意义的,他代表两个字节之间的所有连续字节!但这个连续与否与 ASCII 编码有关,因此,你的编码需要配置正确(在 bash 当中,需要确定 LANG 与 LANGUAGE 的变量是否正确!) 例如所有大写字节则为 [A-Z]
grep -n '[A-Z]' regular_express.txt
[^list]意义:字节集合的 RE 字符,里面列出不要的字串或范围!
范例:搜寻的字串可以是 (oog) (ood) 但不能是 (oot) ,那个 ^ 在 [] 内时,代表的意义是『反向选择』的意思。 例如,我不要大写字节,则为 [^A-Z]。但是,需要特别注意的是,如果以 grep -n [^A-Z] regular_express.txt 来搜寻,却发现该文件内的所有行都被列出,为什么?因为这个 [^A-Z] 是『非大写字节』的意思, 因为每一行均有非大写字节,例如第一行的 "Open Source" 就有 p,e,n,o.... 等等的小写字
grep -n 'oo[^t]' regular_express.txt
\{n,m\}意义:连续 n 到 m 个的『前一个 RE 字符』
意义:若为 \{n\} 则是连续 n 个的前一个 RE 字符,
意义:若是 \{n,\} 则是连续 n 个以上的前一个 RE 字符!
范例:在 g 与 g 之间有 2 个到 3 个的 o 存在的字串,亦即 (goog)(gooog)
grep -n 'go\{2,3\}g' regular_express.txt
*注意 ^ 和[^]的区别!

Globbing
[:alnum:]代表英文大小写字节及数字,亦即 0-9, A-Z, a-z
[:alpha:]代表任何英文大小写字节,亦即 A-Z, a-z
[:blank:]代表空白键与 [Tab] 按键两者
[:cntrl:]代表键盘上面的控制按键,亦即包括 CR, LF, Tab, Del.. 等等
[:digit:]代表数字而已,亦即 0-9
[:graph:]除了空白字节 (空白键与 [Tab] 按键) 外的其他所有按键
[:lower:]代表小写字节,亦即 a-z
[:print:]代表任何可以被列印出来的字节
[:punct:]代表标点符号 (punctuation symbol),亦即:" ' ? ! ; : # $...
[:upper:]代表大写字节,亦即 A-Z
[:space:]任何会产生空白的字节,包括空白键, [Tab], CR 等等
[:xdigit:]代表 16 进位的数字类型,因此包括: 0-9, A-F, a-f 的数字与字节

EXEMPLE:
grep [-A] [-B] [--color=auto] '搜寻字串' filename
选项与参数:
-A :后面可加数字,为 after 的意思,除了列出该行外,后续的 n 行也列出来;
-B :后面可加数字,为 befer 的意思,除了列出该行外,前面的 n 行也列出来;
--color=auto 可将正确的那个撷取数据列出颜色 
用grep命令查找非小写a-i开头,且有 p的句子  
 





用grep查找以非小写字母开头且有字母p的句子
grep -v '^$'   意思是不要空白行。
.* 表示零个或多个任意字节在g 和g 之间

 
 


Saturday, 26 September 2015

Shell pattern

Kernel is the core which is an entity that is in charge of handling users requests allowing them to access hardware resources and services provided by system.
And Shell is a program offering the user a simple interface enabling it to transmit its requests to the system kernel with commands addressed to the core.

Variable:

In shell, there are two types of variables:
• variables that are internal to the shell, which can only be used in the current shell and you can create as explained above;

• environment variables, which are used in the current shell as well as prospective sub-shells.

In shell script:
$? Return value of the last executed command
$# Argument count of the script/function
$0 Script/function name. Shell dependent
$n Value of the nth parameter of the script/function (1 <= n <= 9)
$@ List all the arguments without any substitution
$* List all the arguments, as one word
$$ Current shell PID
$! Last background started task PID


Some environment variables:

 $PATH ----List of directories where the shell is empowered to search the executable.
$LD_LIBRARY_PATH----List of directories where libraries loader is entitled to seed the necessary dynamic libraries the program.
$USER----Name of the current user.
$HOSTTYPE----System architecture.
$PS1----User prompt configuration.
$TERM----Current terminal type.
Use env command to list all the environment variable.

Regular expressions


Builtins
The builtins are commands integrated to the shell and executed by the shell itself.
To check whether a command is a builtin or not, we can use the builtin builtin.

Quoting
double quotes "
It is strongly recommended to use double quotes at the use of variable containing a string of characters.

simple quotes '
 The single quotes have the same behavior as double quotes, but they inhibit the expansion of variables.

Back-quotes `
They allow to run a command in a sub-shell.

Regular expressions

Regular expressions, called regexp or regex, are an essential support for many UNIX.
A regular expression is a pattern that describes a set of strings. Regular expressions are constructed as arithmetic: they use different operators to combine smaller expressions.

Basic regex
ABCD =~ ABCD    = match
abcd =~ ABCD     = mismatch
xyzABCDxyz =~  ABCD = match

The dot: represents one instance of any character.
AxB =~ A.B  =>match

Start ^ and end $: string. For example ^a means ”any string beginning with ’a’”; b$ means ”any string ending with ’b’”.

Alternative: The character |, preceded by a backslash \, represents an alternative between two elements.


Lists are shortcuts for alternatives: instead of writing a\|b\|c, we can write [abc].

If the first character of the list is ^, the meaning of the list is reversed and becomes ”all except one of the character list”: