linux cp 模糊匹配,Linux常用基本命令:grep-从文件或者管道中筛选匹配的行

grep命令

作用:从文本文件或管道数据流中筛选匹配的行及数据,配合正则表达式一起使用,功能更加强大。

格式:

grep [options] [pattern] [file]

1,匹配包含"ghostwu"的行

ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt1my name is ghostwu2how are you3fine think you4My name is Ghostwu5 what's your name?

6my name is ghostwu27ghostwu@dev:~/linux/grep$ grep "ghostwu"ghostwu.txt

my name is ghostwu

my name is ghostwu2

2,-v: 不包含,相当于取反

ghostwu@dev:~/linux/grep$ grep -v "ghostwu"ghostwu.txt

how are you

fine think you

My name is Ghostwu

what's your name?

ghostwu@dev:~/linux/grep$

3,-n 添加行号

ghostwu@dev:~/linux/grep$ grep -n "ghostwu"ghostwu.txt1:my name is ghostwu6:my name is ghostwu2

ghostwu@dev:~/linux/grep$ grep -vn "ghostwu"ghostwu.txt2:how are you3:fine think you4:My name is Ghostwu5:what's your name?

7:

4,-E,使用扩展的egrep命令,模式中可以用正则表达式

ghostwu@dev:~/linux/grep$ catghostwu.txt

my name is ghostwu

how are you

fine think you

My name is Ghostwu

what's your name?

my name is ghostwu2

ghostwu@dev:~/linux/grep$ grep -E "my|your"ghostwu.txt

my name is ghostwu

what's your name?

my name is ghostwu2

ghostwu@dev:~/linux/grep$ grep -Ev "my|your"ghostwu.txt

how are you

fine think you

My name is Ghostwu

ghostwu@dev:~/linux/grep$ grep -En "my|your"ghostwu.txt1:my name is ghostwu5:what's your name?

6:my name is ghostwu2

5,-i选项,不区分大小写

ghostwu@dev:~/linux/grep$ grep "ghostwu"ghostwu.txt

my name is ghostwu

my name is ghostwu2

ghostwu@dev:~/linux/grep$ grep -i "ghostwu"ghostwu.txt

my name is ghostwu

My name is Ghostwu

my name is ghostwu2

6,-c :统计匹配的行数,不是匹配字符串的次数

ghostwu@dev:~/linux/grep$ grep -c "ghostwu"ghostwu.txt2ghostwu@dev:~/linux/grep$ grep -ci "ghostwu"ghostwu.txt3

ghostwu@dev:~/linux/grep$ grep -c "ghostwu"ghostwu.txt2ghostwu@dev:~/linux/grep$ grep "ghostwu"ghostwu.txt

my name is ghostwu,niceto meet you,ghostwu

my name is ghostwu2

ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt1 my name is ghostwu, niceto meet you,ghostwu2how are you3fine think you4My name is Ghostwu5 what's your name?

6my name is ghostwu27

7,-o: 只输出匹配到的字符串

ghostwu@dev:~/linux/grep$ grep -o "ghostwu"ghostwu.txt

ghostwu

ghostwu

ghostwu@dev:~/linux/grep$ grep -oi "ghostwu"ghostwu.txt

ghostwu

Ghostwu

ghostwu

8,-w: 只匹配过滤的单词,类似于精确匹配

ghostwu@dev:~/linux/grep$ grep -w "ghostwu"ghostwu.txt

my name is ghostwu,niceto meet you,ghostwu

ghostwu@dev:~/linux/grep$ grep -wi "ghostwu"ghostwu.txt

my name is ghostwu,niceto meet you,ghostwu

My name is Ghostwu

ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt1 my name is ghostwu, niceto meet you,ghostwu2how are you3fine think you4My name is Ghostwu5 what's your name?

6my name is ghostwu27

9,常用的一招小技巧,去除文件的注释和空行,在运维中,可以用这条命令把配置文件的空行和注释去掉,然后用管道生成。这样配置文件比较容易查看和配置

ghostwu@dev:~/linux/grep$ grep -Ev "^$|#"ghostwu.php<?php

class Person {

public $name= 'ghostwu';

public $age= 20;

publicfunctionshowinfo(){echo $this->name . PHP_EOL;echo $this->age. PHP_EOL;

}

}

ghostwu@dev:~/linux/grep$ cat -n ghostwu.php1 <?php2

3class Person {4

5#人名6 public $name = 'ghostwu';7

8#年龄9 public $age = 20;10

11#显示信息12 public functionshowinfo(){13 echo $this->name . PHP_EOL;14 echo $this->age. PHP_EOL;15}16 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值