awk书上练习
文件car:
plym fury
chevy malibu
ford mustang
volvo s80
ford thundbd
chevy malibu
bmw 325i
honda accord
ford taurus
toyota rav4
chevy impala
ford explor
基本
awk '{print}' car #类似标准输出
awk '/chevy/' car #包含字符串的所有文本行进行复制
chevy malibu
chevy malibu
chevy impala
dahu@dahu-OptiPlex-:~/myfile$ awk '/10/' car #只要包含,以字符串的形式
ford mustang
volvo s80
ford thundbd
ford taurus
dahu@dahu-OptiPlex-:~/myfile$ awk '{print $1,$3}' car #显示第一列,空格,第三列
dahu@dahu-OptiPlex-:~/myfile$ awk '/chevy/{print $1,$3}' car #匹配行,选中列
chevy
chevy
chevy
dahu@dahu-OptiPlex-:~/myfile$ awk '$2 ~ /[0-9]/' car #第二列匹配某种正则,最后显示所有行
volvo s80
bmw 325i
toyota rav4
dahu@dahu-OptiPlex-:~/myfile$ awk '$2 ~ /^[tm]/{print $3,$2,"$"$5}' car #第2个字段匹配正则,并按要求显示
malibu $
mustang $
thundbd $
malibu $
taurus $
dahu@dahu-OptiPlex-:~/myfile$ awk '$5<=3000' car #按大小查找
plym fury
chevy malibu
bmw 325i
toyota rav4
chevy impala
dahu@dahu-OptiPlex-:~/myfile$ awk '/volvo/,/bmw/' car #匹配两个之间的行
volvo s80
ford thundbd
chevy malibu
bmw 325i
dahu@dahu-OptiPlex-:~/myfile$ awk '/chevy/,/ford/' car #贪心,尽可能多的获取行,和上面例子类似
chevy malibu
ford mustang
chevy malibu
bmw 325i
honda accord
ford taurus
chevy impala
ford explor
awk -f file car
file 里可以写awk程序,也就是上面引号的内容,不需要加引号.
BEGIN
dahu@dahu-OptiPlex-:~/myfile$ cat aaa #前缀
BEGIN{
print "Make Mode Year Miles Price"
print "----------------------------"
}
{print}
dahu@dahu-OptiPlex-:~/myfile$ awk -f aaa car
Make Mode Year Miles Price
----------------------------
plym fury
chevy malibu
ford mustang
volvo s80
ford thundbd
chevy malibu
bmw 325i
honda accord
ford taurus
toyota rav4
chevy impala
ford explor
length
dahu@dahu-OptiPlex-:~/myfile$ awk '{print length,$0}' car|sort -n #显示每一行的字符数(包含空格的数量),并按顺序排序,length后也可加括号
bmw 325i
plym fury
volvo s80
ford explor
toyota rav4
chevy impala
chevy malibu
chevy malibu
ford taurus
honda accord
ford mustang
ford thundbd
NR
dahu@dahu-OptiPlex-:~/myfile$ awk '{print NR,$0}' car #显示行数,NR记录编号,NF字段数目
plym fury
chevy malibu
ford mustang
volvo s80
ford thundbd
chevy malibu
bmw 325i
honda accord
ford taurus
toyota rav4
chevy impala
ford explor
dahu@dahu-OptiPlex-:~/myfile$ awk 'NR==2,NR==4' car #显示第2行到第4行的内容,太方便了!
chevy malibu
ford mustang
volvo s80
END
dahu@dahu-OptiPlex-:~/myfile$ awk 'END {print NR,"cars for sale"}' car #end表示数据已处理之后,此时NR就是总行数了
cars for sale
if
dahu@dahu-OptiPlex-:~/myfile$ cat aaa #if的简短用法,没有使用花括号
{
if ($ ~ /ply/) $ ="plymouth"
if ($ ~ /chev/) $ ="chevrolet"
}
dahu@dahu-OptiPlex-:~/myfile$ awk -f aaa car
plymouth fury
chevrolet malibu
ford mustang
volvo s80
ford thundbd
chevrolet malibu
bmw 325i
honda accord
ford taurus
toyota rav4
chevrolet impala
ford explor
dahu@dahu-OptiPlex-:~/myfile$ cat aaa #程序稍加改进,把awk程序文件直接改成可直接运行的,增加一下可执行的权限chmod 744 aaa
#!/usr/bin/awk -f
{
if ($ ~ /ply/) $ ="plymouth"
if ($ ~ /chev/) $ ="chevrolet"
}
dahu@dahu-OptiPlex-:~/myfile$ ./aaa car
dahu@dahu-OptiPlex-:~/myfile$ cat price_range
#{ #这边是将第五列改成评价
#if ($5 <=5000) $5="cheap";
#else if (5000<$5 && $5<10000) $5="please ask";
#else if ($5>=10000) $5="expensive";
#print $
#}
BEGIN{
s="cheap"
}
{ #保留第5列
if ($ <=) s="cheap";
else if (<$ && $<) s="please ask";
else if ($>=) s="expensive";
print $,s #显示的时候,注意$
}
dahu@dahu-OptiPlex-:~/myfile$ awk -f price_range car
plym fury cheap
chevy malibu cheap
ford mustang expensive
volvo s80 please ask
ford thundbd expensive
chevy malibu cheap
bmw 325i cheap
honda accord please ask
ford taurus expensive
toyota rav4 cheap
chevy impala cheap
ford explor please ask
OFS
什么样的情况下才会按照新格式输出呢?
dahu@dahu-OptiPlex-:~/myfile$ cat aaa #OFS输出字段分隔符,默认是空格,但是我改了之后发现只有满足if条件的才会按照新格式输出,如果注释掉了if,就都不会按照新格式输出,存疑.
#!/usr/bin/awk -f
BEGIN{OFS=" >> "}
{
if ($ ~ /ply/) $ ="plymouth"
if ($ ~ /chev/) $ ="chevrolet"
print $
} dahu@dahu-OptiPlex-:~/myfile$ ./aaa car
plymouth >> fury >> >> >>
chevrolet >> malibu >> >> >>
ford mustang
volvo s80
ford thundbd
chevrolet >> malibu >> >> >>
bmw 325i
honda accord
ford taurus
toyota rav4
chevrolet >> impala >> >> >>
ford explor
printf
dahu@dahu-OptiPlex-:~/myfile$ cat aaa #printf改善输出格式
#!/usr/bin/awk -f
BEGIN{
print " Miles"
print "Make Mode Year (000) Price "
print \
"--------------------------------------------"
}
{
if ($ ~ /ply/) $ ="plymouth"
if ($ ~ /chev/) $ ="chevrolet"
printf "%-10s %-8s %2d %5d $ %8.2f\n",\
$,$,$,$,$
} dahu@dahu-OptiPlex-:~/myfile$ ./aaa car
Miles
Make Mode Year () Price
--------------------------------------------
plymouth fury $ 2500.00
chevrolet malibu $ 3000.00
ford mustang $ 10000.00
volvo s80 $ 9850.00
ford thundbd $ 10500.00
chevrolet malibu $ 3500.00
bmw 325i $ 450.00
honda accord $ 6000.00
ford taurus $ 17000.00
toyota rav4 $ 750.00
chevrolet impala $ 1550.00
ford explor $ 9500.00
dahu@dahu-OptiPlex-:~/myfile$ cat redirect #重定向输出
#!/usr/bin/awk -f
/chevy/ {print > "chevfile"}
/ford/ {print > "fordfile"}
END {print "done."} dahu@dahu-OptiPlex-:~/myfile$ ./redirect car
done.
dahu@dahu-OptiPlex-:~/myfile$ cat chevfile
chevy malibu
chevy malibu
chevy impala
dahu@dahu-OptiPlex-:~/myfile$ cat fordfile
ford mustang
ford thundbd
ford taurus
ford explor
FS
输入字段分隔符
for
dahu@dahu-OptiPlex-:~/myfile$ cat manuf #for结构,第一列的内容放入这个字典
awk '{manuu[$1]++}
END {for (name in manuu) {print name ,manuu[name]}}' car #里面的花括号也可以不用加,命令之间用;间隔
dahu@dahu-OptiPlex-:~/myfile$ ./manuf
honda
bmw
volvo
ford
plym
chevy
toyota
dahu@dahu-OptiPlex-:~/myfile$ cat mmanuf #这个程序感觉厉害了
if [ $# != ]
then
echo "something wrong!"
exit
fi
awk < $ '
{count[$'$1']++} #注意这里,单引号成对出现,两端引号内容连接起来,中间还是直接引用传入的第一个参数,在这里是1,所以调用第一列的内容,666,在END里也试过,单引号随便加,反正连起来的.哪怕是把变量名拆掉也行...吊
END{for (item in count) print item,count[item]}
#END{fo''r (item in count) ''print it''em,count[item]} #你敢信吗?
'
dahu@dahu-OptiPlex-:~/myfile$ ./mmanuf car
./mmanuf: line : [: : unary operator expected
honda
bmw
volvo
ford
plym
chevy
toyota
实操:
1.统计不重复的个数:
xch27@lanzhou:/asrdata/users/ql826/lmwork/comm_cloud/aicar_solution/v28_24Apr2017/data/radio/slot$ awk 'BEGIN{FS="&"}{print $1}' slot.map |sort -u
CLASS-FM频道
CLASS-主持人
CLASS-序列号
CLASS-频道
CLASS-频道类型
2.统计某一项的个数:
xch27@lanzhou:/asrdata/users/ql826/lmwork/comm_cloud/aicar_solution/v28_24Apr2017/data/radio/slot$ awk 'BEGIN{FS="&";a=0}{if($1 =="CLASS-频道")a++}END{print a}' slot.map
114716
3.找到某几行的内容
dahu@dahu-OptiPlex-3046:~/Downloads$ awk '{if(NR<=4900 && NR>=4800)print $2}' enwords.oov.cnt-gt800.cnt-pron > en
4.匹配我要找的CLASS-XXX,且不重复,for的简单应用
xch27@lanzhou:/asrdata/users/xch27/lmwork/comm_cloud/aicar_solution/v30_23May2017/data/life/music/pat$ head gequ_geshou
<s> CLASS-动作二 CLASS-歌曲名 CLASS-歌手名 唱 的 </s>
<s> CLASS-动作二 CLASS-歌曲名 CLASS-歌手名 的 </s>
<s> CLASS-动作二 CLASS-歌曲名 CLASS-歌手名 的 </s>
<s> CLASS-动作二 CLASS-歌曲名 CLASS-歌手名 的 </s>
<s> CLASS-动作二 CLASS-歌手名 的 CLASS-语种 歌曲 CLASS-歌曲名 </s>
<s> CLASS-动作三 CLASS-歌曲名 的 歌词 CLASS-歌手名 唱 的 </s>
<s> CLASS-动作三 CLASS-歌曲名 的 歌词 CLASS-歌手名 的 </s>
<s> CLASS-动作三 CLASS-歌曲名 的 歌词 CLASS-歌手名 演唱 的 </s>
<s> CLASS-动作三 CLASS-歌手名 唱 的 CLASS-歌曲名 的 歌词 </s>
<s> CLASS-动作三 CLASS-歌手名 的 CLASS-歌曲名 的 歌词 </s>
xch27@lanzhou:/.../pat$ awk '{for(i=2;i<NF;i++)if($i ~ "CLASS-"){print $i}}' gequ_geshou |sort -u
CLASS-动作三
CLASS-动作二
CLASS-操作
CLASS-歌手名
CLASS-歌曲名
CLASS-语种
统计 不匹配"CLASS-"的行数
awk '{a=0;for(i=1;i<=NF;i++){if($i ~ "CLASS-"){a=1}};if(a!=1){count++}}END{print count}' music.comm.mrg.v3.pat.wseg
修改文件内容,多变量传递
#!/bin/bash
#awk传入变量练习,直接修改小麦
fs=`awk '/你好小迈/{print $2}' wakeup.logp`
#echo $fs
#多个变量这样添加
#p=
#echo |awk -v tt="$fs" -v tg="$p" 'BEGIN{print tt,tg}'
ft=`awk '/小迈你好/{print $2}' wakeup.logp`
awk -v nhxm="$fs" -v xmnh="$ft" '{if($1~"你好小麦"){a=nhxm+2;print $1,a}else if($1~"小麦你好"){a=xmnh+2;print $1,a}else{print $0}}' wakeup.logp >tmp
awk根据不同名称输入不同文件.知识点:FS,RS,substr,split,awk内部输出
xch27@taiyuan:/asrdata/users/xch27/lmwork/comm_cloud/aicar_solution/v32_30june2017/data/navi/slot$ head t1
名称:北京南顺油脂有限公司
拼音:BeiJing NanShun YouZhi YouXianGongSi
别称:
地址:良乡南肖庄道口西
类型:公司企业;公司;公司
省:北京市
市:北京市
区县:房山区
热度:9.6901977 xch27@taiyuan:/asrdata/users/xch27/lmwork/comm_cloud/aicar_solution/v32_30june2017/data/navi/slot$ awk 'BEGIN{FS="\n";RS="\n\n"}{a=substr($5,4);split(a,A,";");print substr($1,4)>>A[1];}' t1 xch27@taiyuan:/asrdata/users/xch27/lmwork/comm_cloud/aicar_solution/v32_30june2017/data/navi/slot$ ls
gaode.alldata..txt readme.sh tmp 体育休闲服务 公司企业 商务住宅 搜索词_0620.txt 生活服务 购物服务 餐饮服务
lineprocess.py t1 住宿服务 全量数据_0620.txt 医疗保健服务 地名地址信息 政府机构及社会团体 科教文化服务 金融保险服务
awk书上练习的更多相关文章
- JAVA理解逻辑程序的书上全部重要的习题
今天随便翻翻看以前学过JAVA理解逻辑程序的书上全部练习,为了一些刚学的学弟学妹,所以呢就把这些作为共享了. 希望对初学的学弟学妹有所帮助! 例子:升级“我行我素购物管理系统”,实现购物结算功能 代码 ...
- c++编程思想(四)--对象和隐藏(感觉书上有误)
c++编程思想里数据抽象和隐藏实现实际就是通常所说的类和封装: 封装,继承,多态对象特点说的很多,就不再说了 关于封装,本人觉得书上有个地方写的有问题,p145和p153都提到Y::f(X*)引用了X ...
- OK 开始实践书上的项目一:即使标记
OK 开始实践书上的项目一:及时标记 然而....又得往前面看啦! ----------------------我是分割线------------------------ 代码改变世界
- 关于node的基础理论,书上看来的
最近看了一本书,说了一些Node.js的东西,现在来记录一下,让自己记得更牢靠一点. 在书上,是这样介绍的:Node.js模型是源于Ruby的Event Machine 和 Python的Twiste ...
- OpenACC 书上的范例代码(Jacobi 迭代),part 3
▶ 使用Jacobi 迭代求泊松方程的数值解 ● 使用 data 构件,强行要求 u0 仅拷入和拷出 GPU 各一次,u1 仅拷入GPU 一次 #include <stdio.h> #in ...
- java代码流类。。程序怎么跟书上的结果不一样???
总结:这个程序很容易懂.的那是这个结果我觉得有问题啊..怎么“stop”后,输出的内容是输入过的呢? 应该是没有关系的呀,与输入的值是不同的....怎么书上运行的结果和我的不一样啊 package c ...
- java核心编程书上的一个错误
书上说这段代码说明了java对对象不是采用的按引用调用 这明显错了,java还是引用传递,只是把引用对象的变量复制了,互换了x,y所指的对象,对a,b没有影响
- shell脚本命令,一些你在书上找不到的命令。
1.!$<!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串.如:你可能会这样: $mkdir mydir$mv mydir yourdir$cd yourdir 可以改成: $mkd ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
随机推荐
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路
A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...
- HashCode与Equals回顾
HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键. 那么Java运行时环境是如何判断HashSet中相同对象.Ha ...
- 出现错误日志:The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path
tomcat6出现错误日志: 信息: The APR based Apache Tomcat Native library which allows optimal performance in ...
- 「Linux」centos7安装mysql
1.yum仓库下载MySQL:sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch. ...
- C语言 结构体传值与传址分析
/********************************************************************** * 版权所有 (C)2017, Wang maochun ...
- 2017北京国庆刷题Day7 afternoon
期望得分:100+30+100=230 实际得分:60+30+100=190 排序去重 固定右端点,左端点单调不减 考场上用了二分,没去重,60 #include<cstdio> #inc ...
- cmd下常用命令汇总
1.获得文件夹内所有文件的文件名列表 dir *.png /b>list.txt 其中: (1)*.png表示筛选后缀为.png的文件 (2)/b为输出的模式.如下: 引用 /b 只有文件名 ...
- VS调试程序快捷键和系统快捷键
调试程序快捷键 编译程序:F7 运行程序:ctrl + F5 打断点:F9 运行到断点位置:F5 单步执行:F10 单步进入函数:F11 结束调试:shift+F5 注释代码:ctrl+k,ctrl+ ...
- 【BZOJ】1707: [Usaco2007 Nov]tanning分配防晒霜
[算法]贪心扫描线(+堆) [题意]给定n头牛有区间[a,b],m个防晒霜值为ai,每个可以使用bi次,每次可以使包含它的区间涂到防晒霜,问最多被涂牛数. [题解] 参考:[bzoj1707]: [U ...
- 【CodeForces】915 E. Physical Education Lessons 线段树
[题目]E. Physical Education Lessons [题意]10^9范围的区间覆盖,至多3*10^5次区间询问. [算法]线段树 [题解]每次询问至多增加两段区间,提前括号分段后线段树 ...