You are not logged in.
Hey best community on the internet,
I think the solution is pretty simple and probably I can't find the right search terms.
I used obsidian.md for an RPG Campaign I'm planning and made a lot of pages. Then I decided that a dokuwiki suits my needs better. So I bulk renamed the .md files to .txt. I used sed to remove all unnecessary markup and prepare internal links for the dokuwiki namespaces.
Now I have 274 content.txt files, each with a unique name that I want to keep.
And I have one template.txt with template code for the wiki pages . There is a specific line in the template where I want the content to be.
I thought there must be an elegant solution for this.
I thought about something like:
1. Splitting the template.txt at the point where I want the content to be. I'd get template_part1.txt and template_part2.txt
2. Then prepending template_part1.txt at the beginning of each content.txt
3. Then appending template_part2.text at the end of each content.txt
4. Win! Finally I can upload all files to my dokuwiki without having to copy&paste for several days.
Please help me finding a solution or the proper search terms.
Thanks in Advance
Solution
Always make a backup of your files!
1. I splitted template.txt in part1.txt and part2.txt . They are located in the parent directory
2. I opened a terminal in the folder with all the content.txt files.
I used:
sed -i '1r ../part1.txt' *.txtto insert part1.txt at the beginning of each file and
sed -i '$r ../part2.txt' *.txtto insert part2.txt at the end of each file.
3. Win ![]()
Last edited by timmo (2022-01-04 14:00:48)
Offline
To append to multiple files:
tee -a copy1 < src | tee -a copy2 | tee -a copy3 | ...Inofficial first vice president of the Rust Evangelism Strike Force
Offline
To append to multiple files:
tee -a copy1 < src | tee -a copy2 | tee -a copy3 | ...
Sorry that's not quite what I'm looking for as it only appends the text and I have 274 Files. But thank you. ![]()
Here are examples of the template, content and what I want.
my template.txt:
...code code code...
<WRAP noprint>
<button type="danger" collapse="gmn">GM Notiz: !Nicht lesen! </button>
<collapse id="gmn" collapsed="true"><well>
======> Insert content.txt here!
</well></collapse>
</WRAP>example content.txt:
...content content content...
**Primary Populated Planet**
Atmosphere: Breathable mix
Temperature: Warm, wet
Biosphere: Human-miscible biosphere
Population: Severals million inhabitants
Tech Level: TL 4 Baseline postech
World Tag #1: [[worldtag:Area 51]]
World Tag #2: [[worldtag:Tyranny]]
...content content content...The content.txt I want:
...code code code...
<WRAP noprint>
<button type="danger" collapse="gmn">GM Notiz: !Nicht lesen! </button>
<collapse id="gmn" collapsed="true"><well>
...content content content...
**Primary Populated Planet**
Atmosphere: Breathable mix
Temperature: Warm, wet
Biosphere: Human-miscible biosphere
Population: Severals million inhabitants
Tech Level: TL 4 Baseline postech
World Tag #1: [[worldtag:Area 51]]
World Tag #2: [[worldtag:Tyranny]]
...content content content...
</well></collapse>
</WRAP>Last edited by timmo (2022-01-04 12:49:02)
Offline
Sorry that's not quite what I'm looking for as it only appends the text
In that case you might want to edit the thread title to say 'insert' instead of 'append'.
Offline
timmo wrote:Sorry that's not quite what I'm looking for as it only appends the text
In that case you might want to edit the thread title to say 'insert' instead of 'append'.
Thanks. done.
Offline
So I think I may be on to something.
I splitted the template in part1 and part2. They are located in the parent directory.
if I use:
find . -name "*.txt" -print | xargs sed -i '1r ../part1.txt'it inserts part1.text at the beginning of the file.
find . -name "*.txt" -print | xargs sed -i '$r ../part2.txt'It inserts part2.txt at the end of the file.
BUT it doesn't work with files that contain a space in the filename.
Edit: I found the solution that worked best for me. See the initial post for it.
Last edited by timmo (2022-01-04 19:57:04)
Offline
i see you already figured it out while i was comming up with a solution, so here is my solution anyway:
a simple bash script:
#!/bin/bash
for f in content_files/*; do
cat template1 "$f" template2 > content_fixed/"${f/content_files\//}"
doneso you need to have template1 and template2 in the same folder as the script, they are the before and after parts.
then you need to have 2 folders in the same folder as the script, "content_files" and "content_fixed", and you put a copy of all your existing content.txt files in the "content_files" folder and then run the script and the "content_fixed" folder should have what you want.
that was a terrible explanation but i think you get the point.
Offline
i see you already figured it out while i was comming up with a solution, so here is my solution anyway:
a simple bash script:
#!/bin/bash for f in content_files/*; do cat template1 "$f" template2 > content_fixed/"${f/content_files\//}" doneso you need to have template1 and template2 in the same folder as the script, they are the before and after parts.
then you need to have 2 folders in the same folder as the script, "content_files" and "content_fixed", and you put a copy of all your existing content.txt files in the "content_files" folder and then run the script and the "content_fixed" folder should have what you want.that was a terrible explanation but i think you get the point.
Thanks for this solution. ![]()
Offline