如何编写一个Windows批处理脚本,通过将具有唯一文件名的文件名分组到文本文件中,对URL进行sorting? 我不知道如何进一步描述我想达到什么,但是我希望下面的例子能够解释一切:
我想要这个文本
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
这样sorting
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中的四个),我想根据这种方式对行进行sorting:1,5,9,13,17,21,25,2,6,10,14,18, 22,26,等等。 我希望你明白我的意思。
该文本文件始终包含每个“水果”图片的相同数量的url。 不能有六个柠檬jpg文件和四个番石榴jpg文件。 我希望你明白我的意思。
也许这样的事情:
@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参数化,所以你可以很容易地改变它),然后连接在原来的名字下的文件。
在你的文件上运行这个。 算法在我上面的评论中描述。
#!/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
你可以告诉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
然后按文件名将其排序。