Unix/Linux Go Back    


Windows & DOS: Issues & Discussions 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

Windows & DOS: Issues & Discussions


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-01-2012
pasc pasc is offline
Registered User
 
Join Date: May 2011
Last Activity: 14 June 2017, 7:09 AM EDT
Posts: 140
Thanks: 26
Thanked 1 Time in 1 Post
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
Sponsored Links
    #2  
Old Unix and Linux 07-01-2012
NareshN NareshN is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 4 December 2014, 4:31 AM EST
Posts: 33
Thanks: 0
Thanked 1 Time in 1 Post
use the below script to add text infront of each line :

sed 's/^/STARTTEXT/g' filenmae > output_file
Sponsored Links
    #3  
Old Unix and Linux 07-01-2012
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 20 October 2017, 3:07 PM EDT
Location: Zürich
Posts: 8,750
Thanks: 356
Thanked 1,161 Times in 995 Posts
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 Unix and Linux 07-01-2012
pasc pasc is offline
Registered User
 
Join Date: May 2011
Last Activity: 14 June 2017, 7:09 AM EDT
Posts: 140
Thanks: 26
Thanked 1 Time in 1 Post
so... without sed it is impossible ?
Sponsored Links
    #5  
Old Unix and Linux 07-01-2012
methyl methyl is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 8 June 2016, 9:58 PM EDT
Posts: 6,402
Thanks: 288
Thanked 676 Times in 645 Posts
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.
Sponsored Links
    #6  
Old Unix and Linux 07-02-2012
pasc pasc is offline
Registered User
 
Join Date: May 2011
Last Activity: 14 June 2017, 7:09 AM EDT
Posts: 140
Thanks: 26
Thanked 1 Time in 1 Post
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
Sponsored Links
    #7  
Old Unix and Linux 10-18-2012
scottaazz scottaazz is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 6 November 2012, 8:00 PM EST
Posts: 28
Thanks: 0
Thanked 7 Times in 7 Posts
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

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to insert text(constant) at the beginning of file jclanc8 Shell Programming and Scripting 2 06-24-2012 12:00 AM
How to specify beginning-of-line/end-of-line characters inside a regex range jawsnnn UNIX for Dummies Questions & Answers 3 06-20-2012 01:31 PM
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
Unix Script with line number at beginning of each line. mascorro Shell Programming and Scripting 5 06-19-2006 04:34 PM



All times are GMT -4. The time now is 05:25 PM.