Windows批处理文件脚本来排序URL

时间:2011-04-02 01:02:19

标签: windows batch-file cmd

如何通过在文本文件中将具有唯一文件名的URL分组来编写Windows批处理脚本来对URL进行排序?我不知道如何进一步描述我想要实现的目标,但我希望下面的例子解释一切:

我想要这个文字

http://example.com/5235/Guava.jpg
http://example.com/2725/Guava.jpg
http://example.com/4627/Guava.jpg
http://example.com/8385/Guava.jpg
http://example.com/3886/Lemon.jpg
http://example.com/5896/Lemon.jpg
http://example.com/2788/Lemon.jpg
http://example.com/1758/Lemon.jpg
http://example.com/1788/Apple.jpg
http://example.com/1567/Apple.jpg
http://example.com/8065/Apple.jpg
http://example.com/6467/Apple.jpg
http://example.com/1464/Banana.jpg
http://example.com/6581/Banana.jpg
http://example.com/4642/Banana.jpg
http://example.com/8635/Banana.jpg
http://example.com/2578/Pineapple.jpg
http://example.com/1452/Pineapple.jpg
http://example.com/8652/Pineapple.jpg
http://example.com/9463/Pineapple.jpg
http://example.com/9765/Peach.jpg
http://example.com/3578/Peach.jpg
http://example.com/3583/Peach.jpg
http://example.com/9467/Peach.jpg
http://example.com/3683/Mango.jpg
http://example.com/3479/Mango.jpg
http://example.com/1795/Mango.jpg
http://example.com/7345/Mango.jpg

以这种方式排序

http://example.com/5235/Guava.jpg
http://example.com/3886/Lemon.jpg
http://example.com/1788/Apple.jpg
http://example.com/1464/Banana.jpg
http://example.com/2578/Pineapple.jpg
http://example.com/9765/Peach.jpg
http://example.com/3683/Mango.jpg
http://example.com/2725/Guava.jpg
http://example.com/5896/Lemon.jpg
http://example.com/1567/Apple.jpg
http://example.com/6581/Banana.jpg
http://example.com/1452/Pineapple.jpg
http://example.com/3578/Peach.jpg
http://example.com/3479/Mango.jpg
http://example.com/4627/Guava.jpg
http://example.com/2788/Lemon.jpg
http://example.com/8065/Apple.jpg
http://example.com/4642/Banana.jpg
http://example.com/8652/Pineapple.jpg
http://example.com/3583/Peach.jpg
http://example.com/1795/Mango.jpg
http://example.com/8385/Guava.jpg
http://example.com/1758/Lemon.jpg
http://example.com/6467/Apple.jpg
http://example.com/8635/Banana.jpg
http://example.com/9463/Pineapple.jpg
http://example.com/9467/Peach.jpg
http://example.com/7345/Mango.jpg

换句话说,对于这个特定的例子(每个水果jpeg中有四个),我想按照这种方式对行进行排序:1,5,9,13,17,21,25,2,6,10,14 ,18,22,26等。我希望你明白我的意思。

文本文件始终包含每个“水果”图片的编号相同的网址。不能有六个柠檬jpg文件和四个番石榴jpg文件。我希望你明白我的意思。

3 个答案:

答案 0 :(得分:2)

也许是这样的:

@ECHO OFF
SET origfile=urls.txt
SET c=1
SET skip=4
FOR /L %%c IN (1,1,%skip%) DO IF EXIST %origfile%.%%c DEL %origfile%.%%c
FOR /F "tokens=*" %%L IN (%origfile%) DO CALL :process "%%L"
DEL %origfile%
FOR /L %%c IN (1,1,%skip%) DO (
  TYPE %origfile%.%%c >> %origfile%
  DEL %origfile%.%%c
)
GOTO :EOF

:process
ECHO %~1>>%origfile%.%c%
SET /A c=c%%skip+1

这个想法是将后续行输出到不同的文件,每4行重复一次序列(实际上这里有4个参数化,所以你可以很容易地改变它),然后用原始名称连接这些文件。

答案 1 :(得分:1)

在您的文件上运行此操作。我在上面的评论中描述的算法。

#!/bin/bash

FILE=$1
FIRST=$(head -1 $FILE)
COUNT=$(grep $FIRST $FILE | wc -l)
LINES=$(uniq $FILE)
for i in $(seq 1 $COUNT); do
    echo $LINES | tr " " "\n"
done

答案 2 :(得分:1)

您可以告诉sort从哪里开始比较:

/+n                         Specifies the character number, n, to
                            begin each comparison.  /+3 indicates that
                            each comparison should begin at the 3rd
                            character in each line.  Lines with fewer
                            than n characters collate before other lines.
                            By default comparisons start at the first

因此,如果您的URI前缀始终相同(您的注释表明),您可以通过

运行该文件
sort /+25 list.txt /O:list_new.txt

应按文件名对其进行排序。