如何根据路径/级别对URL进行排序?

时间:2019-02-20 10:51:50

标签: sorting url

我想根据路径/级别对URL进行排序。我该怎么做 ? (它可以是一个在线工具/ PHP / bash / linux脚本),什么都可以。

example.com/a
example.com/b
example.com/c
example.com/aaa/1
example.com/aaa/2
example.com/aaa/3
example.com/aaa/bbb/apath
example.com/aaa/bbb/bpath
example.com/aaa/bbb/cpath
example.com/aaa/bbb/path/1
example.com/aaa/bbb/path/2

1 个答案:

答案 0 :(得分:1)

echo将您的输入重新排序为一个循环。 我将输入行分配给第二个键,对非定界符进行条纹处理,并在键之前加上前缀,然后打印新行。

我将循环通过管道传输到sort,然后将排序后的输出通过管道传输到sed,以剥离前置键。

我没有太多使用$ {x ///}构造,所以可能有一种更清洁的方法,而不是这种转换为下划线的方法,我很期待看到它,大声笑

$: echo "example.com/a
example.com/c
example.com/aaa/bbb/path/1
example.com/aaa/3
example.com/aaa/bbb/apath
example.com/aaa/2
example.com/aaa/bbb/bpath
example.com/aaa/1
example.com/aaa/bbb/cpath
example.com/b
example.com/aaa/bbb/path/2" | 
  while read line
  do key=${line//\//_}
     key=${key//[^_]/}
     printf "$key\t$line\n"
 done | sort |sed 's/^_*          //'
example.com/a
example.com/b
example.com/c
example.com/aaa/1
example.com/aaa/2
example.com/aaa/3
example.com/aaa/bbb/apath
example.com/aaa/bbb/bpath
example.com/aaa/bbb/cpath
example.com/aaa/bbb/path/1
example.com/aaa/bbb/path/2