使用awk和sed获取文件奇偶数行的方法总结
测试文件test.file
[root@localhost ~]# cat test.file
111111111111111
222222222222222
333333333333333
444444444444444
555555555555555
666666666666666
777777777777777
888888888888888
999999999999999
1010101010101010
1) 打印奇数行的方法
[root@localhost ~]# sed -n '1~2p' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999 [root@localhost ~]# sed -n 'p;n' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999 [root@localhost ~]# sed -n '1,$p;n' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999 [root@localhost ~]# sed '2~2d' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999 [root@localhost ~]# awk 'NR%2==1' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999 [root@localhost ~]# awk 'NR%2' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999 [root@localhost ~]# awk 'i=!i' test.file
111111111111111
333333333333333
555555555555555
777777777777777
999999999999999
2) 打印偶数行的方法
[root@localhost ~]# sed -n '2~2p' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010 [root@localhost ~]# sed -n 'n;p' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010 [root@localhost ~]# sed -n '1,$n;p' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010 [root@localhost ~]# sed '1~2d' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010 [root@localhost ~]# awk 'NR%2==0' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010 [root@localhost ~]# awk '!(NR%2)' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010 [root@localhost ~]# awk '!(i=!i)' test.file
222222222222222
444444444444444
666666666666666
888888888888888
1010101010101010
打印奇偶行的方法总结
sed -n '1~2p' test.file 打印奇数行
sed -n '2~2p' test.file 打印偶数行 sed -n 'p;n' test.file 打印奇数行
sed -n 'n;p' test.file 打印偶数行 sed -n '1,$p;n' test.file 打印奇数行
sed -n '1,$n;p' test.file 打印偶数行 sed '2~2d' test.file 打印奇数行
sed '1~2d' test.file 打印偶数行 awk 'NR%2==1' test.file 打印奇数行
awk 'NR%2==0' test.file 打印偶数行 awk 'NR%2' test.file 打印奇数行
awk '!(NR%2)' test.file 打印偶数行 awk 'i=!i' test.file 打印奇数行
awk '!(i=!i)' test.file 打印偶数行
其他相关正则取值说明
1) 打印行号和内容
[root@localhost ~]# awk '{print NR":"$0}' test.file
1:111111111111111
2:222222222222222
3:333333333333333
4:444444444444444
5:555555555555555
6:666666666666666
7:777777777777777
8:888888888888888
9:999999999999999
10:1010101010101010 2) 每行间加一个空行
[root@localhost ~]# awk '1; { print "" }' test.file
111111111111111 222222222222222 333333333333333 444444444444444 555555555555555 666666666666666 777777777777777 888888888888888 999999999999999 1010101010101010 [root@localhost ~]# awk '1 { print } { print "" }' test.file
111111111111111 222222222222222 333333333333333 444444444444444 555555555555555 666666666666666 777777777777777 888888888888888 999999999999999 1010101010101010 [root@localhost ~]# awk '{ print } { print "" }' test.file
111111111111111 222222222222222 333333333333333 444444444444444 555555555555555 666666666666666 777777777777777 888888888888888 999999999999999 1010101010101010 [root@localhost ~]# awk 'BEGIN { ORS="\n\n" }; 1' test.file
111111111111111 222222222222222 333333333333333 444444444444444 555555555555555 666666666666666 777777777777777 888888888888888 999999999999999 1010101010101010 3) 仅输出非空行,并每行间在加一个空行
# awk 'NF { print $0 "\n" }' test.file
NF表示当前行的字段数,$0表示当前行,最后再加一个换行 4) 双倍行距;没行间两个空行
默认输出后会换行的,输出\n,则会输出两个空白行
[root@localhost ~]# awk '1; { print "\n" }' test.file
111111111111111 222222222222222 333333333333333 444444444444444 555555555555555 666666666666666 777777777777777 888888888888888 999999999999999 1010101010101010 [root@localhost ~]# awk '{ print; print "\n" }' test.file
111111111111111 222222222222222 333333333333333 444444444444444 555555555555555 666666666666666 777777777777777 888888888888888 999999999999999 1010101010101010 5) 显示当前行在所在文件中的行号
FNR,表示当前行在文件中的行号
[root@localhost ~]# awk '{ print FNR "\t" $0 }' test.file
1 111111111111111
2 222222222222222
3 333333333333333
4 444444444444444
5 555555555555555
6 666666666666666
7 777777777777777
8 888888888888888
9 999999999999999
10 1010101010101010 6) 显示当前行在本次处理过程中的行号
NR,表示当前行在本次处理过程中的行号
[root@localhost ~]# awk '{ print NR "\t" $0 }' test.file
1 111111111111111
2 222222222222222
3 333333333333333
4 444444444444444
5 555555555555555
6 666666666666666
7 777777777777777
8 888888888888888
9 999999999999999
10 1010101010101010 为啥有FNR和NR的差别呢?效果不都是一样么?
如果使用两个文件filname1.ext filname2.ext,则就会看到差别了。
原来:FNR,是每个文件中的,换了一个文件,会归零;而NR则每个文件会累加起来的 7) 使用简单样式来输出
下面表示"行号占用5位,不足补空格"
[root@localhost ~]# awk '{ printf("] : %s\n", NR, $0) }' test.file
] : 1
] : 2
] : 3
] : 4
] : 5
] : 6
] : 7
] : 8
] : 9
] : 10 8) 显示非空行
# awk 'NF { $0=++a " :" $0 }; { print }' test.file
NF前面说了,表示当前行的行号,此处用他作为条件,如果是空行,则NF为0,跳过;否则,用动态变量a存储非空行的数目 9) 计算行数:效果类似wc -l
END表示每行都处理完了后,在执行,此时NR就是最后一行的行号,也就是总的行数了。
#awk 'END { print NR }' test.file 10) 计算每一行的和
s用作每行和的累加,从1到NF(每行总的字段数),依次累加
# awk '{ s = 0; for (i = 1; i <= NF; i++) s = s+$i; print s }' test.file 11) 计算文件中所有字段的和
s用作总和的累加,每行都处理完成了,再输出s;注意和10对比,此处没有每行清零,所以累加了。没有设置的变量,默认为空,
但是会根据上下文数值计算情况自动变为0
# awk '{ for (i = 1; i <= NF; i++) s = s+$i }; END { print s }' test.file 12) 将每个字段用其绝对值代替
$i表示当前行中的字段,$0表示当前行,可以改变$i的值
# awk '{ for (i = 1; i <= NF; i++) if ($i < 0) $i = -$i; print }' test.file 13) 计算文件中总的字段和(例如计算单词数)
# awk '{ total = total + NF }; END { print total }' test.file 14) 计算匹配指定信息的总行数
# awk '/Linux/ { n++ }; END { print n+0 }' test.file 15) 找到文件中每行第一个字段中,最大的数,以及其所在的行
用max存储最大的数,maxline存储最大数所在的行,并在最后输出
# awk '$1 > max { max=$1; maxline=$0 }; END { print max, maxline }' test.file 16) 显示当前行的字段数,并输出当前行
# awk '{ print NF ":" $0 } ' test.file 17) 显示每行最后一个字段的内容
# awk '{ print $NF }' test.file #NF表示当前行的字段数,例如为3,则$NF,就是$3,也就是第三个字段了 18) 显示最后一行的最后一个字段
每行处理没有输出,尽在最后输出,field作为每行的最后一行的暂存变量
# awk '{ field = $NF }; END { print field }' test.file 19) 显示字段数小于4的行
# awk 'NF < 4' test.file 20) 显示每行的最后一个字段小于4的行
# awk '$NF < 4' test.file
shell脚本,实现奇数行等于偶数行
[root@localhost ~]# cat kevin.file
aa
11
bb
22
cc
33
dd
44 使用awk命令可以这样实现:
[root@localhost ~]# awk 'NR%2==0{print a"="$0}{a=$0}' kevin.file
aa=11
bb=22
cc=33
dd=44 使用shell脚本可以这样实现:
[root@localhost ~]# cat kevin.sh
#!/bin/bash
n=0
for i in $(cat /root/kevin.file)
do
n=$(($n+1))
[ $((n%2)) -eq 1 ] && echo -n $i=
[ $((n%2)) -eq 0 ] && echo $i
done [root@localhost ~]# sh kevin.sh
aa=11
bb=22
cc=33
dd=44
使用awk和sed获取文件奇偶数行的方法总结的更多相关文章
- (转)CSS3:nth-child()伪类选择器,奇偶数行自定义样式first-child
原文地址 Table表格奇偶数行定义样式: CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好的标准只有得到行业浏览器的良好支持才算得上“标准”.CSS3标 准已提出数年,但 ...
- linux 下获取文件最后几行
在Linux下,获取文件倒数几行的命令是: tail -n 10 your_filename #获取倒数10行
- php获取文件后缀的9种方法
获取文件后缀的9种方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...
- CSS3:nth-child()伪类选择器,Table表格奇偶数行定义样式
转自爱设计 原文链接http://www.dangshopex.com/jishufenxiang/WEBkaifajishu/8653.html CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为 ...
- awk、sed处理文件的简单例子
awk.sed对处理日志文件和写shell脚本时非常有益.这个东西,如果不经常操作,真心过一段时间就忘差不多..要掌握熟练,就要多练习,这没什么可说的. awk '条件{命令}' filename 假 ...
- shell获取文件最后100行,开头100行,指定开始行和结束行的内容
文件最后100行:tail -n100 filePath: 文件开头100行:head -n100 filePath: 文件指定开始行和结束行的内容:sed '1,100p' filePath: 文件 ...
- php获取文件mime类型Fileinfo等方法
前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaW ...
- PHP获取文件扩展名的多种方法
PHP获取文件扩展名的N种方法. 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1): } 第2种方法: fun ...
- Java获取文件Content-Type的四种方法
HTTP Content-Type在线工具 有时候我们需要获取本地文件的Content-Type,已知 Jdk 自带了三种方式来获取文件类型. 另外还有第三方包 Magic 也提供了API.Magic ...
随机推荐
- ruby module extend self vs module_funciton
最近学习ruby过程中,extend self 跟 module_function 傻傻分不清楚,查资料后明白之间的差别,虽记录之,原文地址 github module A extend self d ...
- Jquery操作文档标签
1.插入动作 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- c++中二级指针的使用场景
二级指针的使用场景如下: 1.主要用来为指针变量分配内存空间: void GetMemory(char **p) { *p = ]; } 函数调用方式: char *str = NULL; GetMe ...
- Python核心团队计划2020年停止支持Python2,NumPy宣布停止支持计划表
Python核心团队计划在2020年停止支持Python 2.NumPy项目自2010年以来一直支持Python 2和Python 3,并且发现支持Python 2对我们有限的资源增加了负担:因此,我 ...
- HDU5813 Elegant Construction
Elegant Construction Time Li ...
- LeetCode刷题:第一题 两数之和
从今天开始刷LeetCode 第一题:两数之和 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种 ...
- Windows 10 IoT Core 17133 for Insider 版本更新
今天,微软发布了Windows 10 IoT Core 17133 for Insider 版本更新,本次更新只修正了一些Bug,没有发布新的特性.用户可以登录Windows Device Porta ...
- 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框)
[源码下载] 背水一战 Windows 10 (105) - 通知(Toast): 带按钮的 toast, 带输入的 toast(文本输入框,下拉选择框) 作者:webabcd 介绍背水一战 Wind ...
- 包建强的培训课程(12):iOS深入学习(内存管理、Block和GCD等)
@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=Syn ...
- 使用Python多渠道打包apk
使用Python生成多渠道包 往apk包中追加到一个空文件到META-INF目录以标识渠道,Android中获取此文件即可获得App的下载渠道 首先在info文件夹新建一个qdb.txt的空文本文件 ...