从批处理中查找并添加文本文件中的行

时间:2021-12-11 02:28:43

I would like to know if anyone can help me with this. I would like to search a text file for a certain line of text, add a new line under the specific line then add text to the new line. I will be using this to edit the firefox.js file to add the line of text to add support for Iprism. It will run on XP and Windows 7 machines.

我想知道是否有人可以帮助我。我想在文本文件中搜索特定的文本行,在特定行下添加一个新行,然后将文本添加到新行。我将使用它来编辑firefox.js文件以添加文本行以添加对Iprism的支持。它将在XP和Windows 7机器上运行。

I would like to have a batch file that will open firefox.js find the line "pref("browser.xul.error_pages.expert_bad_cert", false);" add a new line uder it and add pref("network.automatic-ntlm-auth.trusted-uris", "IP of Iprsim");

我想要一个打开firefox.js的批处理文件找到“pref(”browser.xul.error_pages.expert_bad_cert“,false)行;”添加新行并添加pref(“network.automatic-ntlm-auth.trusted-uris”,“Iprsim的IP”);

Edited for better explanation!!

编辑更好的解释!

Any help would be very appreciated!!!

任何帮助将非常感谢!!!

Damian

达米安

1 个解决方案

#1


1  

You can iterate over lines in a file with for /f. You need to keep track of the line you're currently at, compare it to what you look for and in case you found the line you were looking for do something. This something looks roughly as follows: You output each line you process to stdout or a new file directly and when you find your desired line you do exactly the same but write something else to that new file as well. At the very end you simply delete your old input file and rename the new one.

您可以使用for / f迭代文件中的行。您需要跟踪当前所在的线路,将其与您查找的线路进行比较,以防万一找到您要寻线的线路。这个内容大致如下:您将处理的每一行直接输出到stdout或新文件,当您找到所需的行时,您完全相同,但也会向该新文件写入其他内容。在最后,您只需删除旧的输入文件并重命名新的输入文件。

In a batch file this might look somehow like the following (untested, so careful):

在批处理文件中,这可能看起来像以下(未经测试,如此小心):

for /f %%x in (inputfile) do (
    echo %%x>>newfile
    if ("%%x"=="Ex3") (
        echo Ex4>>newfile
    )
)

del inputfile
ren newfile inputfile

Of course, adapt as you see fit.

当然,根据您的需要进行调整。

#1


1  

You can iterate over lines in a file with for /f. You need to keep track of the line you're currently at, compare it to what you look for and in case you found the line you were looking for do something. This something looks roughly as follows: You output each line you process to stdout or a new file directly and when you find your desired line you do exactly the same but write something else to that new file as well. At the very end you simply delete your old input file and rename the new one.

您可以使用for / f迭代文件中的行。您需要跟踪当前所在的线路,将其与您查找的线路进行比较,以防万一找到您要寻线的线路。这个内容大致如下:您将处理的每一行直接输出到stdout或新文件,当您找到所需的行时,您完全相同,但也会向该新文件写入其他内容。在最后,您只需删除旧的输入文件并重命名新的输入文件。

In a batch file this might look somehow like the following (untested, so careful):

在批处理文件中,这可能看起来像以下(未经测试,如此小心):

for /f %%x in (inputfile) do (
    echo %%x>>newfile
    if ("%%x"=="Ex3") (
        echo Ex4>>newfile
    )
)

del inputfile
ren newfile inputfile

Of course, adapt as you see fit.

当然,根据您的需要进行调整。