China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-29 07:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to get the content at the specified position of the specified line of a file View 4,191 Replies 20
Original Poster Posted 2006-10-03 13:58 ·  中国 湖北 武汉 电信
初级用户
Credits 116
Posts 6
Joined 2005-04-01 00:00
21-year member
UID 37688
Gender Male
Status Offline
How to use batch processing to obtain the content at a specified position in a specified line of a file and write it to another file.

For example, the 12th character from the end in the 5th line of a file, and the 1st to 3rd characters from the end in the 6th line (there may be 1-3 characters after an = symbol in this line content).

After getting these two values, write them to the specified lines of another file. (It's okay to simply output to a file for simplicity, with not many lines.)

I really don't know any programming, and I think batch processing should be the simplest. However, after reading many tutorials, I didn't find the implementation method for getting the specified content in a file. Please help me, thank you!
Floor 2 Posted 2006-10-03 21:24 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The initial code is roughly written. The poster can try to see if it works. Also, if you can post part of the content of the file, it would be better to find the pattern, because I don't know whether the content you mentioned to extract is the characters from the 1st to the 3rd from the end, or 1 or 2 or 3 from the end.

@echo off
set num=0
setlocal enabledelayedexpansion
for /f "skip=4 delims=" %%i in (test.txt) do (
set /a num+=1
set var=%%i
if !num! equ 1 echo !var:~-12,1!
if !num! equ 2 echo !var:~-3,3! && goto end
)
:end
pause


[ Last edited by namejm on 2006-10-3 at 21:51 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 3 Posted 2006-10-03 21:28 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Brother namejm's code is concise. It would be even better if the situation where spaces exist in text lines and the entire line cannot be extracted is taken into account.
Floor 4 Posted 2006-10-03 21:33 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by pengfei at 2006-10-3 21:28:
The code of user namejm is concise. It would be better if it could consider the situation where the entire line cannot be extracted when there are spaces in the text line.

  Hehe, actually it can already extract lines that may have spaces.

  In the for statement, to extract lines with spaces, in addition to using "tokens=*", "delims=" is also a good choice. We discussed this in the group the other day, and pengfei might not have noticed.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-10-03 21:42 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Hehe~ Indeed, it seems there are three methods for extracting entire lines:

tokens=* delims=* delims=

But I still noticed that your code is missing a /F parameter in the for. ^_^
Floor 6 Posted 2006-10-03 21:50 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Thank you for your reminder. It's indeed missing a /F parameter. I used to test the code before sending it out, but this time I thought it was simple and didn't test it. Unexpectedly, such a basic mistake occurred, and it has been corrected ^_^

So far, I have only found two methods to extract the entire line, and the delims=* you mentioned is difficult to extract because its meaning is just to extract the content separated by *. When there is no * in the content, it will also extract lines with spaces. However, if there is a * in the content, the situation is very different. You can test it and find out.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-10-04 05:06 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Originally posted by namejm at 2006-10-3 21:50:
  Up to now, I have only found two methods to extract entire lines. The "delims=*" you mentioned is difficult to extract because its meaning is just to extract content separated by *. When there is no * in the content, it will also extract lines with spaces. However, if there are * in the content, the situation is quite different. You can test it and find out.


It seems this is a habitual mistake of mine.

"tokens=*" assigns all the content of the text line to the first replaceable variable.

"delims=" does not use any delimiters, which cancels the default space as a delimiter. It makes all the content of the text line assigned to a replaceable variable.

"delims=*" but this uses a special delimiter to replace spaces. Obviously, it doesn't work. As the brother said, once this character appears, it cannot be extracted normally.
Floor 8 Posted 2006-10-04 12:59 ·  中国 湖北 武汉 武昌区 电信
初级用户
Credits 116
Posts 6
Joined 2005-04-01 00:00
21-year member
UID 37688
Gender Male
Status Offline
Thanks to the two enthusiastic experts. However, I tried it and it seems not working.

The content of the file from which the value needs to be obtained is as follows:
[Program ]
Name = NEW ERY
Source=FAC
[FAC]
ImageFileName=90B5A901.ACR
ImageFileNumber=34
[Burn]
ImageFileNumber=0
Description=
[Action]
DoRecovery=No


The values needed are 90B5A901.ACR and 34 (this 34 may be a 1-digit data, may also be a 2-digit data, may also be a 3-digit data).

After getting these two data, write them into a new file.
Image_Name=Here write 90B5A901.ACR, Image_Count=Here write 34.

The content of the new file is as follows:
[Program ]
Name = NEW ERY
Source=FD_INFO
[FD_INFO]
Image_Name=
Image_Count=
Image_Size=51200
Image_PerDisc=13
Image_Language=1033

It is imagined that this operation should be very simple, not trying to get something for nothing, but really too inexperienced. Hope the expert can give more advice, thanks!
Floor 9 Posted 2006-10-05 00:23 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Please ask the owner to test the effect:


@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims==" %%i in (1.ini) do (
set /a num+=1
if !num! lss 8 (
if /i "%%i"=="ImageFileName" set one=%%j
if /i "%%i"=="ImageFileNumber" set two=%%j
)
)
(
echo
echo Name = NEW ERY
echo Source=FD_INFO
echo
echo Image_Name=%one%
echo Image_Count=%two%
echo Image_Size=51200
echo Image_PerDisc=13
echo Image_Language=1033
)>2.ini
pause

Floor 10 Posted 2006-10-05 00:36 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The line "set /a num+=1" can be omitted. In the for loop, using "skip=4" directly skips the first 4 lines of content, and then you can start reading from the fifth line. Alternatively, using "more +4 1.ini" can also achieve the same effect.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 11 Posted 2006-10-05 01:45 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
I use the counting method to extract data because the sentence "ImageFileNumber" appears twice.
Floor 12 Posted 2006-10-05 04:12 ·  中国 湖北 武汉 电信
初级用户
Credits 116
Posts 6
Joined 2005-04-01 00:00
21-year member
UID 37688
Gender Male
Status Offline
Hehe, still not working.

The generated new file did not get the value, it's still like this! The expert can give it a try.
Image_Name=
Image_Count=
Floor 13 Posted 2006-10-05 05:02 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
The file that needs to get the value is defaulted to 1.ini in the current directory
Floor 14 Posted 2006-10-05 05:45 ·  中国 湖北 武汉 电信
初级用户
Credits 116
Posts 6
Joined 2005-04-01 00:00
21-year member
UID 37688
Gender Male
Status Offline
Even the greenest hand knows the file name and path. But the test really didn't get the desired result. Try the files in the attachment by the expert upstairs!
Attachments
1.rar (598 bytes, Credits to download 1 pts, Downloads: 21)
Floor 15 Posted 2006-10-05 08:08 ·  中国 湖南 娄底 新化县 电信
银牌会员
★★★
Credits 1,218
Posts 485
Joined 2006-07-21 21:24
19-year member
UID 58987
From 湖南.娄底
Status Offline
Haha~ It seems another new territory has been discovered. The 1.ini file of the original poster is in Unicode encoding.

This kind of encoded text lines cannot be extracted with for. I changed 1.ini to ANSI code. Please test...
Attachments
test.rar (546 bytes, Credits to download 1 pts, Downloads: 24)
Forum Jump: