![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-09-15 12:37 |
47,812 topics / 349,917 posts / today 0 new / 48,268 members |
| DOS批处理 & 脚本技术(批处理室) » Please describe the specific operation environment and relevant tools you are using, such as using which programming language, which text processing tools etc. Because the content of the problem is relatively general, without specific context, it is difficult to give a precise translation and corresponding solution. If you are using a programming language like Python, you can use file reading and writing operations to achieve. For example, in Python: ```python with open('file.txt', 'r') as f: lines = f.readlines() target_line_number = 2 # Suppose to modify the 3rd line (index starts from 0) new_content = 'The content you need' lines[target_line_number - 1] = new_content + '\n' with open('file.txt', 'w') as f: f.writelines(lines) ``` But this is just a general example. If you are using other environments, the specific implementation will be different. So please provide more specific information. 请描述您使用的具体操作环境和相关工具,比如使用哪种编程语言、哪种文本处理工具等,因为问题内容比较笼统,没有具体上下文,很难给出精准的翻译和对应解决方案。如果您使用Python这类编程语言,可通过文件读写操作实现,例如Python中: ```python with open('file.txt', 'r') as f: lines = f.readlines() target_line_number = 2 # 假设修改第3行(索引从0开始) new_content = '您需要的内容' lines[target_line_number - 1] = new_content + '\n' with open('file.txt', 'w') as f: f.writelines(lines) ``` 但这只是通用示例,若使用其他环境,具体实现会不同。所以请提供更具体信息。 Please describe the specific operation environment and relevant tools you are using, such as using which programming language, which text processing tools etc. Because the content of the problem is relatively general, without specific context, it is difficult to give a precise translation and corresponding solution. If you are using a programming language like Python, you can use file reading and writing operations to achieve. For example, in Python: ```python with open('file.txt', 'r') as f: lines = f.readlines() target_line_number = 2 # Suppose to modify the 3rd line (index starts from 0) new_content = 'The content you need' lines[target_line_number - 1] = new_content + '\n' with open('file.txt', 'w') as f: f.writelines(lines) ``` But this is just a general example. If you are using other environments, the specific implementation will be different. So please provide more specific information. Please describe the specific operation environment and relevant tools you are using, such as using which programming language, which text processing tools etc. Because the content of the problem is relatively general, without specific context, it is difficult to give a precise translation and corresponding solution. If you are using a programming language like Python, you can use file reading and writing operations to achieve. For example, in Python: ```python with open('file.txt', 'r') as f: lines = f.readlines() target_line_number = 2 # Suppose to modify the 3rd line (index starts from 0) new_content = 'The content you need' lines[target_line_number - 1] = new_content + '\n' with open('file.txt', 'w') as f: f.writelines(lines) ``` But this is just a general example. If you are using other environments, the specific implementation will be different. So please provide more specific information. To convert a specific line of text in a file to the text you need, the specific implementation depends on the programming language and tools you are using. For example, if using Python, you can use the following steps: ### Step 1: Read the file Open the file in read mode and read all lines into a list. ```python with open('your_file.txt', 'r') as file: lines = file.readlines() ``` ### Step 2: Locate the line to modify Identify the line number you want to modify (note that list indices start from 0). For example, if you want to modify the 3rd line, the index is 2. ```python target_line_index = 2 # Modify the 3rd line new_line_content = 'The content you want' ``` ### Step 3: Update the line Replace the content of the specified line in the list. ```python lines[target_line_index] = new_line_content + '\n' # Add a newline character ``` ### Step 4: Write the modified content back to the file Open the file in write mode and write the modified list of lines back to the file. ```python with open('your_file.txt', 'w') as file: file.writelines(lines) ``` If you are using other programming languages or tools, the basic idea is similar, but the specific syntax will be different. For example, in Java, you can read the file line by line, modify the specified line, and then write the modified content back to the file. |
| Printable Version 6,157 / 16 |
| Floor1 dosfroum | Posted 2005-12-07 13:04 |
| 初级用户 Posts 54 Credits 154 | |
|
If there is a file Try.txt with the following content
If you want to use a batch file to replace the line with How should this be written Thank you |
|
| Floor2 dosfroum | Posted 2005-12-11 10:57 |
| 初级用户 Posts 54 Credits 154 | |
|
This problem should be encountered by quite a few people
Or is it needed Is it too simple or unsolvable ah No news at all @@ |
|
| Floor3 Wengier | Posted 2005-12-11 11:29 |
| 系统支持 Posts 10,521 Credits 27,736 | |
|
Are third-party tools allowed? If allowed, you can try tools like XCHANGE, which can be solved in one line. For example, try this command (XCHANGE is in the attachment):
XCHANGE Try.txt "CCC = 13.4" "CCC = 55.33" Attachments XCHANGE.EXE (22.04 KiB) |
|
| Floor4 zreo | Posted 2005-12-11 14:30 |
| 初级用户 Posts 14 Credits 51 | |
|
Is there a detailed application explanation of this tool?
|
|
| Floor5 arding | Posted 2006-01-12 20:18 |
| 初级用户 Posts 38 Credits 90 | |
|
```@echo off & setlocal EnableDelayedExpansion
echo.>n_try.txt for /f "tokens=1,2,3*" %%i in (try.txt) do set var1=%%i && if "CCC " == "!var1!" (echo CCC = 55.33>>n_try.txt) else (echo %%i %%j %%k>>n_try.txt) pause |
|
| Floor6 gmy | Posted 2006-01-13 14:00 |
| 版主 Posts 392 Credits 1,113 | |
|
I use HEXC, which is only 5KB, smaller than XCHANGE, but the function is a bit simpler, it's enough.
hexc Try.txt Try.txt "CCC = 13.4" "CCC = 55.33" /u Attachments HEXC.rar (4.21 KiB) |
|
| Floor7 vptdosme | Posted 2006-01-14 22:17 |
| 初级用户 Posts 32 Credits 97 | |
|
It is best not to use third - party tools, otherwise the value of batch processing cannot be realized
|
|
| Floor8 willsort | Posted 2006-01-15 20:06 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re vptdosme:
Brother arding provided a program on the 5th floor. That is, if there is no third-party program used in CMD, if you want to implement it under Win9x/DOS, it will be much more complicated. You need to use a text traversal algorithm or use an ASCII assembler to echo out a similar replacement program for replacement. Unless it is really necessary, it is generally not recommended to adopt it. |
|
| Floor9 dosfroum | Posted 2006-02-16 09:57 |
| 初级用户 Posts 54 Credits 154 | |
Originally posted by Wengier at 2005-12-11 11:29: Sorry, this method doesn't seem to work Because CCC =12.5 #TEST If it is CCC =12.5 #TEST The number of spaces after each person's modification is not fixed, so that's why my title says Modify specific line data And the version owner proposed a string, so can you help solve it? Thanks |
|
| Floor10 dosfroum | Posted 2006-02-16 10:09 |
| 初级用户 Posts 54 Credits 154 | |
Originally posted by arding at 2006-1-12 20:18: This method can indeed modify, but there is a problem. It will cut off the data. That is, when your space is generated by the TAB key, the subsequent data is all cut off. Is there a way to overcome this? Thanks. Modify the line 11b_ATTEN_DUT_PM Original data Data after execution |
|
| Floor11 willsort | Posted 2006-02-16 11:45 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re dosfroum:
This has nothing to do with tab. When /f is used without parameters, it recognizes tab as a delimiter. The main reason is that the output of %%l is omitted in the original program. The example is as follows: |
|
| Floor12 220110 | Posted 2006-02-16 22:52 |
| 荣誉版主 Posts 313 Credits 718 | |
|
Re willsort:
There is a small mistake in your program, the semicolon is lost. Also, the program did not generate the "execution data" that dosfroum expects; instead, it followed the subsequent comments. |
|
| Floor13 willsort | Posted 2006-02-17 09:02 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
Re 220110:
The semicolon was indeed my oversight; due to time constraints, I didn't conduct strict testing. As for the comments, my understanding is that Brother dosfroum hoped to retain those comment information. As for these information losing the original alignment effect, that is the limitation of for /f. We can consider changing the spaces between %%i %%j %%k %%l to tabs, but the effect still may not be very ideal. If using awk, we can use a function similar to C language printf to achieve text alignment. |
|
| Floor14 dosfroum | Posted 2006-02-17 15:25 |
| 初级用户 Posts 54 Credits 154 | |
|
Could you change it so that the data except for that line can remain unchanged? A lot of blanks were removed, but it's already much better than before. Thanks!!
Before execution After execution |
|
| Floor15 dosfroum | Posted 2006-02-17 15:44 |
| 初级用户 Posts 54 Credits 154 | |
|
Sorry
Found another problem The blank in the first grid will increase And the value is not modified Batch file Original file After batch processing [ Last edited by dosfroum on 2006-2-17 at 15:45 ] |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |