免费注册 查看新帖 |

Chinaunix

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

[求助]一个有关awk的应用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-28 22:19 |只看该作者 |倒序浏览
我现在有一个文件,内容如下:


2011-02-08 weru  239874        2394
2011-02-09 35          234                eoir
2011-02-10 soiwo 235433        2fu9
2011-02-08 joi   jfw                cowq

需要按第一域把记录分到相应的单独的文本文件中去。
比如第一域时间如果等于2011-02-08,则把相应记录放到名为2011-02-08的文件中去。求代码~~~!

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2011-06-28 22:22 |只看该作者
回复 1# 紫OS琨
  1. awk '{print >$1".txt"}' urfile
复制代码

论坛徽章:
0
3 [报告]
发表于 2011-06-28 23:22 |只看该作者
yinyuemi 发表于 2011-06-28 22:22

这个第四行为什么没有把第一行的覆盖掉?

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
4 [报告]
发表于 2011-06-28 23:33 |只看该作者
回复 3# 251995427


   
print items > output-file
    This type of redirection prints the items onto the output file output-file. The file name output-file can be any expression. Its value is changed to a string and then used as a file name (see section Actions: Expressions).

    When this type of redirection is used, the output-file is erased before the first output is written to it. Subsequent writes do not erase output-file, but append to it. If output-file does not exist, then it is created.

    For example, here is how one awk program can write a list of BBS names to a file `name-list' and a list of phone numbers to a file `phone-list'. Each output file contains one name or number per line.

    awk '{ print $2 > "phone-list"
           print $1 > "name-list" }' BBS-list

print items >> output-file
    This type of redirection prints the items onto the output file output-file. The difference between this and the single-`>' redirection is that the old contents (if any) of output-file are not erased. Instead, the awk output is appended to the file.

论坛徽章:
3
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:51:162015年亚洲杯之阿曼
日期:2015-04-07 20:00:59
5 [报告]
发表于 2011-06-29 11:43 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
16
IT运维版块每日发帖之星
日期:2015-08-24 06:20:00综合交流区版块每日发帖之星
日期:2015-10-14 06:20:00IT运维版块每日发帖之星
日期:2015-10-25 06:20:00IT运维版块每日发帖之星
日期:2015-11-06 06:20:00IT运维版块每日发帖之星
日期:2015-12-10 06:20:00平安夜徽章
日期:2015-12-26 00:06:302016猴年福章徽章
日期:2016-02-18 15:30:34IT运维版块每日发帖之星
日期:2016-04-15 06:20:00IT运维版块每日发帖之星
日期:2016-05-21 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:002015七夕节徽章
日期:2015-08-21 11:06:17IT运维版块每日发帖之星
日期:2015-08-14 06:20:00
6 [报告]
发表于 2011-06-29 14:21 |只看该作者
回复 3# 251995427


    好像和close有关,你close,然后再试试看

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
7 [报告]
发表于 2011-06-30 04:38 |只看该作者
回复 6# expert1


    多谢斑竹指点

  1. seq 10 |awk '{print >"s"}{close("s")}'

  2. cat s
  3. 10

  4. seq 10 |awk '{print >>"s"}{close("s")}'

  5. cat s
  6. 10
  7. 1
  8. 2
  9. 3
  10. 4
  11. 5
  12. 6
  13. 7
  14. 8
  15. 9
  16. 10
复制代码

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
8 [报告]
发表于 2011-06-30 09:39 |只看该作者
awk '{print >>$1}' urfile

论坛徽章:
0
9 [报告]
发表于 2011-06-30 10:17 |只看该作者
本帖最后由 raohongfu 于 2011-06-30 10:24 编辑

二楼的强

论坛徽章:
0
10 [报告]
发表于 2011-07-07 10:08 |只看该作者
回复  251995427
yinyuemi 发表于 2011-06-28 23:33



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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP