an easier way to substitute text in the middle of a line?

I hope this question is clear - I’ll try to explain the best I can.

Let’s say I have a line of text and in the middle is a word or phrase that I want to replace with the contents of a list - each time with a different substitution.

For example, it could be a command that I need to run on a list of usernames:
dothing USERNAME -someparamater

So I want my list of 50 usernames to replace USERNAME on 50 different lines.

But it’s not only commands, it could be for a list of addresses, or anything else.

Right now what I do works - but it doesn’t seem very elegant. I paste the list of usernames (in this case) into Word, and replace the special character “Paragraph Mark” with whatever text follows the end of the username, then add another paragraph mark, and then add whatever text precedes the username. Usually I need to clean up the first and last rows, but it works.

I know I could do this programatically, but I’m looking for something simple, that doesn’t require any programming knowledge and can be done in a GUI.

Any ideas? (And did I make sense at all?)

Excel should be able to do that for you very easily; just set up a list of usernames and in the column next to that, use a formula to get the combined text you want. You could also use an online spreadsheet like Google Docs - same principle. When you’re done, copy and paste the values out into a text editor, or back into the spreadsheet as values, to have just the text that you want.

Thanks!

I’ve tried Excel, but I find that when I paste the results into a text editor, I have issues with tabs instead of spaces. Any easy solution to that?

Are you talking about Mail Merge?

Basically, you want to send a letter to a large number of people. You write a template “Dear [Title] [Surname]”, and a list of names and addresses, then the word processor prints out lots of copies, substituting the correct data in each one.

Copying cells next to each other will add a tab, so you can **concatenate **your different cells into one cell. E.g., to put together three columns A B and C, put this in the fourth “=A1 & " " & B1 & " " & C1”.

and copy the formula down to get



A	B	C	D
First	Name1	Second	=A1 & " " & B1 & " " & C1
First	Name2	Second	=A2 & " " & B2 & " " & C2
First	Name3	Second	=A3 & " " & B3 & " " & C3

Column D should paste with line returns (in Word paste with "Keep Text Only).

Peter Morris has it. This is exactly what the Mail Merge function is for.

The Excel kludge will be ok for smaller jobs, but it will be a lot more work for larger ones.

I have a plain-old-DOS-based-plain-old-text editor (QED, or simply Q) that is still my preferred plain-old-plain-text editor. It has a simple macro recorder that allows me to record a sequence of keystrokes, and then I can assign a single key to press that will repeat the whole sequence.

So I start a new file, and type in the list of user names, one per line:

Andy
Bruce
Chuck
David
(etc.)

Then, move the cursor to line #1, start the keystroke recorder, then edit that line to add the surrounding text. Be sure to finish editing that line by leaving the cursor on the following line:

someprogram -username Andy -whatever-other-stuff
Bruce [leaving cursor at start of this line.]
Chuck
David
(etc.)

Then stop the keystroke recorder. Then just press my one-key assigned macro playback and it will do the same to the second line. Keep pressing the key (or just hold it down) until entire file is done.

someprogram -username Andy -whatever-other-stuff
someprogram -username Bruce -whatever-other-stuff
someprogram -username Chuck -whatever-other-stuff
someprogram -username David -whatever-other-stuff
(etc.)

I think there are many plain-old-plain-text editors that can do something similar (a notable exception being Windows Notepad, which is waaaay primitive).

ETA: If my goal is to run a command repeatedly with similar options but with just one item different each time (as the OP’s example seems to suggest), I use the above technique to create a DOS Batch file or similar, then run that.

Thanks!

If you’re trying to do DOS (more properly cmd.exe) commands, and you’ve got any power user / scripting chops at all, look into the FOR command. It was designed for exactly this use case. It can consume a file of parameters, parse them, and stuff them into a command of your choosing.

For more complex situations you can combine that with creating a simple batch file to accept one row of your parameters and then run multiple commands on each such set of parameters.

It’s remarkable how much you can accomplish with the built-in features of cmd.exe. It’s almost a lost art. When I was doing a lot of this 5-10 years ago the youngun’s in my department were amazed; these newbie professional devs had just never learned any of that old magic.