Rover12421's Blog

The End.

Bat删除指定行

文本t.txt,要求删除其中1,2,67行,内容如下

t.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name
-------
[Server Rate]
;;  
 
;
ItemRootRate = 2.0
 
;
MineSpeedRate = 1.0
 
;
ForceLiverAccumRate = 1.0
 
;
MasteryGetRate = 1.0
 
;
AnimusExpRate = 1.0
 
;
PlayerExpRate = 1.0
 
;
DarkHoleRewardRate = 1.0
 
[Premium PCRoom Rate]
;;  
 
;
ItemRootRate = 4.0
 
;
MineSpeedRate = 2.0
 
;
ForceLiverAccumRate = 2.0
 
;
MasteryGetRate = 2.0
 
;
AnimusExpRate = 2.0
 
;
PlayerExpRate = 2.0
 
[Infinite Guild Major War]
;       (0->;, 1->;)
 
;  HQ.
Major_Bind_HQ = 0
 
;,   .
Major_Sette_Mine_Map = 0
 
;    .
Major_Scroll_Item = 0
 
;(,)  .
Major_Cash_Item = 0
 
;  .
Major_Add_Character = 0
 
(受1)

因for不能统计空行,就在空行中加了些空格,出来的bat如下

cs.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
setlocal enabledelayedexpansion
for /f "delims=? eol=" %%a in (t.txt) do (
set str=;%%a
set /a NUM+=1
if !NUM!==1 (
 echo.
) else (
  if !NUM!==2 (
  echo.
 ) else (
  if !NUM!==68 (
   echo.
) else (
    echo !str!32.txt
   )
  )
)
)

要统计空格并也有办法,用findstr就行了,但是要加 /n 参数就多来行号出来,代码和效果如下

cs2.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
setlocal enabledelayedexpansion
for /f "eol=? delims=" %%a in ('findstr /n .* t.txt') do (
set str=;%%a
set /a NUM+=1
if !NUM!==1 (
 echo.
) else (
  if !NUM!==2 (
  echo.
 ) else (
  if !NUM!==68 (
   echo.
) else (
    echo !str! >> 32.txt
   )
  )
)
)

结果:
3.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
3:[Server Rate]
4:;;
5:
6:;
7:ItemRootRate = 2.0
8:
9:;
10:MineSpeedRate = 1.0
11:
12:;
13:ForceLiverAccumRate = 1.0
14:
15:;
16:MasteryGetRate = 1.0
17:
18:;
19:AnimusExpRate = 1.0
20:
21:;
22:PlayerExpRate = 1.0
23:
24:;
25:DarkHoleRewardRate = 1.0
26:
27:[Premium PCRoom Rate]
28:;;
29:
30:;
31:ItemRootRate = 4.0
32:
33:;
34:MineSpeedRate = 2.0
35:
36:;
37:ForceLiverAccumRate = 2.0
38:
39:;
40:MasteryGetRate = 2.0
41:
42:;
43:AnimusExpRate = 2.0
44:
45:;
46:PlayerExpRate = 2.0
47:
48:
49:
50:[Infinite Guild Major War]
51:;       (0->, 1->)
52:
53:;  HQ.
54:Major_Bind_HQ = 0
55:
56:;,   .
57:Major_Sette_Mine_Map = 0
58:
59:;    .
60:Major_Scroll_Item = 0
61:
62:;(,)  .
63:Major_Cash_Item = 0
64:
65:;  .
66:Major_Add_Character = 0
67:

如果不用 /n 参数的话效果和cs.bat是一样的,把代码也贴下吧。如果在空行里加上空格,就能真确处理。就看实际有没有必要了

cs3.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
setlocal enabledelayedexpansion
for /f "eol=? delims=" %%a in ('findstr  .* t.txt') do (set str=;%%a
set /a NUM+=1
if !NUM!==1 (
 echo.
) else (
  if !NUM!==2 (
  echo.
 ) else (
  if !NUM!==68 (
   echo.
) else (
    echo !str! >> 32.txt
   )
  )
)
)

Comments