"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird
搜寻特定字符串:
[iyunv@www ~]# grep -n 'the' regular_express.txt
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world is the same with "glad".
18:google is the best tools for search keyword.
反向选择,可以使用 'v' 选项:
[iyunv@www ~]# grep -vn 'the' regular_express.txt
则所有包含 ‘the’ 的行都不会显示
如果不需要区分大小写,可以使用 ‘i’ 选项:
[iyunv@www ~]# grep -in 'the' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
12:the symbol '*' is represented as start.
14:The gd software is a library for drafting programs.
15:You are the best is mean you are the no. 1.
16:The world is the same with "glad".
18:google is the best tools for search keyword.
利用中括号 [] 来搜寻集合字节
[iyunv@www ~]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
如果需要匹配某一部分,但是不想匹配以某些开头的情况:(匹配oo,但不匹配以g开头的情况)
[iyunv@www ~]# grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!
但是我的机器上(使用ubuntu11.04),下面这种情况不能匹配成功:
[iyunv@www ~]# grep -n '[^a-z]oo' regular_express.txt 3:Football game is not use feet only. #本来该有这样的输出,但是却无输出
转而使用下面的命令则成功:
[iyunv@www ~]# grep -n '[^[:lower:]]oo' regular_express.txt
3:Football game is not use feet only.
行首与行尾字节 ^ $
[iyunv@www ~]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.
匹配以小写字母开头的行:
[iyunv@www ~]# grep -n '^[a-z]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.
匹配非大小写字母开头的行:(很纳闷,这里使用 [^a-zA-Z] 为什么就能成功?)
[iyunv@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird 注意:那个 ^ 符号,在字节集合符号(括号[])之内与之外是不同的! 在 [] 内代表“反向选择”,在 [] 之外则代表定位在行首的意义!
匹配空行:
[iyunv@www ~]# grep -n '^$' regular_express.txt
22:
任意一个字节 . 与重复字节 *
[iyunv@www ~]# grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
在正则表达式中‘.’ 0个或1个任意字符,‘*’代表任意多个任意字符
限定连续 RE 字符范围 {}
[iyunv@www ~]# grep -n 'go\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
上面的式子匹配了2个‘o’,表达式的意思是匹配2-5个‘o’
若第二个参数为空,则匹配 第一个参数到无穷多个的情况:
[iyunv@www ~]# grep -n 'go\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
将基础的正规表示法特殊字符汇整如下:
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
意义:字节集合的 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]
意义:若是 \{n,\} 则是连续 n 个以上的前一个 RE 字符! 范例:在 g 与 g 之间有 2 个到 3 个的 o 存在的字串,亦即 (goog)(gooog)
grep -n 'go\{2,3\}g' regular_express.txt
sed 工具:
[iyunv@www ~]# sed [-nefr] [动作]选项与参数:
-n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN
的数据一般都会被列出到萤幕上。但如果加上 -n 参数后,则只有经过
sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令列模式上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内, -f filename 则可以运行 filename 内的
sed 动作;
-r :sed 的动作支持的是延伸型正规表示法的语法。(默认是基础正规表示法语法)
-i :直接修改读取的文件内容,而不是由萤幕输出。
动作说明: [n1[,n2]]function
n1, n2 :不见得会存在,一般代表『选择进行动作的行数』,举例来说,如果我的动作
是需要在 10 到 20 行之间进行的,则『 10,20[动作行为] 』
function 有底下这些咚咚:
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :列印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配