免费注册 查看新帖 |

Chinaunix

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

复制文件的行并添加索引(copy lines and add index) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-12 03:10 |只看该作者 |倒序浏览
由于测试需要,要将导出的csv(逗号)格式文件中的现有的一些行追加(复制)在同一文件的后面,之后,需要给所有的行添加索引。下面的脚本可以实现简单的功能,
一些提示,
1。)cvs中已有的行假设已经有索引,如下,
    ...
    1,deea,des,aer3,3432,asd...
    2,43asrl,fdw,er,wer, dfa...
    ...
2。)awk的域分割符,设定为逗号( NF=“,” )
3。)一些参数必须在执行脚本的命令行中给定。
    所需参数,
    a.)起始行的行号;
    b.)终止行的行号;
    (通过这两个行号,可以queding要被复制的范围,注意起始行应小余终止行的行号,程序中没有对子进行判断)
    c.)循环的次数,即,(终止行-起始行+1)*婚还次数=要复制的行数
    d.)文件的名称(如必要请代路径, 它这里是数据源同时又是数据输出目的)。
##########################################
##  
## Name:  CopyLineToNr
##
## Desc:  there is a file, a few lines are
##      already in here, they are indexed as well.
##      it is required to copy the existing lines
##      and append them to the same file
##      till given number, that means the newly
##      appended lines must be indexed.
##
##OneLine Command:
##      awk -F":" '{sub($1, ++COUNT); print ;}'
##
##
##Command Format:
##     awk -f CopyLineToNr -v args=starNr,endNr,nrLoop,xyz xyz
##      example:
##        awk -f CopyLineToNr -v args=2,4,10,record.lst record.lst
##
BEGIN{
    #clear screen
    system("tput");
   
    #print help information
    print ("\n====Help====\narguments: \n1.)startLine=start line number; \n2.)endLine=end line number; \n3.)nrGiven=loop nr.; \n4.)fileName=file name; \n\n ");
    #split argument
    split(args, a,",");
    print ("\nGiven Arguments: ","Start="a[1], "End="a[2], "Loop Nr.="a[3], "File Name="a[4], "\n");
    print ("==============Performing copying lines to given number=================");
        
    #do loop to copy lines into same file
    for (i = 1; i>"a[4] );
    }
    print ("===========================Starting====================================");
    COUNT = 1;
    FS = ",";
   
}
{
    #using count to replace the first field (indexed)
    sub($1, COUNT);
    #print the line (already indexed)
    print ;
    #index incremented
    COUNT++;
}
END{
    print ("=============================Ending====================================");
}
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/45602/showart_420036.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP