in reply to Delete till end of line to another string

The trick here is to use the non-eager version of * (code not tested):

s/ < # match '<' .*? # match anything, but as short as possible (\{|$) # match either '{' or end of line //x
BTW, this also removes the '{'.

-- 
Ronald Fischer <ynnor@mm.st>

Replies are listed 'Best First'.
Re^2: Delete till end of line to another string
by Anonymous Monk on Aug 18, 2009 at 11:32 UTC
    The format of the file contains Ascii characters. When I try to print the line,it only prints till EVITE LA ENFERMEDAD PERIODONTAL<. Please tell me any way we can encode the file
    #!/usr/bin/perl while(<DATA>){ $_ =~ s/<.*?(\{|$)//x; print $_; } __DATA__ EVITE LA ENFERMEDAD PERIODONTAL< AS {IT}CTIQUE LA HIGIENE DENTAL PARA CUIDAR SUS ENCà Cuando hace casi d +os años visité a mi dentista para hacerme un chequeo dental , me encontré ante un problema que podía terminar con mi dent +adura. Siempre tuve unos dientes sanos y jamás se me ocurrió pensar que pod +ía perder
      The format of the file contains Ascii characters.
      ??? I think you mean "contains non-ascii characters".
      it only prints till EVITE LA ENFERMEDAD PERIODONTAL<
      Are you sure that it also prints the '<' less sign? With the code you have posted, the '<' should not be printed. At least it does not for me:
      $ perl -lwe 'my $_="EVITE LA ENFERMEDAD PERIODONTAL< AS"; s/<.*?(\{ +|$)//; pr int' EVITE LA ENFERMEDAD PERIODONTAL
      Please tell me any way we can encode the file
      I'm not sure what you mean by "encode the file". Maybe you find what you need in perlio, where the encoding layer is described.

      -- 
      Ronald Fischer <ynnor@mm.st>
        #!/usr/bin/perl use strict; use warnings; my $tag; my $output; my $fh; my $deletestrings; #open DATA,"$ARGV[0]"; while (<DATA>) { chomp; s/[\cA-\cZ]//g; # To remove control characters #To delete the lines which has {IT} and any lines between {IT} and {SO +URCETAG} if (/\{IT\}/ .. /^\{SOURCETAG\}/) { unless (/^\{(IT|SOURCETAG)\}/) { $deletestrings = $_; $_ = '' if index( $_, "$deletestrings" ) >= 0; } } s/[\\|<]$//g; s/^[\\|<]//g; s/<//g; s/^\s+//g; s/\s+$//g; if(/^{(.*)}$/) { # match {METATAG} line $fh = output($output, $tag, $fh); $output = ""; $tag = $1; } else { # not a {TAG} line next unless($tag); next if(/^\s*$/); s/\\//g; $output .= ($output) ? " $_" : "<$tag>$_"; } } $fh = output($output, $tag, $fh); if($fh) { print $fh "</ROOT>\n"; close($fh); } exit(0); sub output { my ($output, $tag, $fh) = @_; if($output) { if($output =~ m/<SOURCETAG>(.*)/) { if($fh) { print $fh "</ROOT>\n"; close($fh); } open($fh, '>', "$1.xml") or die "$1.xml: $!"; print $fh "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<RO +OT>\n"; } print $fh "$output</$tag>\n"; } return($fh); } __DATA__ ^C^D^V^V^A exit 0543 961017 N S 9702050900 00000000^B{IT} N {SOURCETAG} 9702050900 {ACCESSION} 000000 {DATE} 961017 {TDATE} Thursday, October 17, 1996 {PUBLICATION} EXITO {EDITION} Exito {SECTION} SALUD FAMILIAR {PAGE} 22 {SOURCE} Por Nahyr Acosta. Colaboradora de ¡Exito! {COLUMN} Nuestro hijos. {HEADLINE} EVITE LA ENFERMEDAD PERIODONTAL< AS {LEAD}CTIQUE LA HIGIENE DENTAL PARA CUIDAR SUS ENCà Cuando hace casi + dos años visité a mi dentista para hacerme un chequeo dental , me encontré ante un problema que podía terminar con mi dent +adura. Siempre tuve unos dientes sanos y jamás se me ocurrió pensar que pod +ía perder mis piezas si no tomaba acción inmediata. Me puse en manos del denti +sta y empezamos a trabajar en mis encías.<
        This code creates a xml output. But for {LEAD} it doesnot creates the proper tag. For the file when I pass the input file as an command line argument, The output is seen as below.
        <SECTION>SALUD FAMILIAR</SECTION> <PAGE>22</PAGE> <SOURCE>Por Nahyr Acosta. Colaboradora de ¡Exito!</SOURCE> <COLUMN>Nuestro hijos.</COLUMN> + AS</HEADLINE> <LEAD>Cuando hace casi dos años visité a mi dentista para hacerme un + chequeo dental , me encontré ante un problema que podía terminar c +on mi dentadura. Siempre tuve unos dientes sanos y jamás se me ocurr +ió pensar que podía perder mis piezas si no tomaba acción inmedia +ta. Me puse en manos del dentista y empezamos a trabajar en mis encà +­as. La descomposición de la comida, la saliva y las bacterias que h +ay en la boca producen una sustancia llamada placa, que se adhiere a +la superficie de los dientes y, si no se limpia diariamente, irrita l +as encías y se endurecen, formando el sarro. Esta sustancia, a su ve +z, se adhiere cada vez más, inflamando las encías y produciendo bol +sas que se llenan con colonias de bacterias. Estas son las que con el + tiempo, causan la enfermedad periodontal. Las bacterias también pro +ducen una sustancia llamada toxina, la cual destruye el hueso.</LEAD>
        For {LEAD} it creates the tag but not for HEADLINE