在Unix中对find命令进行sorting以遵守自定义顺序

在Unix中对find命令进行sorting以遵守自定义顺序,第1张

概述在Unix中对find命令进行sorting以遵守自定义顺序 @H_403_0@我有一个脚本,输出文件path(通过find ),我想基于非常具体的自定义逻辑进行sorting :

@H_403_0@第一个sorting键:我希望第二个和第三个分隔的字段(如果存在的话)使用基于我提供的键列表的 自定义sorting进行sorting – 但不包括数字后缀。

@H_403_0@

@H_403_0@通过下面的示例input,密钥列表是:

@H_403_0@

@H_403_0@rp,Alpha,beta-ri,beta-rs,RC

@H_403_0@第二sorting键:按每行的结尾数字进行数字sorting。

@H_403_0@给出以下示例input(注意每行的/foo/bar/test/example/8.2.4.0前缀是附带的):

@H_403_0@我怎样才能得到python logging.deBUG()消息输出到标准输出?

@H_403_0@虚拟机apt-get grub问题

@H_403_0@我如何使shell脚本在linux和OS X上都可以工作?

@H_403_0@让RabbitMQ自动运行?

@H_403_0@为了在linux上创build一个新的用户,我应该给crypt提供什么样的价值?

@H_403_0@/foo/bar/test/example/8.2.4.0-RC10 /foo/bar/test/example/8.2.4.0-RC2 /foo/bar/test/example/8.2.4.0-RC1 /foo/bar/test/example/8.2.4.0-Alpha10 /foo/bar/test/example/8.2.4.0-beta-ri10 /foo/bar/test/example/8.2.4.0-beta-ri2 /foo/bar/test/example/8.2.4.0-beta-rs10 /foo/bar/test/example/8.2.4.0-beta-rs2 /foo/bar/test/example/8.2.4.0-Alpha2 /foo/bar/test/example/8.2.4.0-rp10 /foo/bar/test/example/8.2.4.0-rp2

@H_403_0@我预计:

@H_403_0@/foo/bar/test/example/8.2.4.0-rp2 /foo/bar/test/example/8.2.4.0-rp10 /foo/bar/test/example/8.2.4.0-Alpha2 /foo/bar/test/example/8.2.4.0-Alpha10 /foo/bar/test/example/8.2.4.0-beta-ri2 /foo/bar/test/example/8.2.4.0-beta-ri10 /foo/bar/test/example/8.2.4.0-beta-rs2 /foo/bar/test/example/8.2.4.0-beta-rs10 /foo/bar/test/example/8.2.4.0-RC1 /foo/bar/test/example/8.2.4.0-RC2 /foo/bar/test/example/8.2.4.0-RC10

@H_403_0@GoogleTest CMake不承认TEST_F:就像它不承认GTest的东西

@H_403_0@在屏幕上打印消息,并同时发送到系统日志

@H_403_0@从windows可执行文件复制机器代码,并在linux上运行

@H_403_0@C linux的错误 – conio.h:没有这样的文件或目录

@H_403_0@在C中closuresTCP侦听套接字

@H_403_0@使用我的答案的变体您的原始问题 :

@H_403_0@./your-script | awk -v keysInorder='rp,RC' ' BEGIN { FS=OFS="-" keyCount = split(keysInorder,a,",") for (i = 1; i <= keyCount; ++i) keysToOrdinal[a[i]] = i } { sortKey = $2 if (NF == 3) sortKey = sortKey FS $3 sub(/[0-9]+$/,"",sortKey) auxFIEldPrefix = "|" FS if (NF == 2) auxFIEldPrefix = auxFIEldPrefix FS sub(/[0-9]/,auxFIEldPrefix "&",$NF) sortOrdinal = sortKey in keysToOrdinal ? keysToOrdinal[sortKey] : keyCount + 1 print sortOrdinal,$0 } ' | sort -t- -k1,1n -k3,3 -k5,5n | sed 's/^[^-]*-//; s/|-{1,2}//'

@H_403_0@./your-script表示任何命令产生你想要排序的输出。

@H_403_0@请注意,一个辅助。 字符, | ,用来方便排序,假定这个字符不会出现在输入中 – 这应该是合理的安全的,因为文件系统路径通常不包含管道字符。

@H_403_0@任何不在排序键列表中的字段2值(无数字后缀),排序在字段2/3值之后 ,使用字母排序。

@H_403_0@虽然这与OP所寻找的不匹配,但指出sort命令具有用于版本排序的选项-V将是有用的。 它遵循ASCII表格中正确的字符顺序来完成这个工作(即大写字母在前,小写字母在下)

@H_403_0@例如:

@H_403_0@cat test.sort.txt /foo/bar/test/example/8.2.4.0-RC10 /foo/bar/test/example/8.2.4.0-RC2 /foo/bar/test/example/8.2.4.0-RC1 /foo/bar/test/example/8.2.4.0-Alpha10 /foo/bar/test/example/8.2.4.0-beta-ri10 /foo/bar/test/example/8.2.4.0-beta-ri2 /foo/bar/test/example/8.2.4.0-beta-rs10 /foo/bar/test/example/8.2.4.0-beta-rs2 /foo/bar/test/example/8.2.4.0-Alpha2 /foo/bar/test/example/8.2.4.0-rp10 /foo/bar/test/example/8.2.4.0-rp2

@H_403_0@和排序:

@H_403_0@% sort -V test.sort.txt /foo/bar/test/example/8.2.4.0-RC1 /foo/bar/test/example/8.2.4.0-RC2 /foo/bar/test/example/8.2.4.0-RC10 /foo/bar/test/example/8.2.4.0-Alpha2 /foo/bar/test/example/8.2.4.0-Alpha10 /foo/bar/test/example/8.2.4.0-beta-ri2 /foo/bar/test/example/8.2.4.0-beta-ri10 /foo/bar/test/example/8.2.4.0-beta-rs2 /foo/bar/test/example/8.2.4.0-beta-rs10 /foo/bar/test/example/8.2.4.0-rp2 /foo/bar/test/example/8.2.4.0-rp10

@H_403_0@因此,在给出版本名称时注意这一点很有用。

@H_403_0@这就是说,如果你坚持,这是一个使用sed强制排序的班轮:

@H_403_0@cat test.sort.txt|sed -e 's/-rp/-x1xrp/;s/-Alpha/-x2xAlpha/;s/-beta-ri/-x3xbeta-ri/;s/-beta-rs/-x4xbeta-rs/;s/-RC/-x5xRC/'|sort -V|sed -e 's/xx//' /foo/bar/test/example/8.2.4.0-rp2 /foo/bar/test/example/8.2.4.0-rp10 /foo/bar/test/example/8.2.4.0-Alpha2 /foo/bar/test/example/8.2.4.0-Alpha10 /foo/bar/test/example/8.2.4.0-beta-ri2 /foo/bar/test/example/8.2.4.0-beta-ri10 /foo/bar/test/example/8.2.4.0-beta-rs2 /foo/bar/test/example/8.2.4.0-beta-rs10 /foo/bar/test/example/8.2.4.0-RC1 /foo/bar/test/example/8.2.4.0-RC2 /foo/bar/test/example/8.2.4.0-RC10

@H_403_0@我发现了一个完全不同于@ mklement0建议我的解决方案。

@H_403_0@#!/bin/bash echo "Enter a version :" read VERSION while read line; do find $line -type d | grep $VERSION | sort -n >> outfile.txt grep '.*-Alpha[0-9]' outfile.txt | sort -n >> outfile2.txt grep '.*-beta-ri[0-9]' outfile.txt | sort -n >> outfile2.txt grep '.*-beta-rs[0-9]' outfile.txt | sort -n >> outfile2.txt grep '.*-RC[0-9]' outfile.txt | sort -n >> outfile2.txt rm outfile.txt done <whatever.txt

@[email protected]的内容:

@H_403_0@/foo/bar/test/example/8.2.4.0-Alpha10 /foo/bar/test/example/8.2.4.0-Alpha8 /foo/bar/test/example/8.2.4.0-Alpha9 /foo/bar/test/example/8.2.4.0-beta-ri1 /foo/bar/test/example/8.2.4.0-beta-ri2 /foo/bar/test/example/8.2.4.0-beta-rs1 /foo/bar/test/example/8.2.4.0-beta-rs2 /foo/bar/test/example/8.2.4.0-beta-rs3 /foo/bar/test/example/8.2.4.0-RC1

@H_403_0@唯一的错误是Alpha10在Alpha8之前

@H_403_0@任何线索?

总结

以上是内存溢出为你收集整理的在Unix中对find命令进行sorting以遵守自定义顺序全部内容,希望文章能够帮你解决在Unix中对find命令进行sorting以遵守自定义顺序所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1267624.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存