I would like to add a predefined text to each new line on a text file and create a new text file with the added text. Please help.
Asked
Active
Viewed 4,728 times
2
-
Under which OS ? Is the added text the same for all lines ? In this case, just do: "sed -e 's/^/yourtext/' old_file > new_file" – Patrick Feb 12 '10 at 16:17
1 Answers
2
In Windows, this will do it:
(for /f "delims=" %L in (oldfile.txt) do @echo predefined text %L)> newfile.txt
Note that in a batch file you'll need to use double %
signs:
(for /f "delims=" %%L in (oldfile.txt) do @echo predefined text %%L)> newfile.txt
Note also that if you don't put the ">" right after the %L, you will get a space after every line. If you use ">>" instead of ">" you will keep adding on to newfile.txt instead of creating a new one each time you run it.

Gabe
- 84,912
- 12
- 139
- 238
-
1Note that %L works when you type this line in on the command line. If you want to put it in a batch or cmd file, you need two percent signs on L. So it'd be %%L instead of %L. – indiv Feb 12 '10 at 16:41
-
Edited slightly for correctness and noting other issues. Hope you don't mind. – Joey Feb 13 '10 at 15:06
-
*sigh* ok, I'll refrain from editing, then. In this form it's incorrect, you know? Just try it with a file that contains more than just a single line of text. Agreed though, that digits don't cause harm in this specific case. – Joey Feb 13 '10 at 18:23
-
Johannes was right...you need parens or the redirection applies to the echo instead of the for. – Gabe Feb 13 '10 at 21:03