一、   用grep在文件中搜索文本

  grep能够接受正则表达式,生成各种格式的输出。除此之外,它还有大量有趣的选项。

1、  搜索包含特定模式的文本行:

2、  从stdin中读取:

3、  单个grep命令可以对多个文件进行搜索:

4、  --color选项在输出行中着重标记出匹配到的单词:

5、  grep中使用正则表达式时使用(grep -E或者egrep)

6、  只输出文件中匹配到的文本部分,可以使用-o:

7、  要显示除匹配行外的所有行用-v选项:

8、  统计文件或文本中包含匹配字符串的行数,-c(在单行出现多个匹配,只匹配一次):

9、  打印出包含匹配字符串的行号,-n:

10、  搜索多个文件并找出匹配文本位于哪一个文件,-l(-L与之作用相反):

11、  递归搜素文件,-r(-R与之作用相同):

12、  忽略样式中的大小写,-i:

13、  用grep匹配多个样式,-e:

14、  在grep搜索中指定(--include)或排除(--exclude)文件:

目录中递归搜索所有的.c和.cpp文件

在搜索中排除所有的README文件

如果需要排除目录,使用--exclude-dir选项

15、  grep静默输出,-q:

不输出任何内容,如果成功匹配返回0,如果失败返回非0值。

16、  打印出匹配文本之前或之后的行:

  1. [root@localhost tmp]# seq
  2.  
  3. [root@localhost tmp]# seq | grep -A 3  #打印匹配的后指定行数
  4.  
  5. [root@localhost tmp]# seq | grep -B 3  #打印匹配前指定行数
  6.  
  7. [root@localhost tmp]# seq | grep -C 3 #打印匹配前后指定行数

