Solved

Adding characters to the beginning of each line

Posted on 2013-01-03
4
194 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

740 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