我有一个包含以下条目的txt文件
1
2
3
4
7
8
9
12
14
15
我需要生成以下范围
1-4
7-9
12-12
14-15
如何实现上述输出?
这就是我的尝试:
awk '{q=$1}{f=$1}{print $q} $1!=p+1{print l"-"f}{l=p+1}{p=$1} END{print}' filename
答案 0 :(得分:2)
我会说...
cscript 37673790.vbs
0 0 val equal
0 1 type diff (Empty<->Integer)
0 2 type diff (Empty<->String)
0 3 type diff (Empty<->String)
1 0 type diff (Integer<->Empty)
1 1 val equal
1 2 type diff (Integer<->String)
1 3 type diff (Integer<->String)
2 0 type diff (String<->Empty)
2 1 type diff (String<->Integer)
2 2 val equal
2 3 val diff (| )
3 0 type diff (String<->Empty)
3 1 type diff (String<->Integer)
3 2 val diff ( |)
3 3 val equal
对于您的给定文件,它返回:
awk -v OFS=- 'prev+1<$0 {print first ? first : 1,prev; first=$0}
{prev=$0}
END {print first, prev}' file
我不会完成您的尝试$ awk -v OFS=- 'prev+1<$0 {print first ? first : 1,prev; first=$0} {prev=$0} END {print first, prev}' file
1-4
7-9
12-12
14-15
但我建议您使用更具代表性的变量名称,以及从一小段开始,然后让您的脚本增长。否则,一旦它不起作用,它就会成为你想扔掉的丛林。