当前位置: 首页 > wzjs >正文

无锡网络公司无锡网站推广汕头cms模板建站

无锡网络公司无锡网站推广,汕头cms模板建站,设计师接私活的网站,天琥设计培训学校官网目录 基础正则表达式 1:基础正则表达式示例 (4)查找任意一个字符“.”与重新字符“*” (5)查找连续字符范围“{ }” 文本处理器 一、sed工具 二、awk工具 (1)按行输出文本 (2&#xff0…

目录

基础正则表达式

1:基础正则表达式示例

(4)查找任意一个字符“.”与重新字符“*”

(5)查找连续字符范围“{ }”

文本处理器

一、sed工具

二、awk工具

(1)按行输出文本

(2)按字段输出文本

(3)通过管道、双引号调用 she11 命令


基础正则表达式

1:基础正则表达式示例

    下面的操作需要提前准备一个名为 test.txt 的测试文件,文件具体内容如下所示:

[root@localhost ~]# cat test.txt
he was short and fat, He was wearing a blue polo shirt with black pants. The home
of Football on BBc Sport online.
the tongue is boneless but it breaks bones.12!google is the best tools for search keyword, The year ahead will test our politicalestablishment to the limit. P3 141592653589793238462643383249901429a wood cross!
Actions speak louder than words
#N0ood #
#N000o0ood #
Axyzxуzxyzxy2C
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

(1)查找特定字符

[root@localhost w]# grep -n"the" test.txt
4:the tongue is boneless but it breaks bones.12!
5:google is the best tools for search keyword, 6:The year ahead will test our politicalestablishment to the limit.
Iroot@localhost w]# grep in "the' test.txt
3:The home of Football on 8Bc Sport online. 4:the tongue is boneless but it breaks
bones.121
5:google is the best tools for search keyword, 6:The year ahead will test our political
establishment to the limit.
若反向选择,如查找不包含“the”字符的行,则需要通过 grep 命令的“-v"选项实现,并配合“-n”·起使用显示行号。
[rootglocalhost ~]# grep -vn "the' test.txt
1:he was short and fat, 2:He was wearing a blue polo shirt with black pants. 3:Thehome of football on BBc Sport online,7:P-3.1415926535897932384626433832499014298:a wood cross!
9:Actions speak louder than words
10:
11:#woood #
12:#wo0oo0o0d #
13:Axyzxyzxyzxy2C
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single, 16:I shouldn't have lett so tast.

(2)利用中括号“[ ]”l来查找集合字符

[rootglocalhost ~]# grep -n 'sh[io]rt" test.txt1:he was short and fat, 2:He was wearing a blue polo shirt with black pants.
若要查找包含重复单个字符“oo"时,只需要执行以下命令即可
[rootglocalhost ~]# grep -n 'oo' test.txt
3:The home of Football on 8Bc Sport online, 5:google is the best tools for search
keyword. 8:a wood cross!
11:#woood #
12:#wo0oocood #
14:I bet this place is really spooky late at night!
若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反向选择“[^]”来实现该目的。例如执行"grep -n'[^w]oo"test.txt"命令表示在 test.txt 文本中査找“oo"前面不是“w”的字符串。
[rootglocalhost ~]# grep -n'[^w]oo' test.txt3:The home of Football on BBc Sport online. 5:google is the best tools for searchkeyword. 11:#woood #
12:#wo0o00o0d #
14:I bet this place is really spooky late at night!
若查找“oo”前面不是“w”的字符串,只需要通过集合字符的反向选择“[^]”来实现该目的。例如执行“grep -n'[^w]oo"test.txt"命令表示在 test.txt 文本中査找“oo”前面不是“w”的字符串
[root@localhost ~]# grep -n'['w]oo' test.txt
3:The home of Football on BBc Sport online. 5:google is the best tools for searchkeyword, 11:#woood #
12:#ho0o00o0d #
14:I bet this place is really spooky late at night!
在上述命令的执行结果中发现“woood”与"woo0oood"也符合匹配规则,二者均包含“w”。其实通过执行结果就可以看出,符合匹配标准的字符加粗显示,而上还结果中可以得知。“#woood #”中加粗显示的是“o0o”,而“oo”前面的“o”是符合匹配规则的。同理“#woooo0ood #”也符合匹配规则。若不希望“oo”前而存在小写字母,可以使用“grep -n'[^a-z]oo'test.txt”命令实现,其中 a-z”表示小写字母,大写字母则通过“A-2”表示。
[rootglocalhost ~]# erep -n '[^a-z]oo" test.txt
3:The home of Football on BBc sport online.
查找包含数字的行可以通过“grep -n'[0-9]’test.txt”命令来实现。
[rootglocalhost ~]# grep -n '[8-9]" test.txt
4:the tongue is boneless but it breaks bones.12!
7:P1-3.141592653589793238462643383249901429

