参考资料:https://blog.csdn.net/u010502101/article/details/81839519
AWK, 数据过滤工具 (类似于grep,比grep强大),属数据处理引擎,基于模式匹配检查输入文本,逐行处理并输出。通常用在Shell脚本中,获取指定的数据,单独使用时,可对文本数据做统计
格式
格式1:前置命令 | awk [选项] ‘条件{编辑指令}’
格式2:awk [选项] ‘条件{编辑指令}’ 文件…
编辑指令如果包含多条语句时,可以用分号分隔,处理文本时,若未指定分隔符,则默认将空格、制表符等作为分隔符。print是最常见的指令。
选项
-F:指定分隔符,可省略(默认空格或Tab位)
-V:调用外部Shell变量 variable
awk把分割后的数据字段自动分配给数据字段变量
[root@xuniji01 ~]# cat test.txt
The dog:There is a big dog and a little dog in the park
The cat:There is a big cat and a little cat in the park
The tiger:There is a big tiger and a litle tiger in the park
通过选项-F指定“:”为字段分隔符,把每行数据分为两段,然后输出第二个数据字段$2。
[root@xuniji01 ~]# awk -F: '{print $2}' test.txt
There is a big dog and a little dog in the park
There is a big cat and a little cat in the park
There is a big tiger and a litle tiger in the park
注意:如不显示指定字段分隔符,awk的默认字段分隔符为任意空白字符,包括制表符、空格符、换行符等。
[root@xuniji01 ~]# awk -F: '{$1="Description:"; print $0}' test.txt
Description: There is a big dog and a little dog in the park
Description: There is a big cat and a little cat in the park
Description: There is a big tiger and a litle tiger in the park
[root@xuniji01 ~]# vim pokes.txt
{
$1="Description:"
print $0
}
[root@xuniji01 ~]# awk -F: -f pokes.txt test.txt
Description: There is a big dog and a little dog in the park
Description: There is a big cat and a little cat in the park
Description: There is a big tiger and a litle tiger in the park
awk默认每次读入一行数据,然后用脚本进行处理。如果想在处理文本之前预处理一些命令,可以用BEGIN关键字指定。
[root@xuniji01 ~]# awk -F: 'BEGIN{print "开始处理..."}{print $2}' test.txt
开始处理...
There is a big dog and a little dog in the park
There is a big cat and a little cat in the park
There is a big tiger and a litle tiger in the park
用END关键字在处理完所有数据后,再运行善后处理工作。
[root@xuniji01 ~]# awk -F: '{print $2} END{print "处理结束..."}' test.txt
There is a big dog and a little dog in the park
There is a big cat and a little cat in the park
There is a big tiger and a litle tiger in the park
处理结束...
变量又分为两种形式:awk内置的变量;用户自定义的变量。
[root@xuniji01 ~]# cat test.txt
The dog:There is a big dog and a little dog in the park
The cat:There is a big cat and a little cat in the park
The tiger:There is a big tiger and a litle tiger in the park
[root@xuniji01 ~]# awk 'BEGIN{FS=":"} {print $1, $2}' test.txt #用FS指定字段分隔符为“:”,然后用“:”把每行数据分割为两段。
The dog There is a big dog and a little dog in the park
The cat There is a big cat and a little cat in the park
The tiger There is a big tiger and a litle tiger in the park
用FS指定输入字段分隔符“:”后,每行数据分为两个数据段,输出时,用OFS指定两个数据字段用“>”拼接。
[root@xuniji01 ~]# cat test.txt
The dog:There is a big dog and a little dog in the park
The cat:There is a big cat and a little cat in the park
The tiger:There is a big tiger and a litle tiger in the park
[root@xuniji01 ~]# awk 'BEGIN{FS=":"; OFS=">"} {print $1, $2}' test.txt #其实就是,FS指定字段分隔符为“:”,然后将指定的分隔符替换为>
The dog>There is a big dog and a little dog in the park
The cat>There is a big cat and a little cat in the park
The tiger>There is a big tiger and a litle tiger in the park
默认情况下RS和ORS设置为“\n”,表示输入数据流中的每一行作为一条记录,输出时每条记录之间也以“\n”进行分割。
下面以a.txt文件为例,a.txt文件中内容如下:
[root@xuniji01 ~]# cat a.txt
Tom is a student
and he is 20 years old
Bob is a teacher
and he is 40 years old
默认情况下,每行作为一条记录处理,但此种情况下,要把第一行和第二行作为一条记录处理,第三行和第四行作为一条记录处理。
[root@xuniji01 ~]# awk 'BEGIN{RS=""; ORS="\n"; FS="and"; OFS=","} {print $1, $2}' a.txt
Tom is a student
, he is 20 years old
Bob is a teacher
, he is 40 years old
\n把前两行、后两行各看作一条记录来处理,然后把指定分隔符and替换为逗号。
##先查出IP地址
[root@xuniji01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:15:5d:0b:f5:03 brd ff:ff:ff:ff:ff:ff
inet 10.5.6.244/24 brd 10.5.6.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::b268:c536:3f01:4c85/64 scope link noprefixroute
valid_lft forever preferred_lft forever
##先把这一行拉出来
[root@xuniji01 ~]# ip add | grep global
inet 10.5.6.244/24 brd 10.5.6.255 scope global noprefixroute eth0
##再把以空格为分隔的第二列拉出来
[root@xuniji01 ~]# ip add | grep global | awk '{print $2}'
10.5.6.244/24
##然后把以/24为分隔符的第一列拉出来
[root@xuniji01 ~]# ip add | grep global | awk '{print $2}' | awk -F/24 '{print $1}' # 以/24为分隔符的第一列
10.5.6.244
这样就OK了。
##如果需要提取广播,提取第四列
[root@xuniji01 ~]# ip add | grep global | awk '{print $4}'
10.5.6.255
[root@xuniji01 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 904M 0 904M 0% /dev
tmpfs 915M 0 915M 0% /dev/shm
tmpfs 915M 8.5M 907M 1% /run
tmpfs 915M 0 915M 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 2.0G 48G 4% /
/dev/sda1 1014M 180M 835M 18% /boot
/dev/mapper/centos-home 74G 33M 74G 1% /home
tmpfs 183M 0 183M 0% /run/user/0
[root@xuniji01 ~]# df -h | grep home
/dev/mapper/centos-home 74G 33M 74G 1% /home
[root@xuniji01 ~]# df -h | grep home | awk '{print $4}'
74G
springMVC中异常处理的思路:mvc中,controller层调用service层,service层调用dao层,每一层我们都将异常通过throws向上抛出,最终抛给DispatcherServlet去找异常处理器进行处理。springMVC中的异常处理:1.目录结构2. 前端页面(写的比较简单,测试使用)index.jsp<%@ page contentType="te..._谈一下springmvc统一异常处理的思想和实现方式
Spring MVC的工作流程请描述Spring MVC的工作流程?描述一下 DispatcherServlet 的工作流程?参考答案:1.用户发送请求至前端控制器DispatcherServlet;2.DispatcherServlet收到请求后,调用HandlerMapping处理器映射器,请求获取Handle;3.处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet;4.DispatcherServlet 调用
“知识爆炸”和“知识老化”这两大问题,不断困扰着现代教育,人们解决这一问题的良方之一,就是加强学生对基础知识的学习。近年来在中国兴起的中小学生学习计算机热,也同样面对这两个问题,由于计算机的操作知识是复杂多样的,且在不断的更新、发展之中,所以有关计算讥的知识较比其它学科的知识,更易陶汰和老化。如果我们盲目地随现时社会的潮流确定计算机的教学内容,那么势必造成学生不能适应操作未来更加新型的计算机,甚至..._学生计算机基础知识
一、为什么选择开源项目1. 快速开发 2. 站在巨人的肩膀上成熟的开源项目,功能完善、设计优秀,细节点考虑周全,不用像自己开发一样需要从头去全面开发、测试、完善。本身也是好的学习材料。Android 本身就是在 Linux、Java 上发展起来的。 3. 社区、全网智慧成熟的开源项目会被众多项目采用,运行在各种场景下,实用性更强,场景更广,同
包含目录:#include 中headerfile.h的搜索目录。如果有XXX.h找不到,设置这个目录可以解决。附加依赖项:C++的库会把函数、类的声明放在*.h中,实现放在*.cpp或*.cc中。编译之后,*.cpp,*.cc,*.c会被打包成一个.lib文件,这样可以保护源代码。所以,要使用一个库,除了要include他的头文件以外,还要在链接过程中把lib加进去。这个就是在附加链_vc 链接器 输入 附加依赖项 作用
步骤一@1.阿里巴巴矢量图标库@2.点击加入购物车并下载到本地及解压压缩包步骤二在main.js中导入//引入全局得css样式import './static/uniappIcon/font_2377837_h6osbf613td/iconfont.css'步骤三在页面中使用<text class="iconfont icon-arrow-right list_text"></text>..._uniapp emoji字体
hello_unity webgl 怎么释放内存
报错内容:Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this program 翻译:JavaHOHE和JRYHOST环境变量都不被定义为运行这个程序需要至少一个环境...
枚举是一个被命名的整型常数的集合,用于声明一组带标识符的常数。枚举在曰常生活中很常见,例如一个人的性别只能是“男”或者“女”,一周的星期只能是 7 天中的一个等。类似这种当一个变量有几种固定可能的取值时,就可以将它定义为枚举类型。在 JDK 1.5 之前没有枚举类型,那时候一般用接口常量来替代。而使用 Java 枚举类型 enum 可以更贴近地表示这种常量。声明枚举声明枚举时必须使用 e..._java 在类中声明数字枚举类型
Redis 集合(Set)Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。实例redis 127.0.0.1:6379> SADD...
在我的上一篇帖子中,我遇到了static在头文件中声明,,但不能为外部文件所修改的问题【参见http://www.rupeng.com/forum/thread-11904-1-1.html】经多方面的思考,,现解答如下【请指点】关于static :问题再现://*********************main.cpp#include #include"main.h"void fuc();//文_c++另一个文件中修改static
java 文件下载 中文文件名不支持导致路径不对//浏览器设置String userAgent = request.getHeader("User-Agent");if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { //IE浏览器处理 productName = java.net.URLE...