0

i'm trying to add a new line with data from a file to another file. The problem is, the data in the source file is with whitespaces so i do not get everything included.

Here's my code:

Source File: token

cat token
Token = [223, 146, 134, 157, 244, 212, 236, 171]

My Merge File: preconfig.ini

...
EnableDebugConsole = false
AllowStreamPush = false
UploadLimit = 83886080
RunAsUser = 0
RunAsGroup = 0
...

Target File is the config.ini with the added token. I use sed:

for i in $(cat token); do (sed -e '/EnableDebugConsole/a\' -e $i preconfig.ini > config.ini); done

After this my config.ini look like this:

...
EnableDebugConsole = false
171]
AllowStreamPush = false
UploadLimit = 83886080
RunAsUser = 0
RunAsGroup = 0
...

So the problem seems to be with the wihitespaces? I had tryed with "$i" but the result stays the same. Any ideas?

  • Please show us an example of the desired output. – fpmurphy Nov 6 '18 at 15:53
0

You probably mean to load each line into the file. By using a for loop with cat you are actually loading each "word" that is denoted by a space. Use read to fetch each line.

while read file; do (sed -e '/EnableDebugConsole/a\' -e $file preconfig.ini > config.ini); done < token
  • Your Code adds me the First Word ... Token ... – Kjellson Oct 30 '18 at 7:29
0

Is there a specific reason you are looping for this if you only have a small amount of content to add?

If you have multiple files or something of that nature or are just seeking to take the content of one file and insert into another this SE question seems to have a good answer: https://unix.stackexchange.com/questions/141387/sed-replace-string-with-file-contents

Awk sounds like it may be your best bet in this case albeit a bit more complex.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.