readFile seemingly removes spaces and line endings from txt file

I have a gzillion of txt files like this:
sample.txt

11511500124                                    E42RON                                                                                                                                                                                                                                                                                                                                                                                                                                                               
1151151012490W-2093626.75                       SONG NAM                                                                                                                1XXAUTHX XXNAMEX                         XANOTHERX XAUTH NAME                             00653628529115CA104000100000000000343202310202312TELEVISION BROA642ROMANIA             00000001 000000000001C              00                                                                                                                    
1151151012490W-3925716.35                       XSONGX NAM                                                                                                              1XXAUTHX XXNAMEX                         XANOTHERX XAUTH NAME                             00653628529115CA106000100000000000514202310202312TELEVISION BROA642ROMANIA             00000001 000000000002C              00                                                                                                                    
11511590000000002000000000000857C000000000200000000000000000000                                                                                                                                                                                                                                                                                                                                                                                                                                                     

But with this template

{{ readFile "sample.txt" }}

the output is

11511500124 E42RON 1151151012490W-2093626.75 SONG NAM 1XXAUTHX XXNAMEX XANOTHERX XAUTH NAME 00653628529115CA104000100000000000343202310202312TELEVISION BROA642ROMANIA 00000001 000000000001C 00 1151151012490W-3925716.35 XSONGX NAM 1XXAUTHX XXNAMEX XANOTHERX XAUTH NAME 00653628529115CA106000100000000000514202310202312TELEVISION BROA642ROMANIA 00000001 000000000002C 00 11511590000000002000000000000857C000000000200000000000000000000

adding markdownify to the template

{{ readFile "sample.txt" | markdownify }}

produces this:

11511500124 E42RON
1151151012490W-2093626.75 SONG NAM 1XXAUTHX XXNAMEX XANOTHERX XAUTH NAME 00653628529115CA104000100000000000343202310202312TELEVISION BROA642ROMANIA 00000001 000000000001C 00
1151151012490W-3925716.35 XSONGX NAM 1XXAUTHX XXNAMEX XANOTHERX XAUTH NAME 00653628529115CA106000100000000000514202310202312TELEVISION BROA642ROMANIA 00000001 000000000002C 00
11511590000000002000000000000857C000000000200000000000000000000

which is a tad better but still useless for my purpose.

Is there a way or maybe a completely different approach to import the original un-trimmed content of my txt file?

I can assure you that the os.ReadFile function removes nothing. Print the value to console with warnf and you’ll see the newlines, etc.

If you want the browser to display the content instead of interpreting it as HTML, wrap it in a pre element:

{{ with os.ReadFile "sample.txt" }}
  <pre>{{ . }}</pre>
{{ end }}

Even better, don’t use the os.ReadFile function at all. Instead, place the file somewhere in the assets directory, and read it as a global resource:

{{ with resources.Get "sample.txt" }}
  <pre>{{ .Content }}</pre>
{{ end }}

Or place the file in a page bundle and do:

{{ with .Resources.Get "sample.txt" }}
  <pre>{{ .Content }}</pre>
{{ end }}

So true. Got duped by the browser. AGAIN! :frowning:

My goal is to split each txt in a slice, which I did after reading your reply, so any of your solutions work.

Thanks again @jmooring !

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.