二、  使用sed进行文本替换

  sed是流编辑器(stream editor)的缩写。sed一个用法为文本替换。

  1. [root@cairui ~]# sed --help
  2. Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
  3.  
  4. -n, --quiet, --silent
  5. suppress automatic printing of pattern space  #取消自动打印模式空间
  6. -e script, --expression=script
  7. add the script to the commands to be executed  #添加“脚本”到程序的运行列表
  8. -f script-file, --file=script-file
  9. add the contents of script-file to the commands to be executed  #添加“脚本文件”到程序的运行列表
  10. --follow-symlinks
  11. follow symlinks when processing in place; hard links
  12. will still be broken.
  13. -i[SUFFIX], --in-place[=SUFFIX]
  14. edit files in place (makes backup if extension supplied).
  15. The default operation mode is to break symbolic and hard links.
  16. This can be changed with --follow-symlinks and --copy.
  17. -c, --copy
  18. use copy instead of rename when shuffling files in -i mode.
  19. While this will avoid breaking links (symbolic or hard), the
  20. resulting editing operation is not atomic. This is rarely
  21. the desired mode; --follow-symlinks is usually enough, and
  22. it is both faster and more secure.
  23. -l N, --line-length=N
  24. specify the desired line-wrap length for the `l' command
  25. --posix
  26. disable all GNU extensions.
  27. -r, --regexp-extended
  28. use extended regular expressions in the script.
  29. -s, --separate
  30. consider files as separate rather than as a single continuous
  31. long stream.
  32. -u, --unbuffered
  33. load minimal amounts of data from the input files and flush
  34. the output buffers more often
  35. --help display this help and exit
  36. --version output version information and exit
  37.  
  38. If no -e, --expression, -f, or --file option is given, then the first
  39. non-option argument is taken as the sed script to interpret. All
  40. remaining arguments are names of input files; if no input files are
  41. specified, then the standard input is read.
  42.  
  43. GNU sed home page: <http://www.gnu.org/software/sed/>.
  44. General help using GNU software: <http://www.gnu.org/gethelp/>.
  45. E-mail bug reports to: <bug-gnu-utils@gnu.org>.
  46. Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

1、  sed可以替换给定文本的字符串:

该使用从stdin中读取输入,不影响原本的内容

2、默认情况下sed命令打印替换后的文本,如果想连原文本一起修改加-i命令,-i:

3、  之前的sed都是替换第一个匹配到的内容,想要全部替换就要在末尾加g:

从第N个匹配开始替换

sed中的/为定界符,使用任何其他符号都可以替代

4、  移除空白行

三、  使用awk进行高级文本处理

  awk是一款设计用于数据流的工具。它对列和行进行操作。awk有很多内建的功能,比如数组、函数等,和C有很多相同之处。awk最大的优势是灵活性。

  1. [root@cairui ~]# awk --help
  2. Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
  3. Usage: awk [POSIX or GNU style options] [--] 'program' file ...
  4. POSIX options: GNU long options:
  5. -f progfile --file=progfile
  6. -F fs --field-separator=fs
  7. -v var=val --assign=var=val
  8. -m[fr] val
  9. -O --optimize
  10. -W compat --compat
  11. -W copyleft --copyleft
  12. -W copyright --copyright
  13. -W dump-variables[=file] --dump-variables[=file]
  14. -W exec=file --exec=file
  15. -W gen-po --gen-po
  16. -W help --help
  17. -W lint[=fatal] --lint[=fatal]
  18. -W lint-old --lint-old
  19. -W non-decimal-data --non-decimal-data
  20. -W profile[=file] --profile[=file]
  21. -W posix --posix
  22. -W re-interval --re-interval
  23. -W source=program-text --source=program-text
  24. -W traditional --traditional
  25. -W usage --usage
  26. -W use-lc-numeric --use-lc-numeric
  27. -W version --version
  28.  
  29. To report bugs, see node `Bugs' in `gawk.info', which is
  30. section `Reporting Problems and Bugs' in the printed version.
  31.  
  32. gawk is a pattern scanning and processing language.
  33. By default it reads standard input and writes standard output.
  34.  
  35. Examples:
  36. gawk '{ sum += $1 }; END { print sum }' file
  37. gawk -F: '{ print $1 }' /etc/passwd

awk脚本的结构基本如下所示:

awk ' BEGIN{ print "start" } pattern { commands } END { print "end" }' file

awk脚本通常由3部分组成。BEGIN,END和带模式匹配选项的常见语句块。这3个部分都是可选的。

1、工作原理

(1)执行BEGIN { commands }语句块中的语句。

(2)从文件或stdin中读取一行,然后执行pattern { commands }。重复这个过程,直到文件全部被读取完毕。

(3)当读至输入流末尾时,执行END { commands }语句块。

其中最重要的部分就是pattern语句块中的通用命令。这个语句块同样是可选的。如果不提供该语句块,则默认执行{ print },即打印所读取到的每一行。awk对于每一行,都会执行这个语句块。这就像一个用来读取行的while循环,在循环中提供了相应的语句。

三大文本处理工具grep、sed及awk的更多相关文章

  1. 【Linux】 字符串和文本处理工具 grep & sed & awk

    Linux字符串&文本处理工具 因为用linux的时候主要用到的还是字符交互界面,所以对字符串的处理变得十分重要.这篇介绍三个常用的字符串处理工具,包括grep,sed和awk ■ grep ...

  2. 三大文本处理工具grep、sed及awk的简单介绍

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...

  3. shell之三大文本处理工具grep、sed及awk

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...

  4. 【OS_Linux】三大文本处理工具之sed命令

    1.sed命令的简介及用法 sed:即为流编辑器,“stream editor”的缩写.他先将源文件读取到临时缓存区(也叫模式空间)中,再对满足匹配条件的各行执行sed命令.sed命令只针对缓存区中的 ...

  5. linux(5)--补充(管道| / 重定向> / xargs)/find 与xargs结合使用/vi,grep,sed,awk(支持正则表达式的工具程序)

    本节中正则表达式的工具程序 grep,sed和awk是重点,也是难点!!! 先补充一下一. 管道| / 重定向> / xargs 如:1. 管道和重定向的区别:具体可以见 http://www. ...

  6. grep, sed 与 awk 补补课,到底怎么用!

    grep, sed 与 awk 相当有用 ! gerp 查找, sed 编辑, awk 根据内容分析并处理. awk(关键字:分析&处理) 一行一行的分析处理 awk '条件类型1{动作1}条 ...

  7. 日志分析查看——grep,sed,sort,awk运用

    概述 我们日常应用中都离不开日志.可以说日志是我们在排查问题的一个重要依据.但是日志并不是写了就好了,当你想查看日志的时候,你会发现线上日志堆积的长度已经超越了你一行行浏览的耐性的极限了.于是,很有必 ...

  8. Linux 文本处理工具grep,sed,awk

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...

  9. 正则表达式学习之grep,sed和awk

    正则表达式是用于描述字符排列和匹配模式的一种语法,它主要用于字符串的模式分割.匹配.查找以及替换操作. 描述一个正则表达式需要字符类.数量限定符.位置限定符.规定一些特殊语法表示字符类,数量限定符和位 ...

随机推荐

  1. HTTP:HTTP百科

    ylbtech-HTTP:HTTP百科 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计 ...

  2. jenkins学习 03 jenkins配置Maven项目

    我们的产品使用Git作为版本管理工具,而jenkins需要git插件来支持git,所以我们需要为jenkins添加git插件. 在Available tab页中找到Git Plugin 点击下方的In ...

  3. 2016.1.1 VS中宏的使用技巧点滴

    Dim selection As TextSelection = DTE.ActiveDocument.Selection'定义 TextSelection 对象 selection.StartOfL ...

  4. CSS3图片以中心缩放,放大超出隐藏实现

    首页,重点是有一个包裹img标签的容器,这里我们给该容器设置一个class为selfScale <div class="selfScale"> <img sr=& ...

  5. Android编译系统产品线

    1.Android源码中的产品线解析 通常产品厂商在拿到Android源码后会在Android源码基础上进行定制修改,以匹配适应自己的产品.这就引入了产品线的概念.Android系统源码中,产品相关的 ...

  6. Linux split命令实例

    曾经是否想要把一个大文件分割成多个小文件?比如一个5gb日志文件,我们需要把它分成多个小文件,这样我们才有可能使用普通的文本编辑器读取它.有时我们需要传输20gb的大文件到另一台服务器,这就需要我们把 ...

  7. JAVA基础知识总结12(多线程)

    进程:正在进行中的程序.其实进程就是一个应用程序运行时的内存分配空间. 线程:其实就是进程中一个程序执行控制单元,一条执行路径.进程负责的是应用程序的空间的标示.线程负责的是应用程序的执行顺序. 一个 ...

  8. sys添加调用模块的路径;遍历可以调用模块的路径

    import sys sys.path.append("D:") for i in sys.path: print(i)

  9. SSM项目连接远程Linux服务器的mysql 启动tomcat卡在了 Initializing Spring root WebApplicationContext

    网上查了原因, linux下mysql访问速度缓慢并且ssh连接缓慢的原因 解决办法: 1.linux ssh连接慢 最近发现ssh连接的时候却很慢,ping的速度非常好,让人误以为是ssh连接不上. ...

  10. require()和include()代码重用

    第五章 require()函数和include()函数几乎是相同的,二者唯一的区别在于函数失败后,require()函数将给出一个致命的错误,而include()只是给出一个警告. require_o ...