Append to beginning and ending of each line? - sed

I'm working on a sed script that creates a HTML file. I want to append paragraph tags to the beginning and ending of each line but can't figure out how that would work.
Right now I have the following sed script:
1i\
<html>\
<head><title>sed generated html</title></head>\
<body>\
<pre>
$a\
</pre>\
</body>\
</html>
How would I be able to enclose every line in a <p> and </p> tag?
Example:
test.txt
This is a test file.
This is another line in the test file.
Output with the sed script:
<html>
<head><title>sed generated html</title></head>
<body>
<pre>
<p>This is a test file.</p>
<p>This is another line in the test file.</p>
</pre>
</body>
</html>

With sed:
Change your script to:
1i\
<html>\
<head><title>sed generated html</title></head>\
<body>\
<pre>
s/.*/<p>&<\/p>/
$a\
</pre>\
</body>\
</html>
Same as your command, but added s/.*/<p>&<\/p>/. Surround each line in file with <p> and </p>.
Use command sed -f script File
With awk:
cat script
BEGIN {
printf "<html>\n\
<head><title>sed generated html</title></head>\n\
<body>\n\
<pre>\n"
}
{print "<p>"$0"</p>"}
END {
printf "</pre>\n\
</body>\n\
</html>\n"
}
cat File
This is a test file.
This is another line in the test file.
Command:
awk -f script File
Sample:
AMD$ awk -f script File
<html>
<head><title>sed generated html</title></head>
<body>
<pre>
<p>This is a test file.</p>
<p>This is another line in the test file.</p>
</pre>
</body>
</html>

Related

perl lazy match external file with regular expression

I have an index.html file that contains a string like:
vendor.adsf34.bundle.js blah blah inline.1r34afer.bundle.js
I've built the following code (mac, iterm2):
perl -i -pe 's/vendor\..+?\.js/vendor.js/g;' index.html
perl -i -pe 's/inline\..+?\.js/inline.js/g;' index.html
However I end up with:
vendor.js
ie, it seems to be greedy matching where I need it to be lazy.
What I'm trying to do is shorten the js names, eg as follows:
vendor.js blah blah inline.js
Be great to get some pointers!
Something like this maybe?
look for a fixed start string (vendor. or inline.) 1
look for one-or-more non-space characters
look for a fixed end string (.js)
replace it with 1 + .js
$ echo "vendor.adsf34.bundle.js blah blah inline.1r34afer.bundle.js" | perl -pe 's/(vendor|inline)\.\S+\.js/\1.js/g'
vendor.js blah blah inline.js

"use HTML::TableExtract" breaking CGI script

I am new to CGI. I wrote a very complex module around perl, cgi, html and javascript. And it runs perfectly on command line. But I am not able to run it via browser. I went on debugging line by line from bottom of my script, only to find that the issue was around the HTML::TableExtract module itself. So to make it simple -
# perl -c test.cgi
test.cgi syntax OK
test.cgi
#!/usr/bin/perl
use strict;
use warnings;
use HTML::TableExtract;
print "Content-type: text/html\n\n";
print <<htmlcode;
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
htmlcode
This works perfectly on command line. But if I run it via Browser it just doesn't work. However, if I remove "use HTML::TableExtract" it works perfectly fine again - even in browser. The permissions are correctly set to 755.
Can someone please help me understand, what I am missing? And how can I fire it up from browser. How can I go about debugging this - My browser redirects me to page not found if I mention use HTML::TableExtract.
Note: Would like to point out one thing, this may be related to setting up some environment variable around HTML::TableExtract. When I first installed the module, there was an error which my hosting administrator helped resolve.
# ./test.cgi
Content-type: text/html
<html>
<head>
<title>CGI Perl Example</title>
</head>
<body>
<h1>CGI Perl Example</h1>
<p>CGI Perl Example</p>
</body>
</html>
Debugging perl CGI scripts
Add use CGI::Carp; to you script to make it report bugs to browser
[It produces http reply over STDOUT instead of default text over STDERR]
Most likely scripts executed from command line and from web server search for modules in different places e.g. due to different settings of PERL5LIB or PERLLIB environment variables.
WARNING
CGI::Carp may be a security threat in "production" version of cgi scripts.
It may provide crucial information to (potential) hackers.

Perl unable to write file on server

I have a file in.txt with some text.
I am using IIS 7 local server on Windows. and isapiModule is working fine for perl.
When I run this perl code via browser it is printing to browser just print line (not data) but not writing to file out.txt and just opening the empty file out.txt (i.e. unable to write via browser).
My wwwroot folder has full control access for all users.
html code:
<html>
<body>
<form name="form1" action="perl.pl" method="post"
enctype="multipart/form-data">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
perl.pl:
open(file,"<in.txt");
#x=<file>;
open(OUT,">out.txt");
print"print to browser...";
print OUT"#x\n";#for printing to file
close file;
close OUT;
What may be the reason and solution.
UPDATE: program is writing when manually run via command drive.
Have you tried something like this?
#!/usr/bin/perl -w
use strict;
my $in = 'in.txt';
open my $file, '<', $in or die "Can't open $in\n";
my $outfile = 'out.txt';
open my $outprint, '>', $outfile or die "Can't write to $outfile\n";
print "print to browser…\n";
while (<$file>) {
chomp;
print $outprint "$_\n";
}

