日志查看
sed -n '/2019-07-30 20:00:00/,/2019-07-30 21:30:00/'p gateway.log
如果没有数据,可以时间宽松到 日 或者 时 或者 分
获取nginx版本号
curl -fsSL https://nginx.org/en/download.html \
| sed -n '/Stable version/,/Legacy versions/p' \
| grep -oE 'nginx-[0-9]+\.[0-9]+\.[0-9]+' \
| awk '!seen[$0]++'
执行的结果
nginx-1.29.1
nginx-1.28.0
nginx-1.26.3
nginx-1.24.0
nginx-1.22.1
nginx-1.20.2
nginx-1.18.0
nginx-1.16.1
nginx-1.14.2
nginx-1.12.2
nginx-1.10.3
nginx-1.8.1
nginx-1.6.3
nginx-1.4.7
nginx-1.2.9
nginx-1.0.15
nginx-0.8.55
nginx-0.7.69
nginx-0.6.39
nginx-0.5.38
解释说明
解释说明:grep -oE 'nginx-[0-9]+\.[0-9]+\.[0-9]+'
grep
文本搜索工具,把“匹配指定模式的行”打印出来
-o
--only-matching 的缩写。
只输出“行里真正匹配到的那一段”,而不是整行。
例如一行里既有 abcnginx-1.26.2xyz ,又有很多别的文字,加 -o 就只会打印 nginx-1.26.2
-E
--extended-regexp 的缩写。
让 grep 使用“扩展正则表达式”,这样我们就可以用 + 、 | 、 () 等高级写法,而不用再加反斜杠转义。
-正则 'nginx-[0-9]+\.[0-9]+\.[0-9]+'
nginx- 字面量,必须出现这 7 个字符。
[0-9] 匹配任意一个数字(0-9)。
+ 前面的字符/集合“出现一次或多次”。
所以 [0-9]+ 就是“连续 1~n 位数字”。
\. 小数点。因为点在正则里表示“任意字符”, 要匹配真正的点必须加反斜杠 \ 转义。
合起来:
nginx- + “1 段或多段数字” + . + “1 段或多段数字” + . + “1 段或多段数字”
正好对应 nginx 的版本号格式,例如
nginx-1.26.2 、 nginx-1.24.0 、 nginx-1.27.3 …
结果去重 awk '!seen[$0]++'
修改文件同时备份
[root@wj 1]# sed -i_bak 's/a/1/' 1.txt
[root@wj 1]# ls
1.txt 1.txt_bak
[root@wj 1]# more 1.txt
1bc
[root@wj 1]# more 1.txt_bak
abc
查找以开头
#查找以<Directory 开头的
[root@wj conf]# sed -n '/^<Directory .*/p' httpd.conf
<Directory />
<Directory "/var/www">
<Directory "/root/data/sharefile">
<Directory "/var/www/cgi-bin">
在上面添加一行
#在以a开头的行前面添加一个行666
[root@wj 1]# more 1.txt
abc
dvf
ghy
[root@wj 1]# sed -i '/^a/i\666' 1.txt
[root@wj 1]# more 1.txt
666
abc
dvf
ghy
---修改---
#案例:修改默认端口,-i就直接修改了原文件
[root@wj ~]# sed -i 's/^#Port .*/Port 1022/' /etc/ssh/sshd_config
#案例:把DocumentRoot "/var/www/html",换成为 DocumentRoot "/root/data/sharefile"
[root@wj conf]# sed -n 's/^DocumentRoot .*/DocumentRoot "\/root\/data\/sharefile"/p' httpd.conf
DocumentRoot "/root/data/sharefile"
#案例:执行多个命令 用-e参数,同时把a变成A,把b换成B
sed -e 's/a/A/;s/b/B/' test.log
#案例:也可也把脚本写成文件,使用文件方式执行,把sed的命令写入文本中,为了方便识别,我们把文件的名字以.sed结尾
[root@wj ~]# more 1.sed
s/a/A/
s/b/B/
[root@wj ~]# sed -f 1.sed test.log
ABcd z34
ABcd 1244tt
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
