Trying to add text to the beginning of each line

 
Thread Tools Search this Thread
Special Forums Windows & DOS: Issues & Discussions Trying to add text to the beginning of each line
# 1  
Old 07-01-2012
Trying to add text to the beginning of each line

Well here goes:

I tried to write a batch file that adds a specific fixed text to each line of an already existing text file.

for the adding text infront of each line I tried this:

Code:
for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt

for adding text after each line I haven't figured out how to do it.


HOWEVER:

When I use the code descripted, this:

Code:
1stline

becomes this ->>>
Code:
1stline
STARTTEXT1stline

I want it to be just
Code:
STARTTEXT1line

so to sum it up I want to add something BEFORE and AFTER the 1line (for each line in a text file), like this:

Code:
STARTTEXT1stlineENDTEXT
STARTTEXT2ndlineENDTEXT
STARTTEXT3rdlineENDTEXT
...

Any help is appreciated
# 2  
Old 07-01-2012
use the below script to add text infront of each line :

sed 's/^/STARTTEXT/g' filenmae > output_file
# 3  
Old 07-01-2012
Please note: This question is in the "Windows & Dos..." forum. A Unix-like answer may not be appropriate here.

Having said that, and in the absence of a batch script answer, sed is available for Windows:

sed for Windows
# 4  
Old 07-01-2012
so... without sed it is impossible ?
# 5  
Old 07-01-2012
With Windows scripting, it always helps to mention the exact version of Windows and whether you have installed any extended Windows scripting products and whether you have any programming languages installed.
# 6  
Old 07-02-2012
Well I got Windows 7 Ultimate 32 bit.

No additional extended scripting products installed.

if sed is the only way I'd take an example for that aswell.

Thanks
# 7  
Old 10-18-2012
The issue is that you are attempting to read the same file as you are writing. You can go ahead and use the code you wrote but instead of writing to the same file, write to a temporary file and then copy it over the original file

Code:
for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> tempfile.txt
 
followed by 
copy /Y tempfile.txt list.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert text at the beginning of every even number line

i am trying to insert text at the beginning of every even number line with awk i can do it with odd number lines with this command awk 'NR%2{$0="some text "$0}1' filehow can i edit this command thanks (5 Replies)
Discussion started by: bob123
5 Replies

2. Shell Programming and Scripting

How to add one line in the beginning of the file?

Hi gurus, I need add one new line in the begining of current file. current file abc cde add xyz output file newline abc cde add xyz (6 Replies)
Discussion started by: ken6503
6 Replies

3. Shell Programming and Scripting

Add new line at beginning and end of a file

Hi, I have a specific requirement to add text at the beginning and end of a plain text file. I tried to use "sed" with '1i' and '$a' flags but these required two separate "sed" commands separated with "|". I am looking for some command/option to join these two in single command parameter. ... (6 Replies)
Discussion started by: bhupinder08
6 Replies

4. Shell Programming and Scripting

Adding a text in the beginning of a line

Hi, I am doing something like below: cat file1>file3and cat file2>>file3 I wanted to check if there is a way to write a custom message(hardcoded message)something like below at the beginning of each line then PIPE delimitiation and then followed by remaining record. cat file1... (7 Replies)
Discussion started by: Saanvi1
7 Replies

5. Shell Programming and Scripting

How to add a text at the beginning of a text files in a folder?

how to add a text ( surya) at the beginning of a text files (so many) in folder text file: 111111 555555 666666 result: surya 111111 555555 666666 (3 Replies)
Discussion started by: suryanarayana
3 Replies

6. UNIX for Dummies Questions & Answers

sed - Add a variable line to the end of a block beginning with a regex

Hi, Need some help with sed. I have a file that has sections : e.g. a=blah b=blah d=blah e=blah There's many sections in the file. (1 Reply)
Discussion started by: andyatit
1 Replies

7. UNIX for Dummies Questions & Answers

Add word/value at the beginning of each line in a file

how to add value/word at the beginning of each line in a file ? i have file number.txt and the output is below 1000 1001 1002 1003 1004 i want to add 000 at the beginning of each line, desire output is below 0001000 0001001 0001002 0001003 0001004 and so on please advise how... (5 Replies)
Discussion started by: jason6247
5 Replies

8. Shell Programming and Scripting

trying to add text to beginning and end of each line

Well here goes: I tried to write a batch file that adds a specific fixed text to each line of an already existing text file. for the adding text infront of each line I tried this: for /F "delims=" %%j in (list.txt) do echo.STARTTEXT\%%j >> list.txt for adding text after each line I... (0 Replies)
Discussion started by: pasc
0 Replies

9. Shell Programming and Scripting

add a number to the beginning of every line

hey, i would like to add a line number to the beginning like so: red blue green yellow will be: 1=>red 2=>blue 3=>green 4=>yellowplease advise thank u. (5 Replies)
Discussion started by: boaz733
5 Replies

10. Shell Programming and Scripting

Add text to beginning of file

Hi I need to add text to the beginning of a file in the same way that cat will put file contents at the end of a file. I want to do this with many files eg cat newtext >> /usr/home/*/*.bat Any ideas? (2 Replies)
Discussion started by: donkey
2 Replies
Login or Register to Ask a Question