👤
Home Man
Search
Today's Posts
Register

All Windows and DOS questions should go here as well. Discuss UNIX to Windows (Desktop or Server) here!

Trying to add text to the beginning of each line

👤 Login to reply

 
Thread Tools Search this Thread
# 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 to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Insert text at the beginning of every even number line bob123 Shell Programming and Scripting 5 04-04-2016 02:01 PM
How to add one line in the beginning of the file? ken6503 Shell Programming and Scripting 6 06-25-2014 02:40 PM
Add new line at beginning and end of a file bhupinder08 Shell Programming and Scripting 6 06-23-2014 04:50 AM
Adding a text in the beginning of a line Saanvi1 Shell Programming and Scripting 7 01-28-2014 01:11 AM
How to add a text at the beginning of a text files in a folder? suryanarayana Shell Programming and Scripting 3 09-24-2013 03:09 PM
sed - Add a variable line to the end of a block beginning with a regex andyatit UNIX for Dummies Questions & Answers 1 04-19-2013 09:30 AM
Add word/value at the beginning of each line in a file jason6247 UNIX for Dummies Questions & Answers 5 12-07-2012 10:44 AM
trying to add text to beginning and end of each line pasc Shell Programming and Scripting 0 07-01-2012 07:36 AM
add a number to the beginning of every line boaz733 Shell Programming and Scripting 5 12-15-2011 09:39 AM
Add text to beginning of file donkey Shell Programming and Scripting 2 08-24-2007 07:24 AM


All times are GMT -4. The time now is 09:37 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password