1

I am just looking for a quick way to look at the contents of a text document and then add text to the beginning of each line. Every line will get the same text at the beginning. Maybe an easy script to do this. Any ideas or suggestions. Thanks

16
  • A little more info please. What do you mean by script? Is it a bash, do you have unix binutils available for scripting? Should it be cross-platform with python/php/perl?
    – inVader
    Jul 24, 2012 at 15:31
  • I have a text file with hundreds of lines. I need to insert text at the beginning of each of these lines. Trying to figure out a way to add this text to each line within the text document without having to do each line manually.
    – b-rizzle
    Jul 24, 2012 at 15:35
  • Does the answer below help you? If not, what OS are you running and what tools do you have available? SED should be standard in most Linux distributions but you could get it for Win as well.
    – inVader
    Jul 24, 2012 at 15:46
  • Sill I think sed is the way to go. Do you have any scripting languages like python already installed?
    – inVader
    Jul 24, 2012 at 15:49
  • No, no scripting languages installed. So would sed just work in a batch file?
    – b-rizzle
    Jul 24, 2012 at 15:52

3 Answers 3

2

One of the easist ways would be using sed (stream editor) which can read from file or stdin. It does so on a line-by-line basis and can apply modification of the content either for the whole file or for specific sub-ranges. This is what you will want to do:

sed -i 's/^/TEXT/' FILENAME

The -i options tells sed to edit the file "in place", meaning it's directly written into the file instead of going to stdout. The original file is overwritten. If you want to keep the original data just ommit this option and pipe the output into a different file by appending > NEWFILE to the commandline above.

The s command in sed is search/replace. It will, on each line, replace the first occurance of the first pattern with the second pattern. You can add g at the end to replace every occurance of the first pattern in any line but since you're matching the beginning of the line this is not particullary useful here.

The first pattern here is ^ which is a reserved character in regular expressions meaning the beginning of the line. So it will just add the second pattern TEXT before any other text appears.

Of course you can call that from a script if you have to batch-process many files in that way. Hope that helps.

0

If you want to do it fairly cross plattform you could write a short and simple python script.

inputFile = open("fileNameInput")
outputFile = open("fileNameOutput", "w")

for inputLine in inputFile:
    outputFile.write("your text" + inputLine + "\n")

inputFile.close()
outputFile.close()
0

Can easily be done in VB Script, too. If you're looking for a native Windows solution, using this little script snippet:

Set FSO = CreateObject("Scripting.FileSystemObject")

Set inputFile =  FSO.OpenTextFile("inputFilePath", ForReading)
Set outputFile = FSO.OpenTextFile("outputFilePath", ForWriting)

Do Until inputFile.AtEndOfStream

        outputFile.Write("Your Text" & inputFile.ReadLine)

Loop

inputFile.Close
outputFile.Close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.