add text at beginning and end of each line in TextEdit

I have a list of numbers I want to convert into a list of numbered filenames.


I can use Find/Replace and Insert Pattern feature to select line break and replace with end of file name but can't figure out how to add text at beginning of every line.


Ideally, since I can choose a number with Insert Pattern for the find, if I could embed that number in the middle of the outputted text in one go that would be perfect.


Any help here much appreciated.


Thanks.

Posted on Oct 15, 2021 6:02 AM

Reply
10 replies

Oct 15, 2021 8:12 AM in response to DavidBEDSTUY

Provided your list of numbers is in a plain text file, you can use sed in the Terminal to prefix and postfix those numbers in that file with one command. Assumption is that the prefix and postfix strings are the same for each differently numbered file. By example, the input file has a list of different length numbers:


odin: ~/Desktop % more numbers.txt
00005
01
00125
00432

and using the sed (stream editor):


odin: ~/Desktop % sed -En 's/([0-9]+)/prefix&postfix.txt/p' numbers.txt
prefix00005postfix.txt
prefix01postfix.txt
prefix00125postfix.txt
prefix00432postfix.txt

and if you wanted to edit the numbers.txt file in place:


odin: ~/Desktop % sed -i '' -En 's/([0-9]+)/prefix&postfix.txt/p' numbers.txt
odin: ~/Desktop % more numbers.txt
prefix00005postfix.txt
prefix01postfix.txt
prefix00125postfix.txt
prefix00432postfix.txt


Oct 15, 2021 8:24 AM in response to VikingOSX

Wow! For the win on both fronts!!


...so you might have guessed, but this is preparation so the file can be ingested into the script you were helping with in that other thread.


Since you know the end result I can't help but ask: is there a way to combine these two tasks?


I have a list of numbers that need to be converted to a list of filenames so the script can select the files and copy them to another folder.


Can that script be altered to ingest a text list of numbers and prompt for the pre/post fix?


Explicitly: it currently prompts for incoming folder, outgoing folder, and file list. Is there a way to add a prompt to enter the file name to wrap around the numbers in the selected text?

Oct 15, 2021 8:42 AM in response to DavidBEDSTUY


A fourth prompt could ask for a text string (e.g. prefix|postfix.ext) and then split that into separate prefix and postfix variables on that pipe (|) delimiter. One could then read the filename containing a list of just numbers into a list as before, and then generate a new match_list of entries built from the concatenated prefix, number, and postfix names. Then the Finder could get the files from the infolder that match those in the constructed match_list.


The limitation is that you can only provide a single prefix|postfix.ext string per application run. Does this sound like what you want to do with that previous AppleScript?

Oct 15, 2021 9:04 AM in response to VikingOSX

That is exactly what I would want to do!


The limitation you mention is not an issue since the way it would be used each instantiation would be for one list/folder or sequential files. A whole other list all of another separate naming convention would then be used. And so on and so on.


So a prompt for input and output folder and then a prompt to select text file of file list (which are already there, obviously) and then a last prompt to convert each line item in list from say "3" and "5" into "IMAGE_NAME_3.JPG" and "IMAGE_NAME_5.JPG" respectively would in one fail swoop solve everything, make the clouds part, the sun come out and make the world a generally better place. ;-D


To be clear, the file itself wouldn't need to be altered. After the script the file list text would be garbage so its only how the script handles the info that is of interest, not whether to not the newly made file list gets sent back to text file. Unless doing so makes for easier scripting, it is a totally unnecessary step.

Oct 15, 2021 9:18 AM in response to DavidBEDSTUY

DavidBEDSTUY wrote:

That is exactly what I would want to do!

and then a last prompt to convert each line item in list from say "3" and "5" into "IMAGE_NAME_3.JPG" and "IMAGE_NAME_5.JPG" respectively would in one fail swoop solve everything


Should I assume for the above case, that separate line items in the file list file are 3 and 5, and you want the prefix/postfix prompt to allow you to enter the following string at a prompt:


IMAGE_NAME_|.JPG


and I split this on the pipe (|) symbol into a prefix string of "IMAGE_NAME_" and a postfix string of ".JPG", and these are used to construct IMAGE_NAME_3.JPG and IMAGE_NAME_5.JPG in the match_list entries?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

add text at beginning and end of each line in TextEdit

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.