|
krell
初级用户
 
积分 116
发帖 6
注册 2005-4-1
状态 离线
|
『楼 主』:
[求助] 如何取得一个文件的指定行数的指定位置内容
使用 LLM 解释/回答一下
批处理如何取得一个文件的指定行数的指定位置内容,并写到另一个文件中。
比如一个文件的第5排的倒数12位字符,和第6排的倒数1-3位字符(该行内容中有一个=符号后可能是1-3位字符)。
得到这两个数值后,写入到另一个文件的指定行数中。(简单点输出到文件也可以,行数不多)
实在是不会任何编程,想想批处理应该最简单吧。可是,看了不少教程,没发现讲解如何得到一个文件中的指定内容的实现方法,请各位大大指点一下,谢谢!
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!
|
|
2006-10-3 13:58 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
初步写出了如下代码,楼主试一下看是否可行。另外,如果能把文件的内容贴一部分出来的话,就更好找一下规律,因为我不知道你所说的 “和第6排的倒数1-3位字符(该行内容中有一个=符号后可能是1-3位字符)。” ,要提取的内容究竟是倒数1~3的字符,还是倒数的1或2或3。
@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 ]
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-3 21:24 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
namejm兄的代码简洁, 如果把文本行中存在空格就无法整行提取的情况考虑进去就更好了.
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.
|
|
2006-10-3 21:28 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by pengfei at 2006-10-3 21:28:
namejm兄的代码简洁, 如果把文本行中存在空格就无法整行提取的情况考虑进去就更好了.
呵呵,其实已经能够提取行中可能存在的空格的。
for语句中,提取存在空格的行,除了用 "tokens=*" 之外,"delims=" 也是一个不错的选择,头两天在群里讨论过的,pengfei 可能没注意到。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-3 21:33 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
呵呵~ 的确如此, 看来整行提取有三种方法了:
tokens=* delims=* delims=
不过我还是发现兄弟的代码for中少了一个/F参数. ^_^
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. ^_^
|
|
2006-10-3 21:42 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
谢谢你的提醒,确实是少了一个 /F 参数,以前写代码的时候都测试过了才发出来的,这次这个因为觉得简单就没测试了,想不到竟然出现了这个低级错误,已经改正^_^
到目前为止,提取整行的方法我只发现了两种,而你所说的 delims=* 是难以提取的,因为它的含义只是提取以 * 分隔的内容而已。当内容中不含 * 的时候,它也会提取含有空格的行,但是,如果内容中有 * 的时候,情况就大不一样了,你测试一下就可以发现了。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-3 21:50 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by namejm at 2006-10-3 21:50:
到目前为止,提取整行的方法我只发现了两种,而你所说的 delims=* 是难以提取的,因为它的含义只是提取以 * 分隔的内容而已。当内容中不含 * 的时候,它也会提取含有空格的行,但是,如果内容中有 * 的时候,情况就大不一样了,你测试一下就可以发现了。
看来这是我的一个习惯性错误了.
"tokens=*" 将文本行内容都赋给第一个可替换变量
"delims=" 不使用任何分隔符, 作用是取消了默认空格作为分隔符. 使文本行中的内容都赋给一个可替换变量.
"delims=*" 而这个是使用特殊分隔符, 来替代空格. 显然不行的, 像兄说的这样, 呵呵~ 一旦出现该字符将不能正常提取.
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.
|
|
2006-10-4 05:06 |
|
|
krell
初级用户
 
积分 116
发帖 6
注册 2005-4-1
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
谢谢两位热心的大侠。不过试了一下,好像不行。
需要得到数值的文件内容如下:
[Program ]
Name = NEW ERY
Source=FAC
[FAC]
ImageFileName=90B5A901.ACR
ImageFileNumber=34
[Burn]
ImageFileNumber=0
Description=
[Action]
DoRecovery=No
需要的数值是 90B5A901.ACR 和 34 (这个34有可能是1位的数据,也有可能是两位的数据,也有可能是3位的数据)。
得到这两个数据后写到一个新文件中。
Image_Name=这里写90B5A901.ACR,Image_Count=这里写34。
新文件内容如下:
[Program ]
Name = NEW ERY
Source=FD_INFO
[FD_INFO]
Image_Name=
Image_Count=
Image_Size=51200
Image_PerDisc=13
Image_Language=1033
想象中这个操作应该很简单的吧,不是想不劳而获,而是实在太菜,希望大侠再给点意见,谢谢!
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:
Name = NEW ERY
Source=FAC
ImageFileName=90B5A901.ACR
ImageFileNumber=34
ImageFileNumber=0
Description=
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:
Name = NEW ERY
Source=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!
|
|
2006-10-4 12:59 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
请楼主测试效果:
@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
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
|
|
2006-10-5 00:23 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
set /a num+=1 一句可以省略,在for中用 skip=4 直接跳过头4行的内容就可以从第五行内容开始读取了。或者用 more +4 1.ini 也可以达到同样的效果。
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没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-10-5 00:36 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
我使用计数的方式提取数据是因为ImageFileNumber这一句出现了两次.
I use the counting method to extract data because the sentence "ImageFileNumber" appears twice.
|
|
2006-10-5 01:45 |
|
|
krell
初级用户
 
积分 116
发帖 6
注册 2005-4-1
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
呵呵,还是不行呀。
生成的新文件并未得到取值,还是这样的啦!大侠可以试试。
Image_Name=
Image_Count=
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=
|
|
2006-10-5 04:12 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
需要得到数值的文件默认为当前目录下的1.ini
The file that needs to get the value is defaulted to 1.ini in the current directory
|
|
2006-10-5 05:02 |
|
|
krell
初级用户
 
积分 116
发帖 6
注册 2005-4-1
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
就算是再菜也知道文件名和路径的。
不过,测试一下真的没有得到想要的结果。
附件当中的文件,楼上的大侠自己试一下了!
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!
附件
1: 1.rar (2006-10-5 05:45, 598 bytes, 下载附件所需积分 1 点
,下载次数: 21)
|
|
2006-10-5 05:45 |
|
|
pengfei
银牌会员
    
积分 1218
发帖 485
注册 2006-7-21 来自 湖南.娄底
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
哈哈~ 看来又发现一个新大陆, 楼主的1.ini文件为unicode编码.
这种编码格式的文本行for是无法提取的, 我把1.ini改为ANSI码. 请测试...
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...
附件
1: test.rar (2006-10-5 08:08, 546 bytes, 下载附件所需积分 1 点
,下载次数: 24)
|
|
2006-10-5 08:08 |
|