【问题标题】:Batch file, how to remove the first word in a text file and only in the first line?批处理文件,如何删除文本文件中的第一个单词并且仅在第一行中?
【发布时间】:2016-09-13 03:55:04
【问题描述】:

我有一个 .txt 文件,其中可能包含不同行中的各种单词,我只想删除第一行中的第一个单词。 (例如:我的文本文件中有 2 行,每行包含 2 个单词(第一行是 abc,bcd,第二行是 cde,def),我希望输出在第一行是 bcd,第二行是 cde 和 def )。我对此进行了研究,我只遇到了如何删除所有行中的第一个单词,但我只需要在第一行。提前致谢。

这是我能找到的最接近的答案,但它从所有行中删除了第一个单词,我只需要第一行。 Remove First Word in text stream

【问题讨论】:

  • 如果你需要帮助,你需要展示你的努力!我们不为你做你的工作!请阅读帮助主题:How do I ask a good question?
  • @aschipfl 我明白了,我在帖子中添加了一个链接,但我还是批处理新手,我找不到其他任何东西,所以我真的需要答案!
  • 链接的帖子(Unix 相关)与标签 batch-file(Windows 特定)不匹配!成为“新事物”并不能证明不表现出任何自己的努力......
  • @aschipfl 我也使用 GNU32win 和我的批处理文件。你必须理解我,过去 3 晚我没有睡觉,只是为了制作一个脚本来找到一个单词的同义词并将其转移到一个新的文本文件中。我不得不熬夜通过论坛挖掘并查找信息,这是拼图的最后一块。我搜索了一切,尽我所能,所以我只想要这个问题的答案。我的项目明天到期,所以我希望你能帮助我,而不是在我已经指定我找不到任何东西时让我做更多的研究。
  • 你累了与我们无关。您使用 GNU32win 的信息也不是,因为该信息与您提出的问题无关。您坚持尽我所能而没有表现出任何努力,这与学生说诚实,老师!我确实做了作业,但我的狗吃了它!。如果你的项目明天到期,你应该早点开始,以便给自己更多时间来完成它——你没有自动神奇地原谅你做自己的研究或让它成为我们的问题。

标签: string batch-file text cut


【解决方案1】:

虽然你没有向我们展示你自己在手头的问题上所做的努力,但我还是决定提供一些代码,因为这个任务对我来说似乎不是特别琐碎......

下面的脚本从第一行删除第一个单词。

下面的代码使用for /F循环读取给定的文本文件,并用另一个嵌套的for /F循环分割第一行的第一个单词;其余行未经编辑返回:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "FLAG=" & rem // (this marks the first line)
for /F "delims=" %%L in ('findstr /N /R "^" "%~1"') do (
    set "LINE=%%L"
    rem // (toggle delayed expansion to not lose `!`)
    setlocal EnableDelayedExpansion
    set "LINE=!LINE:*:=!"
    if defined FLAG (
        rem // (this is executed for all but the first lines)
        echo(!LINE!
    ) else (
        rem // (this is executed for the first line only)
        if defined LINE (
            rem // (an empty line skipped over the loop)
            for /F "tokens=1,*" %%E in ("!LINE!") do (
                endlocal
                rem // (return line with first word removed)
                echo(%%F
                setlocal EnableDelayedExpansion
            )
        ) else echo/
    )
    endlocal
    rem // (set this after the first loop iteration)
    set "FLAG=#"
)

endlocal
exit /B

这个通过重定向读取给定的文本文件,通过for /F 循环再次拆分第一行的第一个单词,并通过findstr 命令行返回剩余的行:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

< "%~1" (
    rem // (capture first line here)
    set /P "LINE="
    rem /* (the `echo` in the pipe below runs in Command Prompt context,
    rem     so an empty variable `LINE` literally returned `!LINE!) */
    if defined LINE (
        rem /* (this is executed for the first line only;
        rem     delay variable expansion as much as possible) */
        for /F "tokens=1,*" %%I in ('cmd /V /C echo(!LINE!^| findstr /N /R "^"') do (
            rem // (return line with first word removed)
            echo(%%J
        )
    ) else echo/
    rem // (this is executed for all but the first lines)
    findstr /R "^"
)

endlocal
exit /B

两个脚本都希望输入文本文件作为命令行参数提供。假设任一脚本存储为remove-first-word.bat,文本文件名为sample.txt,则要使用的命令行如下:

remove-first-word.bat "sample.txt"

或:

remove-first-word.bat "\path\to\sample.txt"

要将输出写入另一个文件,例如return.txt,而不是控制台,请使用重定向:

remove-first-word.bat "sample.txt" > "return.txt"

或:

remove-first-word.bat "\path\to\sample.txt" > "\path\to\return.txt"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2013-12-20
    • 1970-01-01
    相关资源
    最近更新 更多