Board logo

标题: [已解决]如何将上下行互换 [打印本页]

作者: shw1395     时间: 2008-3-28 01:35    标题: [已解决]如何将上下行互换

[img]UploadFile/2008-1/400camelandoasis.jpg[/img]
Camel and Oasis

[img]UploadFile/2008-1/400-2-5.jpg[/img]
Lion Hunter

改成

Camel and Oasis
[img]UploadFile/2008-1/400camelandoasis.jpg[/img]

Lion Hunter
[img]UploadFile/2008-1/400-2-5.jpg[/img]

[[i] Last edited by shw1395 on 2008-6-1 at 03:07 PM [/i]]
作者: abcd     时间: 2008-3-28 01:54

sed "/^$\|^\[b\]/d" test.txt|sed "N;s/\(.*\)\n\(.*\)/\2\n\1/;G"
[ Last edited by abcd on 2008-3-28 at 02:37 AM ]
作者: shw1395     时间: 2008-3-28 02:09
可以用cmd的for吗?另外要忽略[b]开头的行。sed我从没用过,看了你的回帖,我正在看http://sed.sourceforge.net
作者: abcd     时间: 2008-3-28 02:44

@echo off&setlocal enabledelayedexpansion
set num=0
set str1=
set str2=
for /f "tokens=*" %%i in ('findstr /v "^\[b\]" test.txt') do (
   set /a num+=1,t=num%%2
   if !t!==0 (
    set str2=%%i
    call,echo %%str2%%
    call,echo %%str1%%
    echo.
   ) else (
    set str1=%%i
   )
)
pause>nul

作者: shw1395     时间: 2008-3-28 16:36
确实有效,在这里十分感谢abcd的答复。
其实我完全的效果是这样:
[b]Animals and Birds Gallery[/b]

[img]UploadFile/2008-1/nest.jpg[/img]
Nest

[img]UploadFile/2004-4/2003762261664813.jpg[/img]

[img]UploadFile/2004-4/20025221141571.jpg[/img]

只需处理这一部分的格式:
[img]UploadFile/2008-1/nest.jpg[/img]
Nest
[code]@echo off&setlocal enabledelayedexpansion
set num=0
set str1=
set str2=
for /f "tokens=*" %%i in (test.txt) do (
   set /a num+=1,t=num%%2
   if !t!==0 (
    set str2=%%i
    call,echo %%str2%%>>testend.txt
    call,echo %%str1%%>>testend.txt
    echo.>>testend.txt
   ) else (
    set str1=%%i
   )
)
pause>nul[/code]
好像还差一点点:(,看了for的帮助,原来FOR /F "eol=后只能有一个字符,无法用FOR /F "eol=[b]。也许用EmEditor的正则也是可以替换的,可惜我不会。
再次感谢abcd,他的回复我已经十分满意了。谢谢!
作者: abcd     时间: 2008-3-28 16:50
以后提问题就完整的提出来

要不然,别人回答累,自己也累。