-2

How do I load a certain line from a text document (for example line 546) into a website?

waka
  • 3,362
  • 9
  • 35
  • 54
JakobLbk
  • 23
  • 2
  • What language are you trying to do this in? JavaScript, C#, PHP, etc? – Obsidian Age Oct 11 '17 at 20:14
  • @JakobLbk you will not be able to do this without using another language. JavaScript would probably be easiest for you if you are fairly new. This page has a lot of awesome information on html, css, javascript, etc: https://www.w3schools.com/html/default.asp . I would recommend checking it out and really taking your time to understand what you are trying to do here. – ethan codes Oct 11 '17 at 20:17
  • Welcome to Stack Overflow. Please review [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). Questions should show **evidence of research and attempts to solve the problem yourself**, a clear outline of your specific coding-related issue, and any relevant code in a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), so we have enough information to be able to help. – FluffyKitten Oct 12 '17 at 02:12

2 Answers2

0

Do it with PHP. Use the fopen(), fread() and fclose() methods. To get a specific line of txt is relative, because the formatting of .txt

Start with w3schools and PHP manual. The best way to figured it out is to try and documentations. In my case it was helpful all the way.

Maximilian Fixl
  • 670
  • 6
  • 23
0

I propose this solution using old technology because you asked to make this work for an old file format (html)! So here it goes! Don't be scared! What we are going to do is use Server Side Includes (SSI) to embed the results of a Common Gateway Interface (CGI) program into a static HTML document by using the "#exec cgi" directive. It sounds complicated but it's pretty simple. We only have to create two files and then make sure the web server is properly configured. Three simple steps! I'll guide you step by step:

1. Create Your HTML File

Your HTML file content would be just this (simply copy and paste into an HTML file on your server using a simple text editor):

<HTML>
<TITLE>Load a certain line from a .txt into html</TITLE>

Here's the content of line 546 in filename.txt:
<!--#exec cgi="/cgi-bin/perl-print-line.pl 546 filename.txt"-->

</HTML>

2. Create Your PERL Script File

The above won't work yet. It depends on a simple program to output your line number. It is perl-print-line.pl by Alvin Alexander, and you can simply create this file by copying and pasting the PERL code below into a text editor too (be sure to place it inside your cgi-bin directory):

#!/usr/bin/perl

# purpose: print a specific line from a text file
# usage:   perl-print-line.pl line-number input-file

# use perl argv to verify the number of command line arguments
@ARGV == 2 or die "Usage: print-specific-line.pl line-number input-file\n";
$desired_line_number = $ARGV[0];
$filename = $ARGV[1];

# use perl open function to open the file (or die trying)
open(FILE, $filename) or die "Could not read from $filename, program halting.";

# loop through the file with a perl while loop, stopping when you get to the desired record
$count = 1;
while (<FILE>)
{
  if ($count == $desired_line_number)
  {
    # print line, then get out of here
    print $_;
    close FILE;
    exit;
  }
  $count++;
}
close FILE;

print "Sorry, I think the line number you entered is greater than the number of lines in the file.\n";

3. Configure SSI On Your Web Server

Now, all of the above will not work unless your web server is first configured to process SSI. All popular web servers can still do it, but you must first make sure it is configured. Per the PerlScriptsJavaScripts.com site,

If your server is a unix/linux server, you can be pretty sure it supports SSI. SI requires the use of Apache Server software. Most unix/linux server use Apache. Some Windows server also use Apache, but most of them use IIS (Internet Information Server). The server software is not something you can change, and because it runs th entire server (including other people's web sites) your host will also refuse to change it for you. They may instead be kind enough to move your site to anther one of their servers which does use Apache.

Most people will tell you that to use SSI, you must name the page with a "shtml" extension. While that may be the case initially, you can set the server to also accept and parse regular "htm" or "html" files.

If you have a Windows server instead of a UNIX/Linux server, then you might find this article by Robert McMurray useful. Most importantly he states,

SSI is not enabled by default on IIS 6. To enable SSI on IIS 6, set the status for the Server Side Includes web service extension to Allowed in the Internet Information Services (IIS) Manager.

SSI is not installed by default on IIS 7. To install SSI on IIS 7, see the Server Side Include topic.

The cmd directive for #exec is disabled by default in IIS 5 and IIS 6. To enable the cmd directive, see Microsoft KB article 233969.

The cmd directive for #exec is now disabled for SSI files in IIS 7; you can only use the cgi directive.

Finally, if you are dealing with IIS 7.5 and your SSI directives inside your *.html files aren't being automatically preprocessed, try reading this post.

ShieldOfSalvation
  • 1,297
  • 16
  • 32