初识sed和gwak
一、sed编辑器
sed命令的格式如下:
sed options script file
选项
-e script 在处理输入时,将script中指定的命令添加到已有的命令中
-f file 在处理输入时,将file中指定的命令添加到已有的命令中
-n 不产生命令输出,使用print命令来完成输出
1.在命令行定义编辑器命令
- $ echo "This is a test" | sed '/test/big test/'
- This is a big test
- [root@localhost sed]# vim data1.txt
- [root@localhost sed]# sed 's/dog/cat/' data1.txt
- The quick brown fox jumps over the lazy cat.
- The quick brown fox jumps over the lazy cat.
- The quick brown fox jumps over the lazy cat.
- The quick brown fox jumps over the lazy cat.
- The quick brown fox jumps over the lazy cat.
- The quick brown fox jumps over the lazy cat.
- The quick brown fox jumps over the lazy cat.
- [root@localhost sed]# cat data1.txt
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
2.在命令行使用多个编辑器命令
- [root@localhost sed]# sed -e 's/brown/green/; s/dog/cat/' data1.txt
- The quick green fox jumps over the lazy cat.
- The quick green fox jumps over the lazy cat.
- The quick green fox jumps over the lazy cat.
- The quick green fox jumps over the lazy cat.
- The quick green fox jumps over the lazy cat.
- The quick green fox jumps over the lazy cat.
- The quick green fox jumps over the lazy cat.
- [root@localhost sed]# cat data1.txt
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
- The quick brown fox jumps over the lazy dog.
3.从文件中读取编辑器命令
- [root@localhost sed]# cat script1.sed
- s/brown/green/
- s/fox/elephant/
- s/dog/cat/
- [root@localhost sed]# sed -f script1.sed data1.txt
- The quick green elephant jumps over the lazy cat.
- The quick green elephant jumps over the lazy cat.
- The quick green elephant jumps over the lazy cat.
- The quick green elephant jumps over the lazy cat.
- The quick green elephant jumps over the lazy cat.
- The quick green elephant jumps over the lazy cat.
- 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 从命令行读取程序脚本
- [root@localhost sed]# gawk '{print "Hello World!"}'
- This is a test
- Hello World!
- hello
- Hello World!
- This is another test
- Hello World!
- Hello World!
- ^C
2.3 使用数据字段变量
$0代表整个文本行
$1代表文本行中的第1个数据字段
$2代表文本行中的第2个数据字段
$n代表文本行中的第n个数据字段
- [root@localhost gawk]# cat data2.txt
- One line of test text.
- Two line of test text.
- Three line of test text.
- [root@localhost gawk]# gawk '{print $1}' data2.txt
- One
- Two
- Three
- [root@localhost gawk]# gawk -F: '{print $1}' /etc/passwd
- root
- bin
- daemon
- adm
- lp
- sync
- shutdown
- halt
- uucp
- operator
- games
- gopher
- ftp
- nobody
如果不是空格分隔符,则用-F指定
2.4 在程序中使用多个命令
- [root@localhost gawk]# echo "My name is Rich" | gawk '{$4="Christine"; print $0}'
- My name is Christine
2.5 从文件中读取程序
- [root@localhost gawk]# cat script2.gawk
- {print $1 "'s home directory is " $6}
- [root@localhost gawk]# gawk -F: -f script2.gawk /etc/passwd
- root's home directory is /root
- bin's home directory is /bin
- daemon's home directory is /sbin
- adm's home directory is /var/adm
- lp's home directory is /var/spool/lpd
- sync's home directory is /sbin
- shutdown's home directory is /sbin
- halt's home directory is /sbin
- mail's home directory is /var/spool/mail
- uucp's home directory is /var/spool/uucp
- operator's home directory is /root
- games's home directory is /usr/games
- gopher's home directory is /var/gopher
- [root@localhost gawk]# cat script3.gawk
- {
- text = "'s home directory is "
- print $1 text $6
- }
- [root@localhost gawk]# gawk -F: -f script3.gawk /etc/passwd
- root's home directory is /root
- bin's home directory is /bin
- daemon's home directory is /sbin
- adm's home directory is /var/adm
- lp's home directory is /var/spool/lpd
- sync's home directory is /sbin
- shutdown's home directory is /sbin
2.6 在处理数据前运行程序脚本
- [root@localhost gawk]# gawk 'BEGIN {print "Hello World!"}'
- Hello World!
- [root@localhost gawk]# cat data3.txt
- Line 1
- Line 2
- Line 3
- [root@localhost gawk]# gawk 'BEGIN {print "The data3 File Contents:"}
- {print $0}' data3.txt
- The data3 File Contents:
- Line 1
- Line 2
- Line 3
2.7 在处理数据后运行脚本
- [root@localhost gawk]# gawk 'BEGIN {print "The data3 File Contents:"}
- > {print $0}
- > END {print "End of file"}' data3.txt
- The data3 File Contents:
- Line 1
- Line 2
- Line 3
- End of file
- [root@localhost gawk]# cat script4.gawk
- BEGIN {
- print "The latest list of users and shells"
- print " UserID \t shell"
- print "-------- \t -------"
- FS=":"
- }
- {
- print $1 " \t " $7
- }
- END {
- print "This concludes the listing"
- }
- [root@localhost gawk]# gawk -f script4.gawk /etc/passwd
- The latest list of users and shells
- UserID shell
- -------- -------
- root /bin/bash
- bin /sbin/nologin
- daemon /sbin/nologin
- adm /sbin/nologin
- lp /sbin/nologin
- sync /bin/sync
- 。。。
- nfsnobody /sbin/nologin
- This concludes the listing
初识sed和gwak的更多相关文章
- shell高级-----初识sed和gawk
sed编辑器 sed说明 sed是Linux下一款功能强大的非交互流式文本编辑器,可以对文本文件进行增.删.改.查等操作,支持按行.按字段.按正则匹配文本内容,灵活方便,特别适合于大文件的编辑. 替换 ...
- [shell编程]初识sed和gawk
一.sed编辑器 shell脚本最常见的用途就是处理文本文件,sed和gawk能够极大的简化需要进行的数据处理任务.sed编辑器是流编辑器,跟普通交互式文本编辑器(如vim)不同.流编辑器 ...
- 《Linux命令行与shell脚本编程大全》第十九章 初识sed和gawk
这两个工具能够极大简化需要进行的数据处理任务. 19.1 文本处理 能轻松实现自动格式化.插入.修改或删除文本元素的简单命令行编辑. sed和gawk就具备上述功能 19.1.1 sed编辑器 被称为 ...
- shell学习记录----初识sed和gawk
Linux命令行与shell脚本编程大全中关于sed和gawk的介绍合在一起,而且结构有点乱. 不像之前的命令写的很清楚.所以这次我需要写下来整理一下. 一.sed部分 1.1 sed命令格式如下: ...
- Linux的sed命令
一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces.sed -i 's/12 ...
- linux sed命令
一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces. sed -i 's/1 ...
- 《Linux命令行与shell脚本编程大全》 第十八章 学习笔记
第十八章:初识sed和gawk 文本处理 sed编辑器 sed编辑器可以基于输入到命令行的或是存储在命令文本文件中的命令来处理数据流中的数据. 它每次读取一行,用提供的编辑器命令匹配数据.按命令中指定 ...
- 《Linux命令行与shell脚本编程大全 第3版》
第一部分 Linux 命令行 第1章 初识Linux she1.1 什么是Linux 21.1.1 深入探究Linux 内核 31.1.2 GNU 工具 61.1.3 Linux 桌面环境 81 ...
- WPF学习之路初识
WPF学习之路初识 WPF 介绍 .NET Framework 4 .NET Framework 3.5 .NET Framework 3.0 Windows Presentation Found ...
随机推荐
- JS中数据结构之散列表
散列是一种常用的数据存储技术,散列后的数据可以快速地插入或取用.散列使用的数据 结构叫做散列表.在散列表上插入.删除和取用数据都非常快. 下面的散列表是基于数组进行设计的,数组的长度是预先设定的,如有 ...
- 十、future其他成员函数、shared_future、atomic(原子操作)
一. int mythread(){ cout<<"thread"<<endl; std::chrono::milliseconds dura();//5秒 ...
- linux系统下tomcat应用开机自启动 配置
linux系统下tomcat应用开机自启动 配置 相对简单的方式是将tomcat添加为系统服务第一步 复制文件将 $Tomcat_Home/bin目录下的 catalina.sh脚本文件复制到目录/ ...
- LG2704 [NOI2001] 炮兵阵地
题目描述 (试题来源:Link ) 司令部的将军们打算在 \(N\times M\) 的网格地图上部署他们的炮兵部队.一个 \(N\times M\) 的地图由 \(N\) 行 \(M\) 列组成,地 ...
- [CSP-S模拟测试]:platform(后缀数组+二分+线段树)
题目传送门 题目描述 走过奈何桥有一个名叫望乡台的土台,望乡台有个名曰孟婆的老妇人在卖孟婆汤.一生爱恨情仇,一世浮沉得失,都可以随这碗孟婆汤遗忘得干干净净.现在有$n$碗孟婆汤摆成一排,汤的品种不超过 ...
- MySQL操作数据库值mysql事务
创建一个无参数的事务 注意要写START TRANSACTION或者是Begin;Mysql会默认直接执行一个单元 MYSQL默认是自动提交的,也就是你提交一个QUERY,它就直接执行!我们可 ...
- nb哒LCA
求欧拉序每log分一块每段找最小值共n/log块然后建st表,复杂度n/log*log = n每块记前后缀最小过至少一块很好求对于在一块的:由于欧拉序的标号前后只会相差1所以序列种类只有2^k种k&l ...
- WIN7 下的 filemon 版本
http://blog.sina.com.cn/s/blog_594398e80100tx1q.html WIN7 下的 filemon 版本 (2011-09-26 22:26:12) 标签: fi ...
- crontab 使用
1. 安装,结构 yum install cronie 结构: * * * * * [分钟] [小时] [每月的某一天] [每年的某一月] [每周的某一天] 2.命令 1,添加或更新crontab中的 ...
- Nginx的作用详解
Nginx的产生 没有听过Nginx?那么一定听过它的"同行"Apache吧!Nginx同Apache一样都是一种WEB服务器.基于REST架构风格,以统一资源描述符(Unifor ...