bash 脚本:如何以“number.text number.text etc”的形式对字符串进行排序按升序排列?
例如,如果我们在名为“var”的变量中有一个
字符串“2.test 1.test 9.test”,
我希望它是
1.test 2.test 9.test
我试图应用此命令
echo $var | sort -n
,但输出是不正确,因为如果我有
2.text 11.text
它将打印
11.text 2.text 这是错误的,因为 11>2
谢谢
for example if we have in a variable named "var"
a string "2.test 1.test 9.test"
i want it to be
1.test 2.test 9.test
I was trying to apply this command
echo $var | sort -n
but the output isn't correct because if for example I have
2.text
11.text
it will print
11.text 2.text which is wrong because 11>2
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sort
适用于行,而不是单词。对于您向我们展示的示例,您正在对单行文本进行排序。例如:
但这与您向我们展示的输出不一致,因此我无法确定您正在做什么,或者您正在尝试做什么。
您在寻找这样的东西吗?
您是否需要将线路重新组装成一条线路?通过
fmt -999
管道输出可以做到这一点,但这有点难看(GNU coreutilsfmt
将宽度限制为 2500)。sort
works on lines, not words.For the example you've shown us, you're sorting a single line of text. For example:
But that's inconsistent with the output you've shown us, so I can't be sure just what you're doing, or what you're trying to do.
Are you looking for something like this?
And do you need to re-assemble the lines into a single line? Piping the output through
fmt -999
will do that, but that's a bit ugly (GNU coreutilsfmt
limits the width to 2500).使用
tr
转换换行符中的空格;现在排序
;并将换行符转换回空格ps.在论坛的某个地方看到了这个
convert white spaces in linebreaks with
tr
; and nowsort
; and convert linebreaks back to white spacesps. saw this somewhere in a forum