Solved

Adding characters to the beginning of each line

Posted on 2013-01-03
4
195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
asp.net c# execute .bat server side problem 6 95
Need Regex Money No $ 5 92
Java / Linux and Regular Expressions 11 103
REXEX help Part 2 2 64
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…
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…
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…

710 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