(3)查找行首“^”与行尾字符“$”

[root@localhost ~]# grep -n '^the' test.txt
4:the tongue is boneless but it breaks bones.12!
查询以小写字母开头的行可以通过“^[a-z]"规则来过滤,查询大写字母开头的行则使用“^[A-Z]“规若查询不以字母开头的行则使用“^[^a-zA-2]”规则。
[rootglocalhost ~]# grep -n '^[a-z]’ test.txt1:he was short and fat, 4:the tongue is boneless but it breaks bones.12!5:google is the best tools for search keyword, 8:a wood cross!
[root@localhost ~]# grep -n'^[A-2]' test.txt2:He was wearing a blue polo shirt with black pants, 3:The home of Football on 88CSport online. 6:The year ahead will test our political establishment to the limit.7:PI-3.1415926535897932384626433832499014299:Actions speak louder than words
13:AxyzxYzxy2xy2C
14:I bet this place is really spooky late at night!
15:Misfortunes never come alone/single. 16:I shouldn't have lett so tast.
[root@localhost ~]# grep -n'^[^a-zA-2]" test.txt
11:#woood #
12:#wo0o00o0d #
[root@localhost ~]# grep -n'.$"test.txt
1:he was short and fat, 2:He was wearing a blue polo shirt with black pants. 3:Thehome of Football on BBc Sport online. 5:google is the best tools for search keyword.6:The year ahead will test our political establishment to the limit. 15:Misfortunesnever come alone/single, 16:I shouldn't have lett so tast.
当查询空白行时,执行“grep -n'ns"test.txt”命令即可,
[root@localhost ~]# grep -n'^g'test.txt106
(4)查找任意一个字符“.”与重新字符“*”
[root@localhost ~]# grep -n 'w..d' test.txt5:google is the best tools for search keyword,8:a wood cross!
9:Actions speak louder than words
12:#wo0o0co0d #
14:I bet this place is really spooky late at night!
[rootglocalhost ~]# grep -n 'ooo*' test.txt3:The home of Football on BBc Sport online. 5:google is the best tools for searchkeyword, 8:a wood cross!
11:#wo00d #
12:#wo0oo0ood #
14:I bet this place is really spooky late at night!
查询以 w 开头 d结尾,中间包含至少一个 o 的字符串,执行以下命令即可实现。[rootglocalhost ~]# grep -n'woo*d" test.txt
8:a wood cross!
11:#woood #
12:#woooo0ood #
执行以下命令即可查询以 w开头 d 结尾,中间的字符可有可无的字符申,
[root@localhost ~]# erep -n 'w.*d' test.txt1:he was short and fat. 5:google is the best tools for search keyword. 8:a wood cross!9:Actions speak louder than words
11:#woood #
12:#wo0oocood #
执行以下命令即可查询任意数字所在行。
[rootglocalhost ~]# grep -n'[8-9][8-9].' test.txt4:the tongue is boneless but it breaks bones.12!
7:PI 3.141592653589793238462643383249901429

