免费注册 查看新帖 |

Chinaunix

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

解难题:如何把这样一个文件按照里面的内容分成许多小文件? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-08-03 02:34 |只看该作者 |倒序浏览
解难题:如何把这样一个文件按照里面的内容分成许多小文件?

  原文件只有一个:profile.txt
内容:
IDLihong_16542        76        84        92
Beijing        school        1982-1988
Tianjing        highschool        1998-1993
Beijing        BNU        1993-1998
IDChenying_26613        82        96        100
Shanghai        school        1983-1989
Shanghai        highschool        1989-1994
Beijing        QHU        1994-1998
Beijing      MPH         1998-now
IDZhangdong_15525        72        86        80
Shandong        school        1983-1989
Shandong        highschool        1989-1994
Beijing        BNU        1994-1998


。。。。。。。
编perl或shell程序把它分成:
文件1: Lihong_Record.txt
内容:
IDLihong_16542        76        84        92
Beijing        school        1982-1988
Tianjing        highschool        1998-1993
Beijing        BNU        1993-1998

文件2: Chenying_Record.txt
内容:
IDChenying_26613        82        96        100
Shanghai        school        1983-1989
Shanghai        highschool        1989-1994
Beijing        QHU        1994-1998

文件3: Zhangdong_Record.txt
内容:
IDZhangdong_15525        72        86        80
Shandong        school        1983-1989
Shandong        highschool        1989-1994
Beijing        BNU        1994-1998

.......

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-08-03 07:44 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

SCO UNIX
  1. #!/bin/sh
  2. cat file|sed -n '/^ID/p'|sed 's:^ID\([^_]*\)_.*$:\1:'|while read name
  3. do
  4. cat file|sed -n "/^ID${name}/,/^ID/p"|sed '$d' >${name}_record.txt
  5. done
复制代码

论坛徽章:
0
3 [报告]
发表于 2003-08-03 11:26 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

另一种办法.FreeBSD

  1. #!/usr/bin/awk -f
  2. {
  3.         if(match($0,/^ID[^_]*_/))
  4.                 fn=substr($0,3,RLENGTH-3)
  5.         print $0 >> ""fn"_Record.txt"
  6. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2003-08-03 12:45 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

楼主在perl版也发了同样的问题
偶用perl,一个命令行就搞掂它,嘿嘿
  1. perl -ne 'chomp;/^ID(.*)_/; `echo "$_" >> "$1_Record.txt"`;' profile.txt
复制代码

论坛徽章:
0
5 [报告]
发表于 2003-08-03 13:51 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

Powerplane's code don't work !!!!!!!!!!!
Every file only has one line, look:
--------------------------------------------------
$ ls
Chenying_Record.txt  profile.txt  Zhangdong_Record.txt
Lihong_Record.txt    _Record.txt

$ cat Chenying_Record.txt
IDChenying_26613 82 96 100

$ cat Zhangdong_Record.txt
IDZhangdong_15525 72 86 80

$ cat Lihong_Record.txt
IDLihong_16542 76 84 92

论坛徽章:
0
6 [报告]
发表于 2003-08-03 14:02 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

CONGRADULATION: Admirer is right !!!!!!!!!!!
look at the result:
------------------------------------------------------------------   

$ ls
Chenying_record.txt  div2               profile.txt
div1                 Lihong_record.txt  Zhangdong_record.txt

$ cat Lihong_record.txt
IDLihong_16542 76 84 92
Beijing school 1982-1988
Tianjing highschool 1998-1993
Beijing BNU 1993-1998

论坛徽章:
0
7 [报告]
发表于 2003-08-03 16:28 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

? !! I did test my program before I post.
Did you misreplace operator ">>" with operator ">", that will cause your every recrod file only one line.
">>" means : append content
">" means: write/rewrite content     
Why don't you just copy and paste my code on your machine, and execute it?

here is the result on my machine:
  1. echo 'for i in $(ls *_Record.txt); do echo [$i:];cat $i;echo; done' | sh
复制代码

[Chenying_Record.txt:]
IDChenying_26613 82 96 100
Shanghai school 1983-1989
Shanghai highschool 1989-1994
Beijing QHU 1994-1998
Beijing MPH 1998-now

[Lihong_Record.txt:]
IDLihong_16542 76 84 92
Beijing school 1982-1988
Tianjing highschool 1998-1993
Beijing BNU 1993-1998

[Zhangdong_Record.txt:]
IDZhangdong_15525 72 86 80
Shandong school 1983-1989
Shandong highschool 1989-1994
Beijing BNU 1994-1998

论坛徽章:
0
8 [报告]
发表于 2003-08-03 20:24 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

Powerplane: I am using Red Hat Linux 8.0 to test your code. The result is still as I posted here. Maybe your code doesn't work on Linux. By the way, What is your OS? Linux or Solaris?

论坛徽章:
0
9 [报告]
发表于 2003-08-03 20:33 |只看该作者

解难题:如何把这样一个文件按照里面的内容分成许多小文件?

OS shouldn't be a problem to perl.
I use freebsd 5.1,and the version of perl is 5.6.1.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP