我有一堆数据如下。
1,A9600,001_DIF,NA,TIME,startTime
2,A9600,002_DEP,NA,TIME,startTime
3,A9600,003_LIT,NA,TIME,startTime
4,A9600,004_ETC,NA,TIME,startTime
5,B9600,005_CMP,NA,TIME,startTime
6,B9600,006_IMP,NA,TIME,startTime
7,B9600,007_DIF,NA,TIME,startTime
我想让它们修改如下。
1,A9600,001_DIF,NA,TIME,startTime
1,A9600_1,001_DIF,NA,TIME,startTime
1,A9600_2,001_DIF,NA,TIME,startTime
1,A9600_3,001_DIF,NA,TIME,startTime
2,A9600,002_DEP,NA,TIME,startTime
2,A9600_1,002_DEP,NA,TIME,startTime
2,A9600_2,002_DEP,NA,TIME,startTime
2,A9600_3,002_DEP,NA,TIME,startTime
....
如何使用shell命令实现这一目标?
答案 0 :(得分:1)
This awk one-liner works for this problem. You can just change the c
variable to get different looping times.
awk -F',' -v c='3' -v OFS="," '7;{old=$2;for(i=1;i<=c;i++){$2=old"_"i;print}}' file
答案 1 :(得分:1)
这是一个Perl解决方案,也许它需要更多的增强,但这就是我所拥有的(任何更新都非常受欢迎:D)
cat file | perl -ne 'print "$_" x4' | perl -pe '$c++; $c eq 1 ? s/([a-zA-Z]\d+)/$1/g : s/([a-zA-Z]\d+)/$1_${\($c - 1)}/g;$c eq 4 ? $c=0: $c=$c'
file
应包含以下内容:
1,A9600,001_DIF,NA,TIME,startTime
2,A9600,002_DEP,NA,TIME,startTime
3,A9600,003_LIT,NA,TIME,startTime
4,A9600,004_ETC,NA,TIME,startTime
5,B9600,005_CMP,NA,TIME,startTime
6,B9600,006_IMP,NA,TIME,startTime
7,B9600,007_DIF,NA,TIME,startTime
输出如下:
1,A9600,001_DIF,NA,TIME,startTime
1,A9600_1,001_DIF,NA,TIME,startTime
1,A9600_2,001_DIF,NA,TIME,startTime
1,A9600_3,001_DIF,NA,TIME,startTime
2,A9600,002_DEP,NA,TIME,startTime
2,A9600_1,002_DEP,NA,TIME,startTime
2,A9600_2,002_DEP,NA,TIME,startTime
2,A9600_3,002_DEP,NA,TIME,startTime
3,A9600,003_LIT,NA,TIME,startTime
3,A9600_1,003_LIT,NA,TIME,startTime
3,A9600_2,003_LIT,NA,TIME,startTime
3,A9600_3,003_LIT,NA,TIME,startTime
4,A9600,004_ETC,NA,TIME,startTime
4,A9600_1,004_ETC,NA,TIME,startTime
4,A9600_2,004_ETC,NA,TIME,startTime
4,A9600_3,004_ETC,NA,TIME,startTime
5,B9600,005_CMP,NA,TIME,startTime
5,B9600_1,005_CMP,NA,TIME,startTime
5,B9600_2,005_CMP,NA,TIME,startTime
5,B9600_3,005_CMP,NA,TIME,startTime
6,B9600,006_IMP,NA,TIME,startTime
6,B9600_1,006_IMP,NA,TIME,startTime
6,B9600_2,006_IMP,NA,TIME,startTime
6,B9600_3,006_IMP,NA,TIME,startTime
7,B9600,007_DIF,NA,TIME,startTime
7,B9600_1,007_DIF,NA,TIME,startTime
7,B9600_2,007_DIF,NA,TIME,startTime
7,B9600_3,007_DIF,NA,TIME,startTime
<强>解释强>
perl -ne 'print "$_" x4'
:此部分负责根据file
复制x4
中的每一行,每行将打印4次
perl -pe '$c++; $c eq 1 ? s/([a-zA-Z]\d+)/$1/g : s/([a-zA-Z]\d+)/$1_${\($c - 1)}/g;$c eq 4 ? $c=0: $c=$c'
:此部分负责使用此正则表达式A9600
查找B9600
,[a-zA-Z]\d+
,...等模式,然后将_n
附加到其中根据{{1}}的值,每次将设置为0,以避免计算超出我们需要的数量。