(5)查找连续字符范围“{ }”

查询两个0的字符,

[root@localhost ~]# grep -n'o{2}' test.txt
3:The home of football on BBc Sport online. 5:google is the best tools for searchkeyword. 8:a wood cross!
11:#wo0od #
12:#wo0000o0d #
14:I bet this place is really spooky late at night!


查询以 w 开头以 d 结尾,中间包含 2~5 个0的字符串,

[rootglocalhost ~]# grep -n 'wo'{2,5'}d" test.txt
B:a wood cross!
11:#wo0od #


查询以 w 开头以 d 结尾,中间包含 2个或 2 个以上0的字符串。
 

[rootglocalhost ~]# grep n 'woW2,\}d' test.txt
B:a wood cross!
11:#wo0od #
12:#wo000co0d #

元字符总结
字符说明
\将下一个字符标记为一个特殊字符、或一个原义字符、或一个向后引用、或一个八进制转义符;
^

匹配输入字符串的开始位置;

$匹配输入字符串的结束位置;
*匹配前面的子表达式零次或多次;
+匹配前面的子表达式一次或多次;
?匹配前面的子表达式零次或一次;
.

匹配除换行符( \n、\r )之外的任何单个字符;

[a-z]字符范围。匹配指定范围内的任意字符;
{n}

n是一个非负整数,匹配确定的n次;

{n,}

n是一个非负整数,至少匹配n次;

{n,m}

m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次

\d

匹配一个数字字符。等价于【0~9】;

\D

匹配一个非数字字符。等价于【^0~9】;

\s

匹配任何空白字符,包括空格、制表符、换页符等等。等价于【^、\f、\n、\r、\t、\v】;

\S

匹配任何非空白字符。等价于【A~Z、a~z、0~9】;

\w匹配字母、数字、下划线。等价于`【A~Z、a~z、0~9、_】`;
\W匹配非字母、数字、下划线。等价于`【^A~Z、a~z、0~9、_】`;
\n匹配一个换行符
\f匹配一个换页符
\r匹配一个回车符

文本处理器

一、sed工具

   sed工具是一个强大而简单的文本解析转换工具,可以读取文本,并根据指定的条件对文本内容进行编辑(删除、替换、添加、移动等),最后输出所有行或者仅输出处理的某些行。

   它也可以在无交互的情况下实现相当复杂的文本处理操作,被广泛应用于 shel1 脚本中,用以完成各种自动化处理任务。工作流程主要包括读取、执行和显示三个过程。以下为详细介绍:

  • 读取:sed 从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的缓冲区中。
  • 执行:默认情况下,所有的 sed 命令都在模式空间中顺序地执行,除非指定了行的地址,否则 sed命令将会在所有的行上依次执行。
  • 显示:发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。

    格式如下: 

sed [选项] `操作` 参数
sed [选项] -f scriptfile 参数

    常见的 sed 命令选项主要包含以下几种:

  • -e或--expression=:表示用指定命令或者脚本来处理输入的文本文件
  • -f 或--file=:表示用指定的脚本文件来处理输入的文本文件。
  • -h或--help:显示帮助。
  • -n、--quiet 或 silent:表示仅显示处理后的结果。
  • -i:直接编辑文本文件。

     常见的操作包括以下几种:

  • a:增加,在当前行下面增加一行指定内容。
  • c:替换,将选定行替换为指定内容。
  • d:删除,删除选定的行。
  • i:插入,在选定行上面插入一行指定内容。
  • P:打印,如果同时指定行,表示打印指定行;如果不指定行,则表示打印所有内容;如果有非
  • 打印字符,则以 ASCII 码输出。其通常与“-n”选项一起使用。
  • s:替换,替换指定字符。
  • y:字符转换。

(1)输出符合条件的文本(p 表示正常输出)

[root@localhost ~]# sed -n 'p' test.txt
//输出所有内容,等同于 cat test.txt
he was short and fat, He was wearing a blue polo shirt with black pants. The homeof Footbal1on BBc sport online.
.….//省略部分内容
[root@localhost ~]# sed -n'3p' test.txt
//输出第 3 行
The home of Football on BBc Sport online.
[root@localhost ~]# sed -n '3,5p'test.txt//输出 3~5 行
The home of Football on BBc Sport online.the tongue is boneless but it breaks bones.12!google is the best tools for search keyword,
[root@localhost ~]# sed -n 'p;n' test.txt
//输出所有奇数行,n 表示读入下一行资料
he was short and fat, The home of football on BBc Sport online, google is the besttools for search keyword.…//省略部分内容
[root@localhost ~]# sed -n 'n;p' test.txt
//输出所有偶数行,n 表示读入下一行资料
He was wearing a blue polo shirt with black pants.
the tongue is boneless but it breaks bones.12!
The year ahead will test our political establishment to the limit.….//省略部分内容
[root@localhost ~]# sed -n'1,5{p;n}" test.txt//输出第 1~5 行之间的奇数行(第 1、3、 5 行)he was short and fat. The home of Football on BBc sport online, google is the besttools for search keyword.
[root@localhost ~]# sed -n'10,${n;p}' test.txt//输出第 18 行至文件尾之间的偶数行
#woood #
AxyzxуzxуzxyzC
Misfortunes never come alone/single.

(2)删除符合条件的文本

[root@localhost ~]# nl test.txt sed '3d'    //删除第 3 行
1 he was short and fat, 2 He was wearing a blue polo shirt with black pants. 4 thetongue is boneless but it breaks bones.12!
5 google is the best tools for search keyword. 6 The year ahead will test our politicalestablishment to the limit.…
//省略部分内容 
[root@localhost ~]# nl test.txt |sed '3,5d'
//删除第 3~5 行
1 he was short and fat, 2 He was wearing a blue polo shirt with black pants. 6 Theahead will test our politicalestablishment to the limit.yearPI 3.141592653589793238462643383249901429
8 a wood cross!...
//省略部分内容
[root@localhost ~]# nl test.txt sed '/cross/d'//删除包含 cross 的行,原本的第 8 行被删除;如果要删除不包含 cross 的行,用!符号表示取反操作,如'/cross/!d’…
//省略部分内容
7 PI-3.141592653589793238462643383249901429
9 Actions speak louder than words .
//省略部分内容
[root@localhost ~]# sed "/^[a-z]/d' test.txt
//删除以小写字母开头的行
He was wearing a blue polo shirt with black pants, The home of football on BBc Sportonline, The year ahead will test our political establishment to the limit.P-3.141592653589793238462643383249901429
Actions speak louder than words
#woood #
#w000o0ood #
Axyzxyzxyzxy2C
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.
[root@localhost ~]# sed "/\.$/d' test.txt
//删除以"."结尾的行
the tongue is boneless but it breaks bones.12!PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood #
#wocoo0ood 并
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
[root@localhost ~]# sed "/^$/d' test.txt
//删除所有空行
he was short and fat, He was wearing a blue polo shirt with black pants. The homeof Football on BBc sport online.
the tongue is boneless but it breaks bones.12!google is the best tools for search keyword, The year ahead will test our politicalestablishment to the limit. p 3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words
#woood 并
#W000000od #
Axyzxyzxyzxy2C
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

 (3)替换符合条件的文本

