一、sed编辑器

sed命令的格式如下:

sed options script file

选项

-e script        在处理输入时,将script中指定的命令添加到已有的命令中

-f file            在处理输入时,将file中指定的命令添加到已有的命令中

-n                不产生命令输出,使用print命令来完成输出



1.在命令行定义编辑器命令

  1. $ echo "This is a test" | sed '/test/big test/'
  2. This is a big test
  1. [root@localhost sed]# vim data1.txt
  2. [root@localhost sed]# sed 's/dog/cat/' data1.txt
  3. The quick brown fox jumps over the lazy cat.
  4. The quick brown fox jumps over the lazy cat.
  5. The quick brown fox jumps over the lazy cat.
  6. The quick brown fox jumps over the lazy cat.
  7. The quick brown fox jumps over the lazy cat.
  8. The quick brown fox jumps over the lazy cat.
  9. The quick brown fox jumps over the lazy cat.
  10. [root@localhost sed]# cat data1.txt
  11. The quick brown fox jumps over the lazy dog.
  12. The quick brown fox jumps over the lazy dog.
  13. The quick brown fox jumps over the lazy dog.
  14. The quick brown fox jumps over the lazy dog.
  15. The quick brown fox jumps over the lazy dog.
  16. The quick brown fox jumps over the lazy dog.
  17. The quick brown fox jumps over the lazy dog.

2.在命令行使用多个编辑器命令

  1. [root@localhost sed]# sed -e 's/brown/green/; s/dog/cat/' data1.txt
  2. The quick green fox jumps over the lazy cat.
  3. The quick green fox jumps over the lazy cat.
  4. The quick green fox jumps over the lazy cat.
  5. The quick green fox jumps over the lazy cat.
  6. The quick green fox jumps over the lazy cat.
  7. The quick green fox jumps over the lazy cat.
  8. The quick green fox jumps over the lazy cat.
  9. [root@localhost sed]# cat data1.txt
  10. The quick brown fox jumps over the lazy dog.
  11. The quick brown fox jumps over the lazy dog.
  12. The quick brown fox jumps over the lazy dog.
  13. The quick brown fox jumps over the lazy dog.
  14. The quick brown fox jumps over the lazy dog.
  15. The quick brown fox jumps over the lazy dog.
  16. The quick brown fox jumps over the lazy dog.

3.从文件中读取编辑器命令

  1. [root@localhost sed]# cat script1.sed
  2. s/brown/green/
  3. s/fox/elephant/
  4. s/dog/cat/
  5. [root@localhost sed]# sed -f script1.sed data1.txt
  6. The quick green elephant jumps over the lazy cat.
  7. The quick green elephant jumps over the lazy cat.
  8. The quick green elephant jumps over the lazy cat.
  9. The quick green elephant jumps over the lazy cat.
  10. The quick green elephant jumps over the lazy cat.
  11. The quick green elephant jumps over the lazy cat.
  12. The quick green elephant jumps over the lazy cat.

二、gawk程序可以

①定义变量来保存数据;

②使用算数和字符串操作符处理数据;

③使用结构化编程概念(if-then)来为数据处理增减处理逻辑;

④通过提取数据文件中的数据元素,将其重新排列或格式化,生成格式化报告。



gawk是awk语言中使用gnu的一种



2.1 gawk命令格式

gawk options program file

选项

-F fs                                   指定行中划分数据字段分隔符

-f file                                 从指定的文件中读取程序

-v var=value                      定义gawk程序中的一个变量及其默认值

-mf N                                指定要处理的数据文件中的最大字段数

-mr N                                指定数据文件中的最大数据行数

-W keyword                       指定gawk的兼容模式或警告等级



2.2 从命令行读取程序脚本

  1. [root@localhost sed]# gawk '{print "Hello World!"}'
  2. This is a test
  3. Hello World!
  4. hello
  5. Hello World!
  6. This is another test
  7. Hello World!
  8. Hello World!
  9. ^C

2.3 使用数据字段变量

$0代表整个文本行

$1代表文本行中的第1个数据字段

$2代表文本行中的第2个数据字段

