在每行的开头添加数字,每第n行增加1

时间:2021-07-14 06:50:30

Starting with number 1, how to add this number in front of the direct following next x lines, followed by number 2 also for the following next x lines, and so on, end must be by y lines, how to do that?

从数字1开始,如何在直接跟随下面的x行前面添加这个数字,接着是下面的x行后面的数字2,依此类推,end必须是y行,怎么做?

1 jeriro ieieie ieiue
1 ieirirp wzwezeg
1 ieueujueu ueuuwiuyh
2 iejejrökx lek
2 kejejhejhe pmys
2 krejrjhrjh hegehe
3 ririrjfjf
3 iririr iezete
3 pgogto

1 jeriro ieieie ieiue 1 ieirirp wzwezeg 1 ieueujueu ueuuwiuyh2iejejrökxlek2 kejejhejhe pmys 2 krejrjhrjh hegehe 3 ririrjfjf 3 iririr iezete 3 pgogto

4 个解决方案

#1


$ awk -v x=3 '{print (NR%x?c+1:++c), $0}' file
1 jeriro ieieie ieiue
1 ieirirp wzwezeg
1 ieueujueu ueuuwiuyh
2 iejejrökx lek
2 kejejhejhe pmys
2 krejrjhrjh hegehe
3 ririrjfjf
3 iririr iezete
3 pgogto

$ awk -v x=4 '{print (NR%x?c+1:++c), $0}' file
1 jeriro ieieie ieiue
1 ieirirp wzwezeg
1 ieueujueu ueuuwiuyh
1 iejejrökx lek
2 kejejhejhe pmys
2 krejrjhrjh hegehe
2 ririrjfjf
2 iririr iezete
3 pgogto

#2


Hope it helps !!!

希望能帮助到你 !!!

-bash-4.1$ awk -v a=3 -v b=1 'c<a{print b $0; c+=1} c==a{c=0;b+=1}' file
1asdasdasd
1asdas
1asd
2asd
2asd
2asd
3as
3asd
-bash-4.1$ awk -v a=4 -v b=1 'c<a{print b $0; c+=1} c==a{c=0;b+=1}' file
1asdasdasd
1asdas
1asd
1asd
2asd
2asd
2as
2asd

EDIT After testing two different codes (this one and the andswered by @Ed Morton), i´ve observed quite significant differences in performance between them:

编辑在测试了两个不同的代码(这个和@Ed Morton的答案)之后,我发现它们之间的性能差异很大:

$ cat lanza.sh 
date
awk -v x=3 '{print (NR%x?c+1:++c), $0}' file.dat > file.dat1
date
awk -v a=3 -v b=1 'c<a{print b"  "$0; c+=1} c==a{c=0;b+=1}' file.dat > file.dat2
date
./lanza.sh
EXEC1 
jueves,  7 de mayo de 2015, 22:01:17 CEST
jueves,  7 de mayo de 2015, 22:02:41 CEST
jueves,  7 de mayo de 2015, 22:04:09 CEST
EXEC2 (REVERSE ORDER FOR AWKS IN lanza.sh) 
jueves,  7 de mayo de 2015, 22:07:56 CEST
jueves,  7 de mayo de 2015, 22:09:24 CEST
jueves,  7 de mayo de 2015, 22:11:01 CEST
EXEC3 (REVERSE ORDER FOR AWKS IN lanza.sh) 
jueves,  7 de mayo de 2015, 22:12:14 CEST
jueves,  7 de mayo de 2015, 22:13:57 CEST
jueves,  7 de mayo de 2015, 22:15:20 CEST

$ wc -l file.dat
 30522352 file.dat
$ wc -l file.dat1
 30522352 file.dat1
$ wc -l file.dat2
 30522352 file.dat2

As can be seen , the different performance of two codes is about 5%-10% better with module (%) operator (with @Ed Morton´s code). Maybe more checking is needed, but the difference is quite significant at the first try!

可以看出,使用模块(%)运算符(使用@Ed Morton代码),两个代码的不同性能约为5%-10%。也许需要更多的检查,但第一次尝试时差异非常大!

equal or minus comparator times -> 1m28s, 1m37s, 1m43s

module (%) comparator times -> 1m24s, 1m28s, 1m33s

#3


perl -ne 'print 1+int(($.-1)/4) ," $_"'

or

perl -ne 'printf "%d %s", (3+$.)/4 ,$_'

#4


awk -v Cycle=3 '{print int((NR+Cycle-1)/Cycle), $0}' YourFile
# or
awk -v Cycle=3 '{printf '%d %s\n' (NR+Cycle-1)/Cycle, $0}' YourFile

#simplified for 3
awk '{print int((NR+2)/3), $0}' YourFile

#1


$ awk -v x=3 '{print (NR%x?c+1:++c), $0}' file
1 jeriro ieieie ieiue
1 ieirirp wzwezeg
1 ieueujueu ueuuwiuyh
2 iejejrökx lek
2 kejejhejhe pmys
2 krejrjhrjh hegehe
3 ririrjfjf
3 iririr iezete
3 pgogto

$ awk -v x=4 '{print (NR%x?c+1:++c), $0}' file
1 jeriro ieieie ieiue
1 ieirirp wzwezeg
1 ieueujueu ueuuwiuyh
1 iejejrökx lek
2 kejejhejhe pmys
2 krejrjhrjh hegehe
2 ririrjfjf
2 iririr iezete
3 pgogto

#2


Hope it helps !!!

希望能帮助到你 !!!

-bash-4.1$ awk -v a=3 -v b=1 'c<a{print b $0; c+=1} c==a{c=0;b+=1}' file
1asdasdasd
1asdas
1asd
2asd
2asd
2asd
3as
3asd
-bash-4.1$ awk -v a=4 -v b=1 'c<a{print b $0; c+=1} c==a{c=0;b+=1}' file
1asdasdasd
1asdas
1asd
1asd
2asd
2asd
2as
2asd

EDIT After testing two different codes (this one and the andswered by @Ed Morton), i´ve observed quite significant differences in performance between them:

编辑在测试了两个不同的代码(这个和@Ed Morton的答案)之后,我发现它们之间的性能差异很大:

$ cat lanza.sh 
date
awk -v x=3 '{print (NR%x?c+1:++c), $0}' file.dat > file.dat1
date
awk -v a=3 -v b=1 'c<a{print b"  "$0; c+=1} c==a{c=0;b+=1}' file.dat > file.dat2
date
./lanza.sh
EXEC1 
jueves,  7 de mayo de 2015, 22:01:17 CEST
jueves,  7 de mayo de 2015, 22:02:41 CEST
jueves,  7 de mayo de 2015, 22:04:09 CEST
EXEC2 (REVERSE ORDER FOR AWKS IN lanza.sh) 
jueves,  7 de mayo de 2015, 22:07:56 CEST
jueves,  7 de mayo de 2015, 22:09:24 CEST
jueves,  7 de mayo de 2015, 22:11:01 CEST
EXEC3 (REVERSE ORDER FOR AWKS IN lanza.sh) 
jueves,  7 de mayo de 2015, 22:12:14 CEST
jueves,  7 de mayo de 2015, 22:13:57 CEST
jueves,  7 de mayo de 2015, 22:15:20 CEST

$ wc -l file.dat
 30522352 file.dat
$ wc -l file.dat1
 30522352 file.dat1
$ wc -l file.dat2
 30522352 file.dat2

As can be seen , the different performance of two codes is about 5%-10% better with module (%) operator (with @Ed Morton´s code). Maybe more checking is needed, but the difference is quite significant at the first try!

可以看出,使用模块(%)运算符(使用@Ed Morton代码),两个代码的不同性能约为5%-10%。也许需要更多的检查,但第一次尝试时差异非常大!

equal or minus comparator times -> 1m28s, 1m37s, 1m43s

module (%) comparator times -> 1m24s, 1m28s, 1m33s

#3


perl -ne 'print 1+int(($.-1)/4) ," $_"'

or

perl -ne 'printf "%d %s", (3+$.)/4 ,$_'

#4


awk -v Cycle=3 '{print int((NR+Cycle-1)/Cycle), $0}' YourFile
# or
awk -v Cycle=3 '{printf '%d %s\n' (NR+Cycle-1)/Cycle, $0}' YourFile

#simplified for 3
awk '{print int((NR+2)/3), $0}' YourFile