三大文本处理工具grep、sed及awk

摘要:
16.在匹配文本之前或之后打印行:[root@localhosttmp]#seq1012345678910[root@localhosttmp]#Seq10|grep5-A3#打印匹配的指定行数5678[root@localhosttmp]#Seq10|grep5-B3#指定打印前的行数2345[root@localhosttmp]#Seq10|grep5-C3#指定打印前后与2345678 II匹配的行数。使用sed替换文本。sed是流编辑器的缩写。sed的一个用法是文本替换。Awk最大的优势是灵活性。

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

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

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

三大文本处理工具grep、sed及awk第1张

2、  从stdin中读取:

三大文本处理工具grep、sed及awk第2张

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

三大文本处理工具grep、sed及awk第3张

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

三大文本处理工具grep、sed及awk第4张

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

三大文本处理工具grep、sed及awk第5张

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

三大文本处理工具grep、sed及awk第6张

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

三大文本处理工具grep、sed及awk第7张

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

三大文本处理工具grep、sed及awk第8张

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

三大文本处理工具grep、sed及awk第9张

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

三大文本处理工具grep、sed及awk第10张

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

三大文本处理工具grep、sed及awk第11张

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

三大文本处理工具grep、sed及awk第12张

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

三大文本处理工具grep、sed及awk第13张

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

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

三大文本处理工具grep、sed及awk第14张

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

三大文本处理工具grep、sed及awk第15张

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

15、  grep静默输出,-q:

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

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

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

二、  使用sed进行文本替换

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

[root@cairui ~]# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space  #取消自动打印模式空间
  -e script, --expression=script
                 add the script to the commands to be executed  #添加“脚本”到程序的运行列表
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed  #添加“脚本文件”到程序的运行列表
  --follow-symlinks
                 follow symlinks when processing in place; hard links
                 will still be broken.
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied).
                 The default operation mode is to break symbolic and hard links.
                 This can be changed with --follow-symlinks and --copy.
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode.
                 While this will avoid breaking links (symbolic or hard), the
                 resulting editing operation is not atomic.  This is rarely
                 the desired mode; --follow-symlinks is usually enough, and
                 it is both faster and more secure.
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-gnu-utils@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

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

三大文本处理工具grep、sed及awk第16张

三大文本处理工具grep、sed及awk第17张

三大文本处理工具grep、sed及awk第18张

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

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

三大文本处理工具grep、sed及awk第19张

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

三大文本处理工具grep、sed及awk第20张

从第N个匹配开始替换

三大文本处理工具grep、sed及awk第21张

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

三大文本处理工具grep、sed及awk第22张

4、  移除空白行

三大文本处理工具grep、sed及awk第23张

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

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

[root@cairui ~]# awk --help
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:        GNU long options:
    -f progfile        --file=progfile
    -F fs            --field-separator=fs
    -v var=val        --assign=var=val
    -m[fr] val
    -O            --optimize
    -W compat        --compat
    -W copyleft        --copyleft
    -W copyright        --copyright
    -W dump-variables[=file]    --dump-variables[=file]
    -W exec=file        --exec=file
    -W gen-po        --gen-po
    -W help            --help
    -W lint[=fatal]        --lint[=fatal]
    -W lint-old        --lint-old
    -W non-decimal-data    --non-decimal-data
    -W profile[=file]    --profile[=file]
    -W posix        --posix
    -W re-interval        --re-interval
    -W source=program-text    --source=program-text
    -W traditional        --traditional
    -W usage        --usage
    -W use-lc-numeric    --use-lc-numeric
    -W version        --version

To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.

gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.

Examples:
    gawk '{ sum += $1 }; END { print sum }' file
    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第24张

三大文本处理工具grep、sed及awk第25张

免责声明:文章转载自《三大文本处理工具grep、sed及awk》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇修改xshell远程连接端口iOS 新手引导页面,透明遮罩指引页(镂空处理)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux shell awk中printf使用

printf 是 awk 的重要格式化输出命令printf格式化输出内容 格式:printf format,item1,item2...要点: 1,printf输出时要指定格式format2,formay用于指定后面的每个item输出的格式3,printf语句不会自动打印换行符 format格式: %c:显示单个字符%d,%i:十进制整数%e,%E:科学...

shell编程之sed

一、sed (Stream Editor) 1、定位行: sed -n '12,~3p' pass #从第12行开始,直到下一个3的倍数行(12-15行) sed -n '12,+4p' pass #从第12行开始,连续4行(12-16行) sed -n '12~3p' pass #从第12行开始,间隔3行输出一次(12,15,18,21...) se...

db2 常用命令

1、将常用命令,设置为别名,方便记忆 alias listapp='db2 list applications' alias listappx='db2 list applications show detail' alias listtran='db2 list INDOUBT TRANSACTIONS' alias listtranx='db2 lis...

Linux下杀掉所有得java进程

--转自https://blog.csdn.net/oppo62258801/article/details/81434038 1.Linux查看所有Java进程 ps -ef | grep java | grep -v grep  (是在列出的进程中去除含有关键字"grep"的进程) 2. 使用awk分割结果,获取PID awk '{print $2}'...

查看KVM虚拟机IP

#!/bin/bash #ping当前网段内在线的主机,以便产生arp记录. subnet=`route -n|grep "UG" |awk '{print $2}'|sed 's/..$//g'` for ip in $subnet.{1..253};do { ping -c1 $ip >/dev/null 2>&1 }& d...

Sed命令

sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要...