定义:Grep (Globally search for the reqular expression and print out the line).

好处:Grep 在执行时不需要先调用编辑程序,同时不需要以“//” 来括起规则表达式,所以比vi等快很多。

格式:grep pattern filename1 filename2 .....

术语:pattern, 字符串样式,多被单引号‘ ’包围。

原理:Grep 在文件里查找规则表达式,并打印包含该表达式的所有行。

Practice script

#/bin/bash
echo 'here is a grep script'
echo '======= print the content of employees'
cat employees
#search line including eric
grep eric employees echo '======= grep option practice'
#show line and line number
grep -n '^er' employees
#print bash return, ,,
echo $?
#print line number only
grep -c '^er' employees
#print filename only
grep -l '^er' employees awkfile
#display matching word file and line
grep -w tokoyo employees awkfile
#ignore capital
grep -i tOKoyo employees awkfile
#print unmatch line
grep -v tokoyo employees
#mark module number
grep -b tokoyo employees
#print error only
grep -s tokoyo employees #regular expression practice
echo '======= regular expression practice'
grep frank emp*
grep '^green' employees
grep '12$' employees
grep '939..' employees
grep '^[ej]' employees
grep '^j..[nk]' employees
grep '\<mar.*' employees
grep '\<mar.*12' employees
grep -vin 'ic' employees
grep -l 'ic' employe*

Output of the script

# ./grep.sh
here is a grep script
======= print the content of employees
eric beijing
john xian
mark henan
frank tokoyo
green england
eric beijing
======= grep option practice
:eric beijing employees
employees:frank tokoyo
employees:frank tokoyo
eric beijing
john xian
mark henan
green england
:frank tokoyo
frank tokoyo
======= regular expression practice
frank tokoyo
green england
mark henan
mark henan
eric beijing
john xian
john xian
mark henan
mark henan
:john xian
:mark henan
:frank tokoyo
:green england
employees

q

Grep basic and practice的更多相关文章

  1. PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1078 字符串压缩与解压 (20 分) 凌宸1642 题目描述: 文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一 ...

  2. PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1070 结绳 (25 分) 凌宸1642 题目描述 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下 ...

  3. PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1065 单身狗 (25 分) 凌宸1642 题目描述: "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大 ...

  4. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  5. PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1054 求平均值 (20 分) 题目描述 本题的基本要求非常简单:给定 N 个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的 ...

  6. PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) 目录 PAT (Basic Level) Practice (中文) 1050 螺旋矩阵 (25 分) ...

  7. Awk basic and practice

    定义:Awk是一种程序语言,用来处理数据和产生报告.数据可来自标准输入,文件,管道输出. 格式:#awk '/pattern/ {action}' filename 术语:pattern, 样式是由正 ...

  8. PAT (Basic Level) Practice (中文)1057 数零壹 (20 分) (按行输入带空格的字符串)

    给定一串长度不超过 1 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定 ...

  9. PAT (Basic Level) Practice (中文)1022 D进制的A+B

    1022 D进制的A+B 输入两个非负 10 进制整数 A 和 B (≤2^30^−1),输出 A+B 的 D (1<D≤10)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 ...

随机推荐

  1. 详解 RPL、DPL、CPL 的关系

    保护模式中最重要的一个思想就是通过分级把代码隔离了起来,不同的代码在不同的级别,使大多数情况下都只和同级代码发生关系.Intel的80286以上的cpu可以识別4个特权级(或特权层) ,0级到3级.数 ...

  2. http报文和浏览器缓存机制

    目录 1. 请求报文 1.1请求行 1.2 请求头 一些常用的请求头信息 2. 响应报文 2.1 状态行 1> 响应状态码 2> 常见的响应状态码 2.2 响应头 3. 浏览器缓存 3.1 ...

  3. c++ constructor, copy constructor, operator =

    // list::push_back #include <iostream> #include <list> class element{ private: int numbe ...

  4. java存储位置经典例子

    String a="a";String b="b";String c="ab";String d="ab";String ...

  5. Python 3基础教程25-异常处理

    在Python中,异常处理,主要是try except语句,通常语法格式如下. try: 代码块1 except Exception as e: print(e) 代码2 接着前面读取CSV文件,如果 ...

  6. 11-Mysql数据库----单表查询

    本节重点: 单表查询 语法: 一.单表查询的语法 SELECT 字段1,字段2... FROM 表名 WHERE 条件 GROUP BY field HAVING 筛选 ORDER BY field ...

  7. Turtle模块,一个超精简但功能齐全的绘图包

    先上官方链接https://docs.python.org/3.3/library/turtle.html 再上一个GitHub上别人做的一个小程序,画小猪佩琦的,里面用到了大量常用的turtle接口 ...

  8. mysql数据备份和还原

    MySQL是一个永久存储数据的数据库服务器.如果使用MySQLServer,那么需要创建数据库备份以便从崩溃中恢复.mysql提供了一个用于备份的实用程序mysqldump. 1.普通.sql文件中的 ...

  9. 【解决】Node JS Error: ENOENT

    The Node Beginner Book 书中的实例代码当上传图片时会报Error: ENOENT, 原因:图片默认会选择系统的缓存文件夹下,在windows下无权访问C盘,所以就报错了.. 解决 ...

  10. Uuuuuunity

    foreach http://blog.csdn.net/byondocean/article/details/6871881 yield  http://www.cnblogs.com/CareyS ...