3

CentOS 文件管理命令(3)

 2 years ago
source link: https://blog.51cto.com/baicia/5146120
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

文件过滤命令

|:管道符
# 作用:将管道符左边命令的标准输出,交给管道符右边命令的标准输入来处理

grep:过滤输出内容

## 符号
^:以...开头
$:以...结尾
*:匹配*前面的内容至少0次或多次
.:匹配任意字符

## 举例
^:以...开头
[root@localhost ~]# cat shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。
以胶投漆中,谁能别离此?
[root@localhost ~]# grep '^客' shi.txt
客从远方来,遗我一端绮。
[root@localhost ~]# grep '^相' shi.txt
相去万余里,故人心尚尔。

$:以...结尾
root@localhost ~]# grep '。$' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。
[root@localhost ~]# grep '此?$' shi.txt
以胶投漆中,谁能别离此?

*:匹配*前面的内容至少0次或多次
[root@localhost ~]# grep '鸳鸯*' shi.txt
文采双鸳鸯,裁为合欢被。
[root@localhost ~]# grep '**' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。
以胶投漆中,谁能别离此?

.:匹配任意字符
[root@localhost ~]# grep '客.' shi.txt
客从远方来,遗我一端绮。

## 选项
-n:打印行号 number
-A:after 查看过滤内容的后N行
-B:before 查看过滤内容的前N行
-C:center 查看过滤内容的前、后N行
-v:取反
-E:可以过滤多个结果
-o:只打印出过滤内容
-i:不区分大小写

## 举例
-n:打印行号 number
[root@localhost ~]# grep -n '文' shi.txt
3:文采双鸳鸯,裁为合欢被。
[root@localhost ~]# grep -n '谁' shi.txt
5:以胶投漆中,谁能别离此?

-A:after 查看过滤内容的后N行
root@localhost ~]# grep -A 2 '故' shi.txt
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。
[root@localhost ~]# grep -A 1 '为' shi.txt
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。

-B:before 查看过滤内容的前N行
[root@localhost ~]# grep -B 1 '为' shi.txt
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
[root@localhost ~]# grep -B 2 '故' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。

-C:center 查看过滤内容的前、后N行(只能取均值)
[root@localhost ~]# grep -C 1 '故' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
[root@localhost ~]# grep -C 2 '裁' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。
以胶投漆中,谁能别离此?

-v:取反
[root@localhost ~]# grep -v  '裁' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。
著以长相思,缘以结不解。
以胶投漆中,谁能别离此?
[root@localhost ~]# grep -v  '客从远方来,' shi.txt
相去万余里,故人心尚尔。
文采双鸳鸯,裁为合欢被。
著以长相思,缘以结不解。
以胶投漆中,谁能别离此?

-E:可以过滤多个结果
[root@localhost ~]# grep -E  '客从远|相去' shi.txt
客从远方来,遗我一端绮。
相去万余里,故人心尚尔。

-o:只打印出过滤内容
[root@localhost ~]# grep -o  '客从远' shi.txt
客从远
[root@localhost ~]# grep -Eo  '客从远|相去' shi.txt
客从远
相去
[root@localhost ~]# grep -o  '故人' shi.txt
故人

-i:不区分大小写
root@localhost ~]# grep -i b /etc/sysconfig/network-scripts/ifcfg-ens33 
BROWSER_ONLY="no"
BOOTPROTO="none"
IPV6_ADDR_GEN_MODE="stable-privacy"
UUID="3b3331d7-f120-4ff1-92e0-e81c80dcd4fe"
ONBOOT="yes

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK