Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple substitution question 2

Status
Not open for further replies.

mstelmac

Technical User
Oct 22, 2003
53
US
Hell everyone. I am trying to parse a text file and remove all newline characters(\n) that are located within quotes. My data looks like this:

"id12345"<tab>"Fix code in sections:\n 1.2.7\n 1.2.8" \n
"id12346"<tab>"Fix code in sections:\n 1.2..5.4\n 1.2.3.2" \n
"id12347"<tab>"Fix code in sections:\n 1.g.8\n 1.y.r" \n

I don't want to remove the \n characters at the end of the record, I just want to remove all \n characters that are located inside the quotes. I thought I could just remove all \n characters and then put them back at the end of each record by doing this:

first command = s/\n/ /g;
second command = s/"id1/\n"id1/g;

But this method is ugly and depends on a match of the beginning of the record number. Isn't their a cleaner way to do it????????????????

Sorry guys but I'm really new to perl.

thanks,
Matt
 
Try this:
Code:
#!/usr/bin/perl -w
use strict;

while(<DATA>) {
  chomp;
  print "\n" if /^"id1/;
  print;
}
print "\n";
__DATA__
"id12345"<tab>"Fix code in sections:
1.2.7
1.2.8"
"id12346"<tab>"Fix code in sections:
1.2..5.4
1.2.3.2"
"id12347"<tab>"Fix code in sections:
1.g.8
1.y.r"


Trojan.
 
Code:
while (<FILE>) {
  $_=s/\n//g;
  print FILE2 "$_\n";
}

HTH
--Paul

Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
So it looks like there is no way to actually remove \n characters that are inside quotes?
 
What do you mean?
Does my code not do that for you?
Do you want the data in a file or in an array or something?
Tell us what you want and you might be lucky! ;-)


Trojan.
 
Trojan you code is great. This data is not in a file it is stored in a variable. See below:
############################################################
open OUTPUT, 'C:\Program Files\Rational\ClearQuest\Scripts\OSCAR\db_output.txt';

while ( <OUTPUT> )
{
$outfile = $outfile.$_;
}
close OUTPUT;

$outfile =~ s/\n/ /g;
$outfile =~ s/"id1/\n"id1/g;

$smtp->datasend("$outfile\n");
############################################################

My problem is that new line characters (\n) should only be in the file at the end of each record. However there are some newline characters in the middle of a record. I appologize for not being tech savy.

-matt
 
Try this:
Code:
#!/usr/bin/perl -w
use strict;

my $var = '"id12345"<tab>"Fix code in sections:
1.2.7
1.2.8"
"id12346"<tab>"Fix code in sections:
1.2..5.4
1.2.3.2"
"id12347"<tab>"Fix code in sections:
1.g.8
1.y.r"
';
$var =~ s/("[^\n]+)\n([^\n]+)\n([^"]+")/$1$2$3/g;
print $var;


Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top