-2

I have a .txt file that may contain various words on various lines and I just want to remove the first word in the first line. (ex: I have 2 lines in my text file containing 2 words each (abc, bcd on the first line and cde, def on the second) annd I want the output to be bcd on the first line and cde and def on the second). I researched this and I only came across to how to remove the first word in all the lines but I only need in the first line. Thanks in advance.

This was the closest answer I could find out but it removes the first word from all the lines and I only need the first line. Remove First Word in text stream

Community
  • 1
  • 1
  • You need to show your efforts if you want help! We don't do your work for you! Please read the help topic: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) – aschipfl May 17 '16 at 07:36
  • @aschipfl I understand, I added a link in the post but I'm still new to batch and I couldn't find anything else so please I really need the answer! – Tran Stephen May 17 '16 at 07:43
  • The linked post (Unix-related) does not match the tag [tag:batch-file] (Windows-specific)! Being "new to something" does not justify to not show any own efforts... – aschipfl May 17 '16 at 07:56
  • @aschipfl well I also use GNU32win along with my batch file. You have to understand me, I haven't slept the past 3 nights just to make a script to find a word's synonym and transfer that to a new text file. I had to stay up all night to dig in through forums and find info and this is the very last piece of the puzzle. I searched everything, I gave every effort I could, so I just want the answer to this question. My project is due tomorrow so I would appreciate you help me instead of sending me to do more research when I already specified I wasn't able to find anything. – Tran Stephen May 17 '16 at 08:07
  • I am not asking for more research, I am just asking for what you already have... Anyway, my knowledge aboud `sed` (as used in the tread you linked) is really poor, but I could imagine that one of the following command lines work: `sed -e '/^\w*[\ \t]+/M' test.txt` or `sed -e '1/^\w*[\ \t]+/' test.txt` (I cannot test it as I do not have this tool installed here)... – aschipfl May 17 '16 at 18:26
  • 1
    Your being tired isn't relevant to us. Neither is the information that you're using GNU32win, because that information is irrelevant to the question you've asked. Your insistence that you *gave every effort I could* without showing any of that effort is the same as the student saying *Honest, teacher! I DID do my homework, but my dog ate it!*. If your project is due tomorrow, you should have started it sooner in order to give yourself more time to complete it - that you didn't does not auto-magically excuse you from doing your own research or make it our problem. – Ken White May 19 '16 at 22:09

1 Answers1

1

Although you did not show us your own efforts on the problem at hand, I decided to provide some code, because the task seems not that particularly trivial to me...

The scripts below remove the first word from the first line.

The following one reads the given text file using a for /F loop and splits off the first word in the first line by another nested for /F loop; the remaining lines are returned unedited:

@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

This one reads the given text file via redirection, splits off the first word of the first line again by a for /F loop and returns the remaining lines by a findstr command line:

@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

Both scripts expect the input text file to be provided as a command line argument. Supposing either script is stored as remove-first-word.bat and the text file is called sample.txt, the command line to be used is as follows:

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

or:

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

To write the output into another file, say return.txt, rather than to the console, use redirection:

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

or:

remove-first-word.bat "\path\to\sample.txt" > "\path\to\return.txt"
aschipfl
  • 33,626
  • 12
  • 54
  • 99