免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yinyuemi
打印 上一主题 下一主题

[学习共享] awk初学之常见问题 [复制链接]

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
61 [报告]
发表于 2012-07-13 23:43 |只看该作者
本帖最后由 yinyuemi 于 2012-07-13 23:44 编辑

回复 60# guoweiqust


   
Gawk executes AWK programs in the following order.  First, all variable assignments specified via the -v
       option  are  performed.  Next, gawk compiles the program into an internal form. Then, gawk executes the
       code in the BEGIN block(s) (if any), and then proceeds to read each file named in the  ARGV  array.   If
       there are no files named on the command line, gawk reads the standard input.

论坛徽章:
0
62 [报告]
发表于 2012-07-15 15:45 |只看该作者
本帖最后由 guoweiqust 于 2012-07-15 15:54 编辑

回复 61# yinyuemi

看完后我的理解是这样的:
如果awk没有输入文件的话,从stdin读入信息;如果存在input file的话,直接就就读入input file的内容进行处理
但是,在含有BEGIN块的情况下,无论是否存在input file,awk都会先执行BEGIN块的action,执行完BEGIN后,
如果后面还有action这时必须需要input(END模块的执行必须要有input  )

以上理解正确吗?谢谢

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
63 [报告]
发表于 2012-07-15 23:52 |只看该作者
回复 62# guoweiqust


    是这个意思,有兴趣的话,可以再了解下gawk 4.0的BEGINFILE 和ENDFILE 模块的用法,处理多文件时可以尝试使用,有时会有奇效
http://www.gnu.org/software/gawk/manual/html_node/BEGINFILE_002fENDFILE.html

论坛徽章:
0
64 [报告]
发表于 2012-07-16 09:16 |只看该作者
本帖最后由 guoweiqust 于 2012-07-16 09:16 编辑

回复 63# yinyuemi

没怎么看懂~~
有例子学起来就容易多了,呵呵~~
man gawk得到的结果中没提到BEGINFILE,ENDFILE
   

论坛徽章:
0
65 [报告]
发表于 2012-07-16 11:10 |只看该作者
本帖最后由 guoweiqust 于 2012-07-16 11:11 编辑
  1. [sam@chenwy sam]$ echo "65" | awk '{printf "%c\n",$0}'
  2. A
复制代码
可以得到A,但是反过来怎么不行呢?
  1. x900010-p0024@login1-58% awk 'BEGIN{printf "%d\n",A}'
  2. 0
复制代码

论坛徽章:
0
66 [报告]
发表于 2012-07-16 14:07 |只看该作者
高手云集啊,向大家学习。

论坛徽章:
0
67 [报告]
发表于 2012-07-16 17:25 |只看该作者
回复 1# yinyuemi

请教个问题
awk中的数组是不是和perl中的hash有点类似
   

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
68 [报告]
发表于 2012-07-16 23:38 |只看该作者
回复 67# guoweiqust


   使用上应该是差不多的,参考http://bbs.chinaunix.net/thread-2312439-1-1.html

论坛徽章:
0
69 [报告]
发表于 2012-07-17 17:20 |只看该作者
本帖最后由 guoweiqust 于 2012-07-17 17:23 编辑

回复 1# yinyuemi

初学shell,在看shell基础十二篇,关于find有个地方没有看懂,求赐教。。
  1. find /usr/sam -path "/usr/sam/dir1" -prune -o -name datafile -print  
复制代码
当-path "/usr/sam/dir1" -prune为假时执行-o后面的,我搞不清楚这里的真假的判断是什么

/usr/sam/dir1这个文件夹是存在的
(-path "/usr/sam/dir1" -prune)这条语句本身为什么是假?

论坛徽章:
0
70 [报告]
发表于 2012-07-17 17:24 |只看该作者
回复 68# yinyuemi
谢谢指点。。受教了

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP