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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitution question 2

Status
Not open for further replies.

mstelmac

Technical User
Oct 22, 2003
53
0
0
US
Hello everyone. I have been reading about string substitution but do not understand how to find my pattern. My pattern only exists in quotes like this:

$variable = "The Dog Can Run"<tab>"The Dog Can Swim"<carriage return>

I need to remove any new line, tab, carriage return, basically any control character except spaces that exists within quotes only and replace them with something like 3 dots.

$variable =~ s/?????/..../g;

Thanks :)
-Matt
 
Hi Matt

How about
Code:
$variable =~ s/[^ \w]/.../g;

\w means a word character

So, substitute anything that's not a space or a non-word chanracter with the three dots.

This is in "man perlre" if you want to see more.

You might need to fiddle if you don't want to sub things like a dash. If so, make it s/[^ \w-]/.../g for example

~ Michael
 
Code:
my $variable = "The Dog Can Run\"\t\"The Dog Can Swim\"\n\"";
$variable =~ s/"[\r\n\t\f]+"/.../g;

print $variable."\n";

works for me!

Jeb
\0
 
Hmm I tried both of these methods and I still see new paragraph markers in my file within quotes. I opened the file in MS word and still see these characters that look like a backwards P. If these exist with quotes then I can not import into excel.

thanks,
Matt
 
Hmm, not sure how that's happening, Matt

Can you post a bit more info, a program that is showing this problem and some sample output.

The backwards P in word is just a newline but i can't see how one of them would be left with the code i gave you above
 
Michael,
My perl program is run over a large text file. It's purpose is to remove any character founf within quotes that may mess up getting it into excel. For some reason excel does not like these characters in their cells even though that are wrapped in quotes.

So I tried this line ($outfile =~ s/[^ \w]/.../g; and $outfile =~ s/"[\r\n\t\f]+"/.../g;) and still see the newline characters within my quotes in the text file.

Below is an example of the text I am trying to run this script over. You can see that there is still new line characters within quotes:

"RECORD00003208" "Implemented" "Barry" "" "Kindly" "" "KYY" "" "N/A" "" "N/A" "" "N/A" "" "2006-03-29 00:00:00" "Gene Loopy" "" "5.1 SR9" "Bob, John, Larry" "Software" "Tailorization" "" "" "0" "" "" "" "" "" "Need the ability to turn autoboot on and off. This functionality does not exist in car." "0" "" "" "5" "Bob, John, Larry" "N" "" "0" "/tactical/autoboot.csh" "0" "0" "" "0" "" "" "

===== State: Submitted by:John at 3/22/2006 1:56:40 PM =====



Disclose at meeting #2



===== State: Submitted by:John at 3/21/2006 2:03:50 PM =====



CCB Approved

" "" "Bob" "blah@blah.com" "IBM" "Ken P" "856-789-2585" "V01.00" "" "" "D" "" "" "Allow boat to run /system/autoboot.csh in sudoers file

Push autoboot.csh onto all car nodes except NGPs" "POPS" "Riop" "" "ALL" "0" "2006-02-22 08:58:00" "0" "" "2006-04-14 00:00:00" "1 login to any node

2 sudo into user car

3 run /system/autoboot.csh and verify that the AUTOBOOT_OFF flag is created or removed

4 kindly put the system back to the way it was" "King" "4" "Add autoboot.csh script to tactical nodes" "0" "" "" "" "Low" "2006-04-17 06:36:24
 
Your sample does not have any newlines outside of the quotes. If that is always true then the following works when your sample is placed in input.txt

Is there any special significance to the field separator of four spaces?

Code:
#!/usr/bin/perl -w
use strict;
# my INP;

my @lines;
my $file;
open INP, "<input.txt" or die "error opening input.txt: $!";
@lines = <INP>;
$file = join("", @lines);
$file =~ s/[\r\n\t\f]+/.../mg;
print $file;
exit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top