Unable to get this simple Perl CGI program running

Can someone please explain to me the very basics of getting perl to work on a server. Do I need a module on the server? If so where does it go? What do I name my files and where do they go?
From my understanding you need a module and it goes in the cgi-bin. I can't get a clear answer whether I name the file .pl or .cgi and when I put it in the cgi-bin I am getting a server error. I also have my permissions set to 777, so that shouldn't be the problem.
Please help! I just want to understand the how to get the very basic program working such as the one below. Thanks in advance!
#!/usr/bin/perl
require("cgi-lib.pl");
print &PrintHeader;
print "<html>";
print "<head><title>Hello world!</title></head>";
print "<body>";
print "<p>Hello world!</p>";
print "</body>";
print "</html>";
The latest version of cgi-lib.pl is dated 1999 and is very out of date. I suggest you use the CGI library instead, which is almost bound to be installed already on your server and is kept up to date (most recently on August 16 2012)
Your program should look like this:
#!/usr/bin/perl --
use strict;
use warnings;
use CGI ':standard';
print header;
print <<END;
<html>
<head><title>Hello world!</title></head>
<body>
<p>Hello world!</p>
</body>
</html>
END
Also note that you can run the program from the command line to see if it compiles and what output it generates. Once you have it working there you can move it to the server
The problem you are facing is you probably edited the source file on a Windows machine, which inserts a CR character before each linefeed. Make sure your code does not include any CRs or change the first line to:
#!/usr/bin/perl --
(that's two dashes and a space at the end of the line)
I concur with Dave Cross' comment, you need to tell your school that they are teaching you incorrectly. We in the Perl land are trying to get people to stop using the CGI module, and you are using its predecessor!
Here is a hello world app in a modern Perl framework, Mojolicious:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => 'hello';
app->start;
__DATA__
## hello.html.ep
<html>
<head><title>Hello world!</title></head>
<body>
<p>Hello world!</p>
</body>
</html>
which you place into a file, (lets say test.pl). Install Mojolicious by running:
curl get.mojolicio.us | sh
and then start your app by running
perl test.pl daemon
Now you can visit http://localhost:3000 in your browser to see the result, no Apache, no cgi-bin needed!
A more fun example takes an "argument" with a default:
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/:name' => { name => 'world' } => 'hello';
app->start;
__DATA__
## hello.html.ep
<html>
<head><title>Hello <%= $name %>!</title></head>
<body>
<p>Hello <%= $name %>!</p>
</body>
</html>
Run this and try visiting http://localhost:3000/SilverNightaFall and see what it does!
This process of interpolating dynamic values into your HTML is known as templating, and it is much preferred these days (vs generating your entire HTML on each request).

Perl script is not executing in HTML form

I have a guestbook.htm file in the directory named as Chrome (/home/chankey/Desktop/Chrome/guestbook.htm) whose content is given below
<html>
<head>
<title>Guestbook</title>
</head>
<body>
<form action="/home/chankey/Desktop/Chrome/guestbook.pl" method="get">
<table>
<tr><td>Name</td><td><input name="name" type="text" value=""></td></tr>
<tr><td>E-Mail</td><td><input name="email" type="text" value=""></td></tr>
<tr><td>Location</td><td><input name="loc" type="text" value=""></td></tr>
<tr><td>Comments</td><td>
<TEXTAREA name="comments" rows="10" cols="32"></TEXTAREA></td></tr>
</table><br><br>
<input type="submit" value="Add Entry">
</form>
</body>
</html>
In the same Chrome directory I have one file "guestbook.pl" whose content is
#!/usr/bin/perl
my $query_string = "";
#Get the input
if ($ENV{REQUEST_METHOD} eq 'POST') {
read(STDIN, $query_string, $ENV{CONTENT_LENGTH});
} else {
$query_string = $ENV{QUERY_STRING};
}
##### We will remove this
print "Content-Type: text/html\n\n";
print "Query String is \n<br> $query_string";
##### We will remove this
When I am executing the guestbook.htm file there appears a form, when I fill the data and click on "Add Entry" button a new page opens where the complete script appears.
i.e. the script "guestbook.pl" is not executing. May I know the reason behind this? Why the script is not executing? (I have already given executing permission to this file).
In the httpd.conf file I have added
AddHandler cgi-script cgi pl
<Directory /home/chankey/Desktop/Chrome/>
Options +ExecCGI
</Directory>
Still it is not executing. Let me know the reason.
When you access a file locally (over the file:// type URL in your browser), it's not running from a web server, so:
There is no CGI environment
The httpd.conf/.htaccess files have no effect
A few ways to deal with this:
Create a folder named public_html in your home. Your web server likely has a setting to map http://localhost/~chankey/ to /home/chankey/public_html. (On MacOSX, the preferred name is Sites, instead, I believe.) On an SELinux system, you'll have to specifically grant permission for Apache to use this method.
For the specific case of Perl scripts using the standard CGI package, you can also run them manually from a terminal shell, and redirect their output to a temporary file (e.g. >/tmp/output.html), which you can then access
Migrate your development workspace into your web server's own directory structure, typically /var/www/html/…
PS / unrelated: I strongly recommend that, if you plan to put this on the Internet, you should probably use CGI; use strict; and have the tainting mode enabled #!/usr/bin/perl -WT …