Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unix 按范围排序的数字序列_Unix_Awk_Sed - Fatal编程技术网

Unix 按范围排序的数字序列

Unix 按范围排序的数字序列,unix,awk,sed,Unix,Awk,Sed,我有一个巨大的文本文件,每行都有排序的数字。我想使用sed或awk将其转换为范围块。 例如: 1 2 3 5 6 9 11 12 应转化为: 1-3 5-6 9 11-12 使用sed进行这种转换合理吗?Perl解决方案: perl -lne 'sub out { print $first == $prev ? $first : "$first-$prev" } # Produce the output. if ($_ != $prev+1 and defined $f

我有一个巨大的文本文件,每行都有排序的数字。我想使用sed或awk将其转换为范围块。 例如:

1
2
3
5
6
9
11
12
应转化为:

1-3
5-6
9
11-12
使用sed进行这种转换合理吗?

Perl解决方案:

perl -lne 'sub out { print $first == $prev ? $first : "$first-$prev" } # Produce the output.
           if ($_ != $prev+1 and defined $first) {                     # Current number not in seq.
               out();                                                  # Output the previous seq.
               $first = $_;                                            # Start a new one.
           }
           $prev  = $_;                                                # Remember the current number.
           $first = $_ if !defined $first;                             # Initialize for the first line.
           }{ out()                                                    # Output the last sequence.
          ' input.txt
awk 'NR==1 {printf $0} p+1!=$0 {printf "-"p"\n"$0} {p=$0} END {print "-"$0}' file
1-3
5-6
9-9
11-12

以下是一个
awk
解决方案:

perl -lne 'sub out { print $first == $prev ? $first : "$first-$prev" } # Produce the output.
           if ($_ != $prev+1 and defined $first) {                     # Current number not in seq.
               out();                                                  # Output the previous seq.
               $first = $_;                                            # Start a new one.
           }
           $prev  = $_;                                                # Remember the current number.
           $first = $_ if !defined $first;                             # Initialize for the first line.
           }{ out()                                                    # Output the last sequence.
          ' input.txt
awk 'NR==1 {printf $0} p+1!=$0 {printf "-"p"\n"$0} {p=$0} END {print "-"$0}' file
1-3
5-6
9-9
11-12
这假设所有行都有数字,并且每行的数字都会增加


使用
printf

awk 'NR==1 {printf "%s",$0} p+1!=$0 {printf "-%s\n%s",p,$0} {p=$0} END {print "-"$0}'

好了,伙计们,bash解决方案:

perl -lne 'sub out { print $first == $prev ? $first : "$first-$prev" } # Produce the output.
           if ($_ != $prev+1 and defined $first) {                     # Current number not in seq.
               out();                                                  # Output the previous seq.
               $first = $_;                                            # Start a new one.
           }
           $prev  = $_;                                                # Remember the current number.
           $first = $_ if !defined $first;                             # Initialize for the first line.
           }{ out()                                                    # Output the last sequence.
          ' input.txt
awk 'NR==1 {printf $0} p+1!=$0 {printf "-"p"\n"$0} {p=$0} END {print "-"$0}' file
1-3
5-6
9-9
11-12
A=`head-n1 data.txt`
B=一美元
读行时
做
如果[[`expr$LINE-$B`!=1]]
然后
如果[[“$A”!=“$B”]]
然后
回音“-$B”
其他的
回声
fi
A=美元行
回声-n“$A”
fi
B=美元行
done
sed-to-doculd会让人很痛苦,但因为sed无法计数,所以很难用awk来完成这项工作,而且CPU非常庞大。谢谢你们,awk也很好。我为awk添加了标签。谢谢@Jotne,太完美了!只是想分享一些细节。输入文件有22亿行,输出只有57k…并且在38分钟内终止。真棒:)