- 论坛徽章:
- 0
|
由于测试需要,要将导出的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 |
|