在使用 sed 命令进行替换操作时需要用到 s(字符申替换)、c(整行/整块营换)、y(字符转换)命令选项,常见的用法如下所示。
sed 's/the/THE/" test.txt
//将每行中的第一个 the 替换为 THE
sed 's/l/L/2'test.txt
//将绿行中的第 2 个 1 替换为L
sed 's/the/THE/g  test.txt
//将文件中的所有 the 普换为 THE
sed 's/o/'g' test.txt
//将文件中的所有 o 删除(替换为空申)
sed 's/^/#/' test.txt
//在绿行行首插入#号
sed '/the/s/^/#/' test.txt
//在包含 the 的每行行首插入#号
sed 's/$/E0F/" test.txt
//在每行行尾插入字符申EOF
sed '3,5s/the/THE/e' test.txt
//将第 3~5 行中的所有 the 替换为 THE
郑州课
sed '/the/s/o/o/g  test.txt
//将包含 the 的所有行中的 。 都普换为 0

(4)迁移符合条件的文本

在使用 sed 命令迁移符合条件的文本时,常用到以下参数:
> H:女制到剪贴板;
g、G:将剪贴板中的数据覆盖/追加至指定行:
w:保存为文件;
>r:读取指定文件;
a:追加指定内容。。
具体操作方法如下所示。
关
sed '/the/(H;d};$6' test.txt
//将包含 the 的行迁移至文件末尾,{;}用于多个操作
sed '1,5(H;d};176' test.txt
//将第 1~5 行内容转移至第 17 行后
sed '/the/w out.file" test.txt
//将包含 the 的行另存为文件 out.file
sed '/the'r /etc/hostname" test.txt
//将文件/etc/hostname 的内容添加到包含 the 的行以后
sed '3aNew" test.txt
//在第 3 行后插入一个新行,内容为 New
sed '/the/aNew' test.txt
//在包含 the 的每行后插入一个新行,内容为 Wew
sed '3aNewl\nNew2' test.txt
//在第 3 行后插入多行内容,中间的\n 表示换行

二、awk工具

    awk 是一种强大的‌文本处理工具‌,尤其适合处理结构化数据(如日志、CSV文件)。它不仅是命令行工具,还是一种编程语言,能够高效完成数据提取、统计、格式化输出等任务。通常情况下awk所使用的命令格式如下所示:

awk 选项 `模式或条件 {编辑命令}` 文件1 文件2 ...    //过滤并输出文件中符合条件的内容
awk -f 脚本文件 文件1 文件2 ...    //从脚本中调用编辑指令,过滤并输出内容

     其中,单引号加上大括号“{ }”用于设置对数据进行的处理动作。awk可以直接处理目标文件,也可以通过“ -f ”读取脚本对目标脚本进行处理。

特殊的内建变量(可直接使用),如下:

  • FS:指定每行文本的字段分隔符,默认为空格或制表位;
  • NF:当前处理的行的字段个数;
  • NR:当前处理的行的行数(序数);
  • $0:当前处理的行的整行的内容;
  • $n:当前处理的第n个字段(第n列);
  • FILENAME:被处理的文件名;
  • RS:数据记录分隔,默认为\n,即每行为一条记录。

    相对于sed命令,awk则倾向于将一行分成多个“字段” 然后再进行处理,且默认情况下字段的分隔符为空格或Tab键。而且其执行结果也可通过print的功能将字段数据打印显示。在使用awk命令的过程中,可以也使用逻辑操作符“&&”表示“与”、“ || ”表示“或”、“ !”表示“非”。范例如下:

[root@localhost ~]#awk -F ':' `{print $1,$3,$4}` /etc/passwd
root 0 0
bin 1 1
daemon 2 2
...    ...        //省略部分内容

    处理逻辑过程如下:

 用法示例:

(1)按行输出文本

按行输出文本
awk '{print}' test.txt
//输出所有内容,等同于 cat test.txt
awk '{print $o}' test.txt
//输出所有内容,等同于 cat test.txt
awk 'NR==1,NR==3fprint}' test.txt
//输出第 1~3 行内容
awk (NR>=1)&&(NR<=3){print}' test.txt//输出第 1~3 行内容
awk 'NR==1/|NR==3{print}" test.txt//输出第 1 行、第 3 行内容
awk "(NR%2)==1{print}'test.txt
//输出所有奇数行的内容
awk "(NR%2)==0{print}' test.txt
//输出所有偶数行的内容
awk "/^root/{print}'/etc/passwd
//输出以 root 开头的行

awk 'BEGIN {x=0};/\/bin\/bash$/{x++};END {print x}' /etc/passwd

//统计以/bin/bash 结尾的行数,等同于 grep-c"/bin/bash$"/etc/passwd
awk BEGIN{RS=""};END{print NR}" /etc/squid/squid.conf

//统计以空行分隔的文本段落数

(2)按字段输出文本

awk '{print $3}' test.txt
//输出每行中(以空格或制表位分隔)的第 3 个字段
awk '{print $1,$3}'test.txt
//输出每行中的第 1、3 个字段
awk -F ":"'$2==""{print}'/etc/shadow//输出密码为空的用户的 shadow 记录
aWk 'BEGIN {FS=":"};$2==""{print}' /etc/shadow//输出密码为空的用户的 shadow 记录
awk -F":"'$7~"/bash"{print $1}" /etc/passwd//输出以冒号分隔且第 7个字段中包含/bash 的行的第 1 个字段
awk '($1~"nfs")&&(NF==8){print $1,$2}' /etc/services//输出包含 8 个字段且第 1 个字段中包含 nfs 的行的第 1、2 个字段
awk -F":"'($7!="/bin/bash")&&($7!="/sbin/nologin"){print}' /etc/passwd//输出第7个字段既不为/bin/bash 也不为/sbin/nologin 的所有行 

(3)通过管道、双引号调用 she11 命令

awk -F:'/bash$/{print|"wc -l"}'/etc/passwd//调用 wc -1 命令统计使用 bash 的用户个数,等同于 grep -c"bash$"/etc/passwd
awk "BEGIN {while("w"|getline)n++ ;{print n-2}}//调用 w命令,并用来统计在线用户数
awk 'BEGIN("hostname"getline ; print $0}//调用 hostname,并输出当前的主机名

http://www.dtcms.com/wzjs/601280.html

相关文章:

  • 公司网站建设方案详细公司网站设计图片
  • 至少保存十个以上域名网站html个人网页代码模板
  • 桥拓云智能建站长沙短视频代运营公司
  • 网站域名申请步骤crm管理系统架构
  • 网站域名设计推荐开通微信公众号要收费吗
  • 网站开发进度设计uc搜索引擎入口
  • 免费行情软件网站下载大全做游戏都需要什么网站
  • 新做的网站如何备案能源与动力工程
  • 北京的制作网站的公司有哪些大庆黄页查询电话
  • 制作一个有用户网站向公司申请建设网站申请书
  • 公司做企业网站成都h5模板建站
  • 国外儿童社区网站模板下载如何购买一个网站的域名
  • 这么建设新的网站青岛网站排名哪家公司好
  • 杭州网站建设网络公司建网站资料
  • 合肥工程建设信息网站杭州网站开发设计
  • 郑州做网站找绝唯科技网站开发用哪个程序
  • 郑州 外包网站南安住房与城乡建设部网站
  • html怎么做网站设计可以将自己做的衣服展示的网站
  • 中徽园林建设有限公司网站wordpress添加分类目录关键词
  • 科技网站建设+长沙学习电子商务网站建设与管理感想
  • 企业网站关站成都市网络营销
  • 江苏同邦建设有限公司网站网站体验分享
  • 网站用什么格式的图片格式抚州市企业网站建设
  • php网站插件工商注册平台官网
  • 北京seo网站推广费用用自己服务器做网站
  • 公司静态网站模板天津河西做网站贵吗
  • 下列关于网站开发中网页上传网站建设电话销售工作总结
  • 如何制作一个动态的网站的登录详细步骤页面h5手机网站源码下载
  • 网站备案授权门户网站是啥意思
  • 优化网站界面的工具百度怎么发帖子