How to print text from a file in a knitr doc? - knitr

I wish to read in a text file and print it verbatim in a knitr doc. I got some code to read a text file into a string:
filename = 'foo.txt'
text = readChar(filename, file.info(filename)$size)
However, my every attempt to print out the text results in ugly craziness. Using print() does not wrap the lines. It leaves "## [1]" all over the output. Using results='asis' doesn't do what I want. Trying \verbatim and \spverbatim doesn't do the right thing.
What is the easy way out here?

I got this to work with
\begin{spverbatim}
\Sexpr{text}
\end{spverbatim}
not in a knitr block.

Related

Reading a text file in PowerShell after of a marker

I'm just wondering if it's possible to read the content of text file with specific index?
What I mean is like this, for example:
I have text file like this, 'test1.txt'
12345678900 ## ## readthistext
54321123440 ## ## hellothistext
I just want to read the content of text file after of the hashtag.
To read the text after the # characters you must read the file content up to the # characters first. Also, in PowerShell you normally read files either line by line (via Get-Content) or completely (via Get-Content -Raw). You can discard those parts of the read content that don't interest you, though. For instance:
(Get-Content 'C:\input.txt') -replace '^.*#'
The above will read the file C:\input.txt and for each line remove the text from the beginning of the line up to the last # character.

Read from csv file and write output to a file

I am new to Perl and would like your help on following scenario, can you please help on this subject.
I have a CSV files with following information and I am trying to prepare a key-value pair from CSV file. Can you please help me with below scenario.
Line 1: List,ID
Line 2: 1,2,3
Line 3: 4,5,6
Line 4: List,Name
Line 5: Tom, Peter, Joe
Line 6: Jim, Harry, Tim
I need to format the above CSV file to get an output in a new file like below:
Line 1: ID:1,2,3 4,5,6
Line 2: Name:Tom,Peter,Joe Jim, Harry, Tim
Can you please direct me on how I can use Perl functions for this scenario.
You're in luck, this is extremely easy in Perl.
There's a great library called Text::CSV which is available on CPAN, docs are here: https://metacpan.org/pod/Text::CSV
The synopsis at the top of the page gives a really good example which should let you do what you want with minor modifications.
I don't think the issue here is the CSV format so much as the fact that you have different lists broken up with header lines. I haven't tried this code yet, but I think you want something like the following:
while (<>) { # Loop over stdin one line at a time
chomp; # Strip off trailing newline
my ($listToken, $listName) = split(',');
next unless $listToken; # Skip over blank lines
if ($listToken =~ /^List/) { # This is a header row
print "\n$listName: "; # End previous list, start new one
} else { # The current list continues
print "$_ "; # Append the entire row to the output
}
}
print "\n"; # Terminate the last line
Note that this file format is a little dubious, as there is no way to have a data row where the first value is the literal "List". However, I'm assuming that either you have no choice in file format or you know that List is not a legal value.
(Note - I fixed a mistake where I used $rest as a variable; that was caused by my renaming them as I went along and missing one)

How can I save Perl/Expect output that contains mixed ascii content?

I have a perl script that uses the expect library to login to a remote system. I'm getting the final output of the interaction with the before method:
$exp->before();
I'm saving this to a text file. When I use cat on the file it outputs fine in the terminal, but when I open the text file in an editor or try to process it the formatting is bizarre:
[H[2J[1;19HCIRCULATION ACTIVITY by TERMINAL (Nov 6,14)[11;1H
Is there a better way to save the output?
When I run enca it's identified as:
7bit ASCII characters
Surrounded by/intermixed with non-text data
you can remove none ascii chars.
$str1 =~ s/[^[:ascii:]]//g;
print "$str1\n";
I was able to remove the ANSI escape codes from my output by using the Text::ANSI::Util library's ta_strip() function:
my $ansi_string = $exp->before();
my $clean_string = ta_strip($ansi_string);

Perl write to file returns huge weird stacktrace

I have the following problem: when I try to save the file that contains a semicolon in the name it returns a huge and weird stacktrace of the characters on the page. I've tried to escape, to trim and to replace those semicolons, but the result is still the same. I use the following regex:
$value =~ s/([^a-zA-Z0-9_\-.]|;)/uc sprintf("%%%02x",ord($1))/eg;
(I've even added the |; part separately..)
So, when I open the file to write and call the print function it returns lots of weird stuff, like that:
PK!}�3y�[Content_Types].xml ���/�h9\�?�0���cz��:� �s_����o���>�T�� (it is a huge one, this is just a part of it).
Is there any way I could avoid this?
Thank you in advance!
EDIT:
Just interested - what is the PK responsible of in this string? I mean I can understand that those chars are just contents of the file, but what is PK ? And why does it show the content type?
EDIT 2.0:
I'm uploading the .docx file - when the name doesn't contain the semicolon it works all fine. This is the code for the file saving:
open (QSTR,">", "$dest_file") or die "can't open output file: $qstring_file";
print QSTR $value;
close (QSTR);
EDIT 3.0
This is a .cgi script, that is called after posting some data to the server. It has to save some info about the uploading file to a temp file (name, contents, size) in the manner of key-value pairs. So any file that contains the semicolon causes this error.
EDIT 4.0
Found the cause:
The CGI param function while uploading the params counts semicolon as the delimiter! Is there any way to escape it in the file header?
The PK in file header it means it is compressed ZIP like file, like docx.
One guess: The ; is not valid character in filename at the destination?
Your regexp is not good: (the dot alone is applicable to any character...)
$value =~ s/([^a-zA-Z0-9_\-.]|;)/uc sprintf("%%%02x",ord($1))/eg;
Try this:
#replace evey non valid char to underscore
$value =~ s/([^a-zA-Z0-9_\-\.\;])/_/g;

Print freeform text in a Perl function?

I have been getting a very odd error when trying to print freeform text in a subroutine in Perl. Below is the code I am calling
print OUTFILE <<"HEADER";
The freeform text would go here
HEADER
The odd thing is that this only works in the main of my function. As soon as I place it in a function call, I get this error:
Can't find string terminator "HEADER" anywhere before EOF
Meaning it can't find the HEADER, even though it is there. Can you not use freeform text within a function (subroutine)?
Make sure that there is no space/tab/indentation before ending string identifier, that is HEADER. Your code should look like this:
function someFunc(){
print OUTFILE <<"HEADER";
The freeform text would go here
HEADER
}
Notice that there is no space/tab/indentation before HEADER there. It should start from first character of its line.
Check this tutorial out for more information:
Perl Here-Doc Tutorial
Quoting:
The important rule to remember is that
you finish a here-doc using the same
word you started, and it must be by
itself on the line