Microsoft > Microsoft DOS

Add string to certain position

(1/1)

swede:
Hi all,

Long time no see  :)

I'd like to insert a string into position 15 in a oneliner .txt file using a batch script that utilizes Dave Benham's amazingly great repl.bat (https://www.dostips.com/forum/viewtopic.php?f=3&t=3855).
I have used repl.bat for years and it is just awesome.  Many thanks to Dave if you ever read this!  :)

Here's my batch script

--- Code: ---set value=add_this_string
type "file.txt"|repl.bat "(.{14})...............(.*)" "$1%value%$2" > "file.txt.new"
--- End code ---

Problem
The above script works fine if there are already characters in position 15. It also works if position 15 and onwards consist of whitespace. However, if the line ends before pos 15 the string is not inserted.

I discovered repl.bat's successor jrepl.bat (https://www.dostips.com/forum/viewtopic.php?f=3&t=6044) just the other day but haven't found someone who has solved this problem so any help using either one of them would be of great help!

Any help would be appreciated!  8)

DaveLembke:
My thought on this is that you will want to pad the files with added spaces for files that are under a 15 character count. You could do this to all files if you have a bunch of them and it will just add padding to the end of the lines in the files.

Here is a similar article to what you may need to do. If it were me, I'd pad the files by 15 spaces at the end of all lines, so all files will have a 15th character to target at {14}. Reading what you said it seems as though string length in each file is not important so padded lines with spaces shouldn't mess anything up.

https://superuser.com/questions/907792/how-can-i-pad-every-line-in-a-text-file-with-a-given-number-of-spaces-with-uneve

Below is quoted from that link using the power of SED. *You will need to edit this to add however many spaced to the end of lines that you want. Below only adds 3 spaces. Unable to play with this from work computer on my break :


--- Quote ---But if you're editing the file, sed will serve you well
--- End quote ---


--- Quote --- sed 's/^.*$/&\ \ \ /g' some_file > some_other_file
This will add 3 spaces to the end of each line in some_file and write the output to some_other_file. Note that the spaces are escaped and the ampersand serves the purpose of writing the first pattern to the output.
--- End quote ---

Navigation

[0] Message Index

Go to full version