$n代表文本行中的第n个数据字段

  1. [root@localhost gawk]# cat data2.txt
  2. One line of test text.
  3. Two line of test text.
  4. Three line of test text.
  5. [root@localhost gawk]# gawk '{print $1}' data2.txt
  6. One
  7. Two
  8. Three
  1. [root@localhost gawk]# gawk -F: '{print $1}' /etc/passwd
  2. root
  3. bin
  4. daemon
  5. adm
  6. lp
  7. sync
  8. shutdown
  9. halt
  10. mail
  11. uucp
  12. operator
  13. games
  14. gopher
  15. ftp
  16. nobody

如果不是空格分隔符,则用-F指定



2.4 在程序中使用多个命令

  1. [root@localhost gawk]# echo "My name is Rich" | gawk '{$4="Christine"; print $0}'
  2. My name is Christine

2.5 从文件中读取程序

  1. [root@localhost gawk]# cat script2.gawk
  2. {print $1 "'s home directory is " $6}
  3. [root@localhost gawk]# gawk -F: -f script2.gawk /etc/passwd
  4. root's home directory is /root
  5. bin's home directory is /bin
  6. daemon's home directory is /sbin
  7. adm's home directory is /var/adm
  8. lp's home directory is /var/spool/lpd
  9. sync's home directory is /sbin
  10. shutdown's home directory is /sbin
  11. halt's home directory is /sbin
  12. mail's home directory is /var/spool/mail
  13. uucp's home directory is /var/spool/uucp
  14. operator's home directory is /root
  15. games's home directory is /usr/games
  16. gopher's home directory is /var/gopher
  1. [root@localhost gawk]# cat script3.gawk
  2. {
  3. text = "'s home directory is "
  4. print $1 text $6
  5. }
  6. [root@localhost gawk]# gawk -F: -f script3.gawk /etc/passwd
  7. root's home directory is /root
  8. bin's home directory is /bin
  9. daemon's home directory is /sbin
  10. adm's home directory is /var/adm
  11. lp's home directory is /var/spool/lpd
  12. sync's home directory is /sbin
  13. shutdown's home directory is /sbin

2.6 在处理数据前运行程序脚本

  1. [root@localhost gawk]# gawk 'BEGIN {print "Hello World!"}'
  2. Hello World!
  1. [root@localhost gawk]# cat data3.txt
  2. Line 1
  3. Line 2
  4. Line 3
  5. [root@localhost gawk]# gawk 'BEGIN {print "The data3 File Contents:"}
  6. {print $0}' data3.txt
  7. The data3 File Contents:
  8. Line 1
  9. Line 2
  10. Line 3

2.7 在处理数据后运行脚本

  1. [root@localhost gawk]# gawk 'BEGIN {print "The data3 File Contents:"}
  2. > {print $0}
  3. > END {print "End of file"}' data3.txt
  4. The data3 File Contents:
  5. Line 1
  6. Line 2
  7. Line 3
  8. End of file
  1. [root@localhost gawk]# cat script4.gawk
  2. BEGIN {
  3. print "The latest list of users and shells"
  4. print " UserID \t shell"
  5. print "-------- \t -------"
  6. FS=":"
  7. }
  8. {
  9. print $1 " \t " $7
  10. }
  11. END {
  12. print "This concludes the listing"
  13. }
  14. [root@localhost gawk]# gawk -f script4.gawk /etc/passwd
  15. The latest list of users and shells
  16. UserID shell
  17. -------- -------
  18. root /bin/bash
  19. bin /sbin/nologin
  20. daemon /sbin/nologin
  21. adm /sbin/nologin
  22. lp /sbin/nologin
  23. sync /bin/sync
  24. 。。。
  25. nfsnobody        /sbin/nologin
  26. This concludes the listing

