免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3509 | 回复: 8
打印 上一主题 下一主题

如何用grep,sed实现awk的列查找功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-18 11:38 |只看该作者 |倒序浏览
rt
awk '{print $1}'
用grep,sed该怎么实现?

论坛徽章:
0
2 [报告]
发表于 2007-05-18 12:19 |只看该作者

  1. sed 's/^\([^ ]*\).*$/\1/'
复制代码


  1. grep -o '^\([^ ]*\)[ ]'
复制代码

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2007-05-18 12:40 |只看该作者
为什么要这样?没有awk,没有cut?

论坛徽章:
0
4 [报告]
发表于 2007-05-18 12:48 |只看该作者
原帖由 FinalBSD 于 2007-5-18 12:40 发表
为什么要这样?没有awk,没有cut?


同意。

特定的任务就是要专用的工具。

论坛徽章:
0
5 [报告]
发表于 2007-05-18 12:51 |只看该作者
比如sed也可以实现显示匹配记录到之前的若干行,但是实现起来很麻烦。
不如 grep -B 一个参数来的直接。

论坛徽章:
0
6 [报告]
发表于 2007-05-18 12:58 |只看该作者
还有看到有人用sed模拟实现整数计算,也很麻烦,用途也估计是学术探讨,或特殊场合。
awk, expr, bc很容易就可以解决脚本计算。


  1. #! /bin/sed -f

  2. # algorithm by :
  3. # Bruno <Haible@ma2s2.mathematik.uni-karlsruhe.de>

  4. # incrementing one number, is just add 1 to first digit, i.e. replacing
  5. # it by the following digit
  6. #
  7. # there is one exception, when carry does happen, on that case, all
  8. # following digits must be added with one
  9. #
  10. # now this solution by `Bruno <Haible@ma2s2.mathematik.uni-karlsruhe.de>'
  11. # is very clever and smart
  12. #
  13. # the only way to happen carry, is when the first digit is a 9
  14. # all others cases are just fine
  15. #
  16. # for a number beginning with any digit except 9, just replace it (the digit)
  17. # by the next digit, for each number beginning with a 9, just "remove" it and
  18. # proceed as above for all others, i.e. all leadings 9s are "removes" until
  19. # a non-9 is found, if any 9 did not remain, a 0 is insert

  20. # replace all leading 9s by _ (any other char except digits, could be used)
  21. #
  22. :d
  23. s/9\(_*\)$/_\1/
  24. td

  25. # if there aren't any digits left, add a MostSign Digit 0
  26. #
  27. s/^\(_*\)$/0\1/

  28. # incr last digit only - there is no need for more
  29. #
  30. s/8\(_*\)$/9\1/
  31. s/7\(_*\)$/8\1/
  32. s/6\(_*\)$/7\1/
  33. s/5\(_*\)$/6\1/
  34. s/4\(_*\)$/5\1/
  35. s/3\(_*\)$/4\1/
  36. s/2\(_*\)$/3\1/
  37. s/1\(_*\)$/2\1/
  38. s/0\(_*\)$/1\1/

  39. # replace all _ to 0s
  40. #
  41. s/_/0/g
复制代码

论坛徽章:
0
7 [报告]
发表于 2007-05-18 15:07 |只看该作者
谢谢了,我的特定环境下确实没有awk,看来还是有很多要学习的地方。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2007-05-18 16:47 |只看该作者
如果文件不是很大, 不考虑效率的话 利用shell也可以, 例如:

  1. while read line;do set -- $line;echo $1;done<urfile
复制代码

#可以用重置IFS指定分隔符

论坛徽章:
0
9 [报告]
发表于 2007-05-19 09:19 |只看该作者
  1. sed 's,[[:space:]].*,,' urfile
  2. grep -o '[^ \t]' urfile
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP