Solved

Adding characters to the beginning of each line

Posted on 2013-01-03
4
189 Views
Last Modified: 2013-01-04
How can I add some text to the beginning of each line in a txt file within notepad?
0
Comment
Question by:NickMalloy
  • 3
4 Comments
 
LVL 14

Expert Comment

by:Michael Dyer
ID: 38742097
Here is a batch file that will add a text string to the beginning of each line in a file.  This example reads file.txt and creates new_file.txt

REM Add text to the beginning of each record
REM Change $text to the correct string you want appended to each record

Echo off
cls
ECHO Processing Records - please wait
ECHO.

REM create a environmental variable with the text in it
  set $text=TEXT TO BE APPENDED

REM process FILE.TXT

      IF exist new_file.txt del new_file.txt

      setlocal EnableDelayedExpansion
       set x=%$text%
      for /f "delims=" %%i in (file.txt) do (
        echo !x!%%i >> new_file.txt
      )

REM output file is new_file.txt
0
 

Author Comment

by:NickMalloy
ID: 38742150
awesome. How would I extend this to add some text to the end of each line?
0
 
LVL 14

Expert Comment

by:Michael Dyer
ID: 38742231
you just have to edit the echo line - this is where the text is combined with the record.  %%i represents the original record.  !x! is the new text.  

for example, to add the text at the end of the line you would change this to:

echo %%i!x! >> new_file.txt
0
 
LVL 14

Accepted Solution

by:
Michael Dyer earned 330 total points
ID: 38742242
This example adds text at the beginning and the end of the record:

REM add text to the beginning and end of each record
Echo off
cls
ECHO Processing Records - please wait
ECHO.

REM create a environmental variable with the text in it

set $fronttext=TEXT TO BE APPENDED at the beginning
set $backtext=TEXT TO BE APPENDED at the end

REM process FILE.TXT

      IF exist new_file.txt del new_file.txt

      setlocal EnableDelayedExpansion
       set x=%$fronttext%
                   set y=%$backtext%

      for /f "delims=" %%i in (file.txt) do (
        echo !x!%%i!y! >> new_file.txt
      )

REM output file is new_file.txt
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now