使用外壳线条的排序项 [英] Sort entries of lines using shell

查看:113
本文介绍了使用外壳线条的排序项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到以下输入和输出:

Considering the following input and output:

  infile   |   outfile
1 3 5 2 4  |  1 2 3 4 5
2 4 5      |  2 4 5
4 6 2 1    |  1 2 4 6

是否有任何的 的UNIX程序的组合,的不涉及编程语言的 - ,在每行排序项 - 并不比shell脚本本身的任何其他文件比下面的方法更快:

Is there any combination of UNIX programs, not involving programming languages -- not any other than the shell scripting itself --, that sorts the entries in each line of a file faster than the following approach:

while read line; do
    tr ' ' '\n' <<< ${line} | sort | tr '\n' ' '
    echo ""
done < infile > outfile

我的意思是,我能够创建一个小的 CPP /蟒蛇/ AWK /...计划这样做,但它仅仅是不一样的使用通常的单行的神奇地解决问题。

I mean, I'm able to create a small cpp/python/awk/... program to do so, but it is just not the same as using the usual one-liners to magically solve problems.

编辑:

我必须加入过多的文字,而不是简单地问我想要的东西;直截了当,我想确认是否有任何UNIX程序/的的节目组合(使用管道,维权,田地,...)的能够在线路排序项,但没有尽可能多的开销为上面的一个解决方案。

I must have added too much text, instead of simply asking what I wanted; straightforwardly, I wanted to confirm whether there was any UNIX program/combination of programs (using pipes, fors, whiles, ...) capable of sorting entries in a line, but without as much overhead as the one solution above.

我知道我可以做的讨厌的工作的编程语言,如Perl,AWK,蟒蛇,但我实际上是在寻找UNIX程序的成分,不会涉及到这些语言间preters。从答案,我必须断定没有这样的在线排序工具(s)和我为我已经得到了解决方案,非常感谢 - 主要是很整齐Perl的单行。

I know I may do the nasty job in a programming language, like perl, awk, python, but I was actually looking for a composition of UNIX programs that wouldn't involve these language interpreters. From the answers, I must conclude there is no such inline sort tool(s), and I'm very thankful for the solutions I've got -- mainly the very neat Perl one-liner.

不过,我真的不明白就猛砸的方法,我贴了这么多开销的原因。难道真的是由于上下文切换众多,或者是它翻译回来来回输入和排序它的简单的开销?

Yet, I do not really understand the reason for so much overhead on the Bash approach I posted. Is it really due to a multitude of context switches, or is it simply the overhead of translating back and fro the input, and sorting it?

我似乎无法理解其中的这些步骤是这么多放缓的执行。这需要几分钟的时间在50万〜行的文件中的条目排序,以30〜值中的每一行。

I can't seem to understand which of these steps is slowing down the execution so much. It takes several minutes to sort the entries in a file with ~500k lines, with ~30 values in each line.

推荐答案

Perl可以做到这一点很好地作为一个单行的Unix / Linux命令:

Perl can do this nicely as a one-line Unix/Linux command:

perl -n -e "print join ' ', sort{a<=>b} split ' '" < input.txt > output.txt

这是古老的Perl没有美元在 A B ,允许命令运行精在Windows和Bash shell两者。如果使用美元,庆典,他们要么必须用反斜杠转义,或者必须反转单引号和双引号。

This is "archaic" Perl with no dollars before the a and b, which allows the command to run fine in both Windows and bash shells. If you use the dollars with bash, they must either be escaped with backslashes, or you must invert the single and double quotes.

请注意,您正在尝试的差别的命令,编程语言之间的画,程序pretty薄。击是一种编程语言。 Perl可以肯定作为外壳。两者都是命令。

Note that the distinctions you are trying to draw between commands, programming languages, and programs are pretty thin. Bash is a programming language. Perl can certainly be used as a shell. Both are commands.

你的脚本运行速度慢的原因是,它产生每循环迭代3的过程。工艺创作是pretty昂贵。

The reason your script runs slowly is that it spawns 3 processes per loop iteration. Process creation is pretty expensive.

这篇关于使用外壳线条的排序项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