wzf1024 当前离线
一级士官
codegay 当前离线
少校
""" python移动文本第A行到第N行.py http://bbs.bathome.net/thread-39853-2-1.html 2016年4月4日 18:50:06 codegay 这个问题本质等于把列表元素第A个移动到第N.... """ with open("22.txt") as f: txt=f.readlines() txt.insert(4,txt[1])#第二行插入第五行的位置 del(txt[1])#删除原来的第二行 print(txt)复制代码
#方法2 a=list("1234567") a.insert(3,a.pop(1))#弹出第二行插入到第四行 print(a)复制代码
TOP
CrLf 当前离线
论坛巡查
@sed "2{h;d};4{G}" 旧.txt >新.txt复制代码
pcl_test 当前离线
荣誉版主
srcLine<dstLine?arr.splice(dstLine-1, 0, str):arr.splice(dstLine, 0, str); //插入复制代码
apython 当前离线
列兵
@echo off setlocal enabledelayedexpansion for /f "delims=" %%i in (r1.txt) do ( set/a n+=1 if !n! equ 1 set "a1=%%i" if !n! equ 2 set "a2=%%i" if !n! equ 3 set "a3=%%i" if !n! equ 4 set "a4=%%i" if !n! equ 5 set "a5=%%i" ) for /f "skip=5 delims=" %%i in (r1.txt) do ( set/a a+=1 if !a! equ 1 >temp.txt ( echo,!a1! echo,!a3! echo,!a4! echo,!a2! echo,!a5! ) echo,%%i >>temp.txt ) pause复制代码
WHY 当前离线
上校
@if (0)==(0) echo off for /f "delims=" %%i in ('dir /b /s *.txt') do ( type "%%i" | cscript //nologo //e:jscript "%~f0" > "%%i.Log" ) pause & exit @end var srcLine = 6, dstLine = 4; //第6行移动到第4行 var arr = WSH.StdIn.ReadAll().replace(/\r\n$/, '').split('\r\n'); var str = arr[srcLine-1]; arr.splice(srcLine-1, 1); //删除 arr.splice(dstLine-1, 0, str); //插入 WSH.Echo(arr.join('\r\n'));复制代码
评分人数
happy886rr 当前离线
等待验证会员
@echo off REM 支持倒着插,如把第6000行插入到第3行 REM 要提取的行 set k1=6000 REM 插入的位置 set k2=3 if %k1% equ %k2% (exit) if %k1% gtr %k2% (set gk=1) for /f "delims=" %%T in ('dir /a-d /b /s *.txt') do ( CD "%%~dpT" if defined gk ( sort /+1888 /rec 1888 "%%T" /o temp for /f %%a in ('find /c /v ""^<temp') do (set/a k1=%%a-k1+1,k2=%%a-k2+1) ) else ( ren "%%T" temp ) setlocal enabledelayedexpansion more +!k2! temp>tmp2 set/p =;<nul>>temp for /f %%a in ('find /c /v ""^<temp') do ( set/a L=%%a if !k1! leq 0 (echo 总共才%%a行,参数设置有误!&del temp&del tmp2&set/p=&exit) if !k2! leq 0 (echo 总共才%%a行,参数设置有误!&del temp&del tmp2&set/p=&exit) ) set/a K=k1-1,L1=L-K,L2=L-k2 sort /+1888 /rec 1888 temp /o tmp more +!L1! tmp|sort /+1888 /rec 1888 /o tmp1 more +!k1! temp|sort /+1888 /rec 1888 /o tmp more +!L2! tmp|sort /+1888 /rec 1888 >>tmp1 more +!K! temp>tmp &del temp (set/p a=&echo;!a!&endlocal)<tmp>>tmp1 &del tmp find /v ""<tmp2>>tmp1 &del tmp2 if defined gk ( sort /+1888 /rec 1888 "tmp1" /o "%%T" &del tmp1 ) else ( move tmp1 "%%T" ) )复制代码
gawk 当前离线
少将
@echo off set path=%~dp0;%path% for /f "delims=" %%i in ('dir /b /s /a-d *.txt') do ( pushd "%%~dpi" gawk "{if(NR==2)s=$0;else if(NR==4)print$0\"\n\"s;else print}" "%%i" > "%%~ni_new.txt" popd )复制代码