You are not logged in.

#1 2022-01-04 11:45:53

timmo
Member
From: Germany
Registered: 2014-11-10
Posts: 10

[SOLVED] How to insert Text from one file to multiple files

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' *.txt

to insert part1.txt at the beginning of each file and

sed -i '$r ../part2.txt' *.txt

to insert part2.txt at the end of each file.

3. Win big_smile

Last edited by timmo (2022-01-04 14:00:48)

Offline

#2 2022-01-04 12:03:23

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,669
Website

Re: [SOLVED] How to insert Text from one file to multiple files

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

#3 2022-01-04 12:48:17

timmo
Member
From: Germany
Registered: 2014-11-10
Posts: 10

Re: [SOLVED] How to insert Text from one file to multiple files

schard wrote:

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. smile


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

#4 2022-01-04 13:05:06

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [SOLVED] How to insert Text from one file to multiple files

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'.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#5 2022-01-04 13:06:49

timmo
Member
From: Germany
Registered: 2014-11-10
Posts: 10

Re: [SOLVED] How to insert Text from one file to multiple files

Slithery wrote:
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

#6 2022-01-04 13:34:23

timmo
Member
From: Germany
Registered: 2014-11-10
Posts: 10

Re: [SOLVED] How to insert Text from one file to multiple files

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

#7 2022-01-04 14:22:05

jonno2002
Member
Registered: 2016-11-21
Posts: 887

Re: [SOLVED] How to insert Text from one file to multiple files

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\//}"
done

so 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

#8 2022-01-04 19:58:39

timmo
Member
From: Germany
Registered: 2014-11-10
Posts: 10

Re: [SOLVED] How to insert Text from one file to multiple files

jonno2002 wrote:

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\//}"
done

so 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. smile

Offline

Board footer

Powered by FluxBB