初识sed和gwak的更多相关文章

  1. shell高级-----初识sed和gawk

    sed编辑器 sed说明 sed是Linux下一款功能强大的非交互流式文本编辑器,可以对文本文件进行增.删.改.查等操作,支持按行.按字段.按正则匹配文本内容,灵活方便,特别适合于大文件的编辑. 替换 ...

  2. [shell编程]初识sed和gawk

    一.sed编辑器       shell脚本最常见的用途就是处理文本文件,sed和gawk能够极大的简化需要进行的数据处理任务.sed编辑器是流编辑器,跟普通交互式文本编辑器(如vim)不同.流编辑器 ...

  3. 《Linux命令行与shell脚本编程大全》第十九章 初识sed和gawk

    这两个工具能够极大简化需要进行的数据处理任务. 19.1 文本处理 能轻松实现自动格式化.插入.修改或删除文本元素的简单命令行编辑. sed和gawk就具备上述功能 19.1.1 sed编辑器 被称为 ...

  4. shell学习记录----初识sed和gawk

    Linux命令行与shell脚本编程大全中关于sed和gawk的介绍合在一起,而且结构有点乱. 不像之前的命令写的很清楚.所以这次我需要写下来整理一下. 一.sed部分 1.1 sed命令格式如下: ...

  5. Linux的sed命令

    一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces.sed -i 's/12 ...

  6. linux sed命令

    一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces. sed -i 's/1 ...

  7. 《Linux命令行与shell脚本编程大全》 第十八章 学习笔记

    第十八章:初识sed和gawk 文本处理 sed编辑器 sed编辑器可以基于输入到命令行的或是存储在命令文本文件中的命令来处理数据流中的数据. 它每次读取一行,用提供的编辑器命令匹配数据.按命令中指定 ...

  8. 《Linux命令行与shell脚本编程大全 第3版》

    第一部分 Linux 命令行 第1章  初识Linux she1.1   什么是Linux 21.1.1 深入探究Linux 内核 31.1.2 GNU 工具 61.1.3 Linux 桌面环境 81 ...

  9. WPF学习之路初识

    WPF学习之路初识   WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...

随机推荐

  1. DELPHI 10 SEATTLE 在OSX上安装PASERVER

    旧版本的DELPHI在安装目录下里的PASERVER目录有安装文件,但奇怪在这个SEATTLE上的PASERVER目录下只有一个EXE程序的安装程序​​,显然不能安装到OSX里,需要在Embarcad ...

  2. npm和gem

    https://blog.csdn.net/u011099640/article/details/53083845

  3. PHP curl_multi_close函数

    curl_multi_close — 关闭一组cURL句柄 说明 void curl_multi_close ( resource $mh ) 关闭一组cURL句柄. 参数 mh 由 curl_mul ...

  4. JS在页面加载之后运行

    通用的页面加载后再运行JS有两种方式:1.在DOM加载完毕后,页面全部内容(如图片等)完全加载完毕前运行JS.   2.在页面全部内容加载完成(包括引用文件,图片等)之后再加载JS 1.在DOM加载后 ...

  5. 十、future其他成员函数、shared_future、atomic(原子操作)

    一. int mythread(){ cout<<"thread"<<endl; std::chrono::milliseconds dura();//5秒 ...

  6. day 107radis非关系型数据库

    http://www.cnblogs.com/wupeiqi/articles/5132791.html   参考邮件. radis : 1. NoSql 2. 缓存在内存中 3.支持数据持久化 二. ...

  7. vue2.0 项目小总结

    最近做了一个vue的PC端的项目,不大,真正用到vue的东西也不是太多,逻辑处理用到了不少原生js东西. 1.图片渲染 后台返回base64格式数据,一开始绑定src,提示pic字段未定义,懵逼了好久 ...

  8. Cocos2d-x之Sound

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 音效简介: 1.1 在游戏开发的过程中除了华丽的界面,生动的动画之外,适当的音效也是重要的一部分 1.2 游戏中的声音分为两类,一类是音乐 ...

  9. Cocos2d-x之Menu

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. cocos2d-x菜单简介: 菜单也是游戏开发中的重要环节,一般游戏开始的第一个画面都是游戏主菜单,这些菜单包括,开始游戏,游戏设置,关卡 ...

  10. ORCAL 数据库的约束以及SQL语言的四种类型

    oracle数据库约束: 定义:要输入的这个值是一个什么样的值, 或者是哪个范围内的值 作用: 确保完整性, 确保精确性 1, 非空约束(not null) 记录一条信息的时候如果用户名和密码没有被记 ...