-
flowlist.txt 文件:
1501010001 71.052141618 3 07/11/15 21:46:57 1006 User001 109 4 303 1501010002 865.86 1686831 2 05/11/15 03:52:44 1009 User009 109 1 302 1501010003 652.61 2587675 2 07/14/15 19:17:39 1010 User003 110 1 305 1501010004 905.24 1282788 1 04/17/15 17:14:09 1010 User004 102 3 304 1501010005 444.25 1624680 1 04/05/15 11:40:51 1005 User009 108 1 308 1501010006 48.212473714 3 01/18/15 23:45:51 1001 User003 110 2 302 1501010007 396.26 2512994 4 08/07/15 05:55:16 1005 User009 102 2 308 1501010008 690.74 1259159 2 08/06/15 02:48:20 1004 User001 104 4 304 1501010009 122.37 2462139 1 04/27/15 08:19:22 1008 User008 105 2 310
-
grep 命令格式:
grep [参数] 查找的内容 [文件]
-
例子:查找所有以 txt 结尾的文件中存在字符串的记录并显示到控制台。
[root@localhost test]# grep "User009" *.txt 1501010002 865.86 1686831 2 05/11/15 03:52:44 1009 User009 109 1302 1501010005 444.25 1624680 1 04/05/15 11:40:51 1005 User009 108 1308 1501010007 396.26 2512994 4 08/07/15 05:55:16 1005 User009 102 2308
-
例子:查找存在字符串的行数。
[root@localhost test]# grep -c "1005" flowlist.txt 2
-
例子:查找存在字符串的记录及行号。
[root@localhost test]# grep -n "1005" flowlist.txt 5:1501010005 444.25 1624680 1 04/05/15 11:40:51 1005 User009 108 1308 7:1501010007 396.26 2512994 4 08/07/15 05:55:16 1005 User009 102 2308
-
例子:显示不包含条件的记录。
[root@localhost test]# grep -v "1005" flowlist.txt 1501010001 71.05 2141618 3 07/11/15 21:46:57 1006 User001 109 4303 1501010002 865.86 1686831 2 05/11/15 03:52:44 1009 User009 109 1302 1501010003 652.61 2587675 2 07/14/15 19:17:39 1010 User003 110 1305 1501010004 905.24 1282788 1 04/17/15 17:14:09 1010 User004 102 3304 1501010006 48.21 2473714 3 01/18/15 23:45:51 1001 User003 110 2302 1501010008 690.74 1259159 2 08/06/15 02:48:20 1004 User001 104 4304 1501010009 122.37 2462139 1 04/27/15 08:19:22 1008 User008 105 2310
-
例子:精确查找符合条件的记录。
[root@localhost test]# grep "303" flowlist.txt 1501010001 71.05 2141618 3 07/11/15 21:46:57 1006 User001 109 4 303
-
例子:忽略大小写查询。
[root@localhost test]# grep -i "user001" flowlist.txt 1501010001 71.05 2141618 3 07/11/15 21:46:57 1006 User001 109 4303 1501010008 690.74 1259159 2 08/06/15 02:48:20 1004 User001 104 4304
-
例子:将查询结果通过管道继续查询符合条件的记录。
[root@localhost test]# grep "User001" flowlist.txt | grep "304" 1501010008 690.74 1259159 2 08/06/15 02:48:20 1004 User001 104 4304
-
例子:将查询结果输出到文件。
[root@localhost test]# grep "304" flowlist.txt > file_304.txt [root@localhost test]# cat file_304.txt 1501010004 905.24 1282788 1 04/17/15 17:14:09 1010 User004 102 3304 1501010008 690.74 1259159 2 08/06/15 02:48:20 1004 User001 104 4304
-
例子:查询用户信息。
[root@localhost test]# grep "hadoop01" /etc/passwd hadoop01:x:500:500:yarn:/home/hadoop01:/bin/bash [root@localhost test]# grep hadoop01 /etc/passwd hadoop01:x:500:500:hadoop01:/home/hadoop01:/bin/bash
-
例子:grep 命令不仅可以在文件查找符合条件的行,也可在字符串中查找符合条件的子串。
[root@localhost test]# echo "Hello Linux" | grep in Hello Linux [root@localhost test]# echo "I Study Hadoop" | grep "doo" I Study Hadoop