Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Removing linefeeds from part of a file

by Desdinova (Friar)
on Jul 12, 2001 at 01:34 UTC ( #95863=perlquestion: print w/replies, xml ) Need Help??
Desdinova has asked for the wisdom of the Perl Monks concerning the following question:

I am somewhat stumped as to how to approach this problem.
Basiclly I have an ASCII text file that looks like this:
POSTNUM 12345 ENDPOSTNUM DESCRIPTION item 1 Item 2 item 3 long line ENDDESCRIPTION CONTACT Bill Smith ENDCONTACT
What I need to do is reformat just the DESCRIPTION section to remove the linefeeds in it so that it becomes:
POSTNUM 12345 ENDPOSTNUM DESCRIPTION item 1 Item 2 item 3 long line ENDDESCRIPTION CONTACT Bill Smith ENDCONTACT
Note that this shows a single record, there are close to 50 records like this in each file.
I have no idea how to approach this. Anyone who can point me a general direction for this would be a great help.

Replies are listed 'Best First'.
Re: Removing linefeeds from part of a file
by Abigail (Deacon) on Jul 12, 2001 at 02:33 UTC
    { local $\ = "\n"; while (<>) { chomp; if (/^ENDDESCRIPTION$/) { print "\n"; $\ = "\n"; } print; $\ = "" if /^DESCRIPTION$/; } }

    -- Abigail

Re: Removing linefeeds from part of a file
by John M. Dlugosz (Monsignor) on Jul 12, 2001 at 01:45 UTC
    something like (just off my head; prob. needs work):
    while (<>) { chomp if (my $num= /^DESCRIPTION$/ .. /^ENDDESCRIPTION/); next if $num==0 || $num =~ /E/; # skip endpoints print; }
    There was a thread on that a short time ago. Look up Range Operator in Perlop, and do a super search on flip-flop and variations for the past couple weeks.

    —John

Re: Removing linefeeds from part of a file
by thpfft (Chaplain) on Jul 12, 2001 at 04:31 UTC

    Methods that use <> will be most efficient but , for the sake of MTOW, here's another. To do this properly with a regex would either require you to unroll the loop or use a proper parser, but this is the sloppy version. It's more expensive, especially for a big file, but it'll work well enough and i'd feel happier not having to rely on a state toggle.

    $text =~ s{DESCRIPTION(.*?)ENDDESCRIPTION} {"DESCRIPTION\n" . join(' ', split("\n",($1))) . "\nENDDESCR +IPTION"}ges;
Re: Removing linefeeds from part of a file
by particle (Vicar) on Jul 12, 2001 at 02:27 UTC
    sorry, this code is untested, but it may just work~

    #!/usr/bin/perl -w use strict; my $do_i_chomp?; # off by default while(<>){ # read from STDIN $do_i_chomp? && chomp; # chomp if you should m/^DESCRIPTION$/ && do{ !$do_i_chomp?; # flip chomping on print; # print the DESCRIPTION line }; print; # print the line m/^ENDDESCRIPTION$/ && do{ print "\n$_"; # print newline, then the ENDDESCRIPTION +line !$do_i_chomp?; # flip chomping off }; }
    `perl script.pl <infile >outfile`

    ~Particle

Re: Removing linefeeds from part of a file
by lemming (Priest) on Jul 12, 2001 at 01:47 UTC
    open file check each line if it's eq to "DESCRIPTION" turn on line chomping if it's eq to "ENDDESCRIPTION" turn off line chomping print each line close file
    Note you'll need a seperator for inside description

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://95863]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2018-07-19 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (398 votes). Check out past polls.

    Notices?