1

I'm complete newby to perl and I hope you can help me with this line of code. The issue is related to this one here, but it doesn't quite answer my question. I tried looking for it, but I just get more confused.

I have a txt input (batch) that I want to have a filename printed in the first line, but wrapped in a specific text. I am converting these files later into html and so I would like the .name to have "<div class="head">" printed before and "</div>" afterwards.

Here is the code I have and it works to print the name:

perl -i -pe 'BEGIN{undef $/;} s/^/$ARGV\n/' `find . -name '*.txt'`

I run this by first navigating to the directory where all the files are. example of filename: 2016-05-20_18.32.08.txt the files are plane text poetry and in the output i get: ./2016-05-20_18.32.08.txt in the first line.

I tried something like this:

perl -i -pe 'BEGIN{undef $/;} s/^/$ARGV\n/' `find . -name ‘“<div class="head”>”’*.txt’”</div>”’

but of course it doesn't work. it just give me a > I need to add the arguments in this part s/^/$ARGV\n/' but i already have troubles defining it.

Can you help pls?

In addition, the filename prints with ./ in the beginning, is there a simple way to exclude that?

kata
  • 13
  • 4
  • Please give examples of how your input, output looks like. and your current scripts output – rajashekar Nov 29 '18 at 08:45
  • Hi @RajashekarReddyM i've added the input output example in the question. hope thats what u meant? – kata Nov 29 '18 at 08:55
  • So you have a file some_date_time.txt , do you want a html file with the content of the text file wrapped in
    html tags? Or do you want the file names? What is you ideal output?
    – rajashekar Nov 29 '18 at 09:00
  • correct. i want the file name to be wrapped in
    and the content of the file wrapped in another
    . I thought i could do this dirty by find and replace later, because in simple txt to html conversion i get p class tags for each line of text. but this solution would be much better, i agree.
    – kata Nov 29 '18 at 09:18

2 Answers2

0

I'm sure there is a more elegant way of doing it, but something like this will work

#!/usr/bin/perl

undef $/;

for (@ARGV){
        open($fh,$_);
        $content=<$fh>;
        close($fh);
        open($fh,">$_");
        print $fh "<div class=\"head\">$_</head>\n$content";
        close($fh)
}
Essex Boy
  • 7,565
  • 2
  • 21
  • 24
0
perl -i -pe 'BEGIN{undef $/;} s/^/<div class=head> $ARGV <\/div>\n<div class=poem>\n/; s/$/\n<\/div>/' `find . -name '*.txt'`

This should work. But if you are new to perl, I suggest you try working with scripts rather than one-liners.

The -i flag will edit the file inplace. so if you want a html file, remove -i and redirect to another .html file.

rajashekar
  • 3,460
  • 11
  • 27
  • Great thanks!! But in this format it gave an error so i found that the bug was a missing `.` after find. So, the correct code then is: `perl -i -pe 'BEGIN{undef $/;} s/^/
    $ARGV <\/div>\n
    \n/; s/$/\n<\/div>/' `find . -name '*.txt'` `
    – kata Nov 29 '18 at 10:02