Operating System - HP-UX
1821245 Members
2926 Online
109632 Solutions
New Discussion юеВ

Text to Paragraph conversion by perl

 
SOLVED
Go to solution
Dodo_5
Frequent Advisor

Text to Paragraph conversion by perl

i have attached a text file.
a)i have to print output as paragraph.actually when it finds an "\n" in the text file it should start writing(output)in next paragraph.and
b) before starting every paragraph it should
leave a fixed amount of space(same as "\t"),then it should start writing.
c)every line in the output file should not exceed a fixed position in the screen.
please write a script in perl to do this..its urgent.waitin for reply.

the example of output is given below:

I am a software professional.I am working in a software company.
The company is good.I have to work there fo
r for 8 hours.
Great job.My boss is Mr.xxx.Very good perso
n.......


look at the output...
**every line ending border is fixed.when it exceeds than previous one it come in next line
(like for in 2nd para and person in 3rd para)
**every paragraph starts with a definite amount of of space.
18 REPLIES 18
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

the actual output should be like this ..
(see attached file)
James R. Ferguson
Acclaimed Contributor

Re: Text to Paragraph conversion by perl

Hi:

> I am a software professional. I am working in a software company

> please write a script in perl to do this..its urgent

You have got to be kidding! No professional would ask someone else to do their work for them with the expectation of making their boss believe it was their's!

Buy some books; study them; and *try* yourself. Then, if you're stuck, post what you have tried and ask for help. If you can't or won't expend the effort, I urge you to find a new line of work.

...JRF...
Peter Godron
Honored Contributor

Re: Text to Paragraph conversion by perl

Dodo,
please have a look at the unix fmt command (man fmt) to get the fixed width.
The perl format function can be used to act upon the various controls (\n) etc.

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

actually i have done this one(attached file)
but main problem is can't recognise the \n every time...only first time it does.
actually i can't print the every element of the text file individually.how to do that?
pls help...
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

actually i have done this one(attached file)
but main problem is can't recognise the \n every time...only first time it does.
actually i can't print the every element of the text file individually.how to do that?
pls help...
Peter Godron
Honored Contributor

Re: Text to Paragraph conversion by perl

Dodo,
sorry to see that my previous answer didn't really help (3 points).

Perhaps this will help:

#!/usr/bin/perl
$string="I\nam a software\n professional.\nI am";
$~ = "OUTLINE";
write;
format OUTLINE =
@*
$string
.

Will return:
I
am a software
professional.
I am


As per my first answer look at perl format command, as above and the unix fmt command.
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

hi peter,
thanks for helping.but actually i want to take a text file as input (not a string).then have to print in next paragraph whenever it gets a "\n" in that text file while reading it.
and the line width of output file should be fixed.
please help a little more if u can..i expect a little more from u Expert People
Peter Godron
Honored Contributor

Re: Text to Paragraph conversion by perl

Dodo,
why don't you take my script, wrap it in some code to read through your file (line-by-line) to assign the read line to $string and then process as normal.

The output of this should be redirected to a file and then fmt with the -w option would reformat to the desired width.
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

that script give a compilation error.i think something prob in format option
Peter Godron
Honored Contributor

Re: Text to Paragraph conversion by perl

Dodo,
file attached

Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

your provided script is:

#!/usr/bin/perl
$string="I\nam a software\n professional.\nI am";
$~ = "OUTLINE";
write;
format OUTLINE =
@*
$string
.


after running the script output is:

sh monitor.sh
monitor.sh: line 2: =I\nam a software\n professional.\nI am: command not found
monitor.sh: line 3: $~: command not found
usage: write user [tty]
monitor.sh: line 5: format: command not found
monitor.sh: line 6: @*: command not found
monitor.sh: line 8: .: filename argument required
.: usage: . filename [arguments]

so what should i do?
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

sorry i have done a great mistake.i have edited the file in sh (shell) file.now i have done it in .pl file.now its simply workin.
but can you help me running this script for a input text file..and how to format output page in fixed width.actually i dont know how to use the format command.
pls help me if you can..waitin
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

sorry peter..i should give you a little more point..but due to my error i gave you only 3.so sorry.
Peter Godron
Honored Contributor

Re: Text to Paragraph conversion by perl

Dodo,
we are so called 'experts' because we were willing to investigate and learn from suggestions, rather than just ask for finished solutions . I have provided all the building blocks you need to do the job.I feel more and more like James describes.

Use the internet for searches on perl scripts to read files etc. use "man frmt" for the unix format part.
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

ok..many many thanks for suggestion and helping..
Hein van den Heuvel
Honored Contributor
Solution

Re: Text to Paragraph conversion by perl

Dodo,
I agree 100% with the sentimen
ts expressed by JRF and Peter. Th
e only way to learn this, it to R
EAD and TRY.
However I must give you credit
for having provided input example
s and output examples, and the st
art of a solution.
What is missing is details in
the spec: line length, size of ta
b, real-tab or spaces? The sample
output does not seems to match t
he requirement either.
Below you'll find a possible s
olution. You may want to think of
adding a newline between paragra
phs, not breaking up words, and s
tarting words in a new line witho
ut a space.

And uh... how about signing wi
th a real name, preferably your o
wn?

Cheers,
Hein.

------------------- format.pl ----------

use warnings;
use strict;
my $line;
my $tab = " "; # Paragraph indent
my $max = 40; # Maximum line size
while (<>) { # read all input
s/^/$tab/; # Start line with tab
s/\\n/\n$tab/g; # Replace EOL marker with newline + tab
foreach $line (split /\n/) { # grab lines, which might be too long
while (length($line) > $max) {
print substr($line,0,$max) . "\n";
$line = substr($line,$max);
} # next chunk
print "$line\n"; # Final chunk of line
} # next line
} # next input

-------------------- usage sample -----
#perl format.pl \temp\tmp.txt
I am a software professional.I a
m working in a software company.
the company is good.i have to wo
rk there for 8 hours.
Great job.My boss is Mr.xxx.Very
good person
#
James R. Ferguson
Acclaimed Contributor

Re: Text to Paragraph conversion by perl

Hi (again):

OK, under the assumption that you are serious, and in the spirit of this Forum, here's another solution. Consider this:

/*file_begins*/

Perl has a read-made soultion for you if you are willing to use it instead of a pure shell script. The Perl module you need is called Text::Wrap and it's included in you standard Perl distribution. In fact, you can use this very file to call it and show it reformatting. Let's reformat this into text that spans no more than 30-columns
and has it paragraphs indented four (4) spaces.

/*file_ends*/

Now, use this passing the name of the file with the above contents:

# perl -MText::Wrap -e '@lines=<>;$Text::Wrap::columns=30;print wrap(" ","",@lines)' file

Perl has a read-made
soultion for you if you are
willing to use it
instead of a pure shell
script. The Perl module you
need is called
Text::Wrap and it's included
in you standard Perl
distribution. In fact,
you can use this very file to
call it and show it
reformatting. Let's
reformat this into text that
spans no more than 30-columns
and has it paragraphs
indented four (4) spaces.

Regards!

...JRF...
Dodo_5
Frequent Advisor

Re: Text to Paragraph conversion by perl

thanks to all the people for ur replies and kind hearted suggestions and helpings....
that's why i always looking forward to you expert people.
thanks again..