按值长度对属性文件进行排序

时间:2016-12-23 16:00:51

标签: bash shell properties

查看是否有人有一些shell或bash魔法允许按属性值的字符长度对属性文件进行排序。

例如:

输入{% extends 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig' %} {% block blast_nested_treeable_widget %} {{ block('sonata_type_model_widget') }} {% endblock %} 文件:

input.properties

将输出排序为short-but-long-value=12345 this-is-a-really-long-property-name-with-short-value-length=12 foo=1234 bar2=123 var1=AUTHENTICATE, DISCOVER_SCHEMA, ENABLE, PROVISIONING var2=OU=OU_SOMETHING,OU=OU_SOMTHING_ELSE,OU=OU_MORE_SOMETHING,OU=LAST_SOMETHING 文件:

output.properties

1 个答案:

答案 0 :(得分:2)

使用awk / sortcut逻辑: -

awk -F"=" '{ st = index($0,"="); print length(substr($0,st+1)),$0 }' input.properties | sort -n | cut -d" " -f2-
this-is-a-really-long-property-name-with-short-value-length=12
bar2=123
foo=1234
short-but-long-value=12345
var1=AUTHENTICATE, DISCOVER_SCHEMA, ENABLE, PROVISIONING
var2=OU=OU_SOMETHING,OU=OU_SOMTHING_ELSE,OU=OU_MORE_SOMETHING,OU=LAST_SOMETHING

将文件写为

awk -F"=" '{ st = index($0,"="); print length(substr($0,st+1)),$0 }' input.properties | sort -n | cut -d" " -f2- > output.properties