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!

How can I do a replace inside of " " useing a regular expresion?

Status
Not open for further replies.

networkwoes

Technical User
Aug 17, 2001
13
0
0
US
I have a file that is a comma separated file with “ “ around each field. One of the fields has a comma inside of the field and I need to replace it with a space. Below is a sample entry before I have done anything to it, and the second is what I want the entry to look like afterwards. They are exactly the same except for the user name the first is (…,"MOORE, E. CELESTE",…) and I need to change it to (...,"MOORE E. CELESTE",…). I believe that I could use a regular expression for this but I am not familiar with them. Could someone show me how I could do a search and replace inside of quotation marks?

DATA," 1328221","DIS","SS7WTL","DISC",,"N","N","N","56KB DDS","001","SNFC.PP","DNVR.TLP","WZ505426","RCC1","CL06","20020415","20020319","20000515","20020319","20020321","ICOM","E7C","MOORE, E. CELESTE","20020321","ECM","LNA",,-,-

DATA," 1328221","DIS","SS7WTL","DISC",,"N","N","N","56KB DDS","001","SNFC.PP","DNVR.TLP","WZ505426","RCC1","CL06","20020415","20020319","20000515","20020319","20020321","ICOM","E7C","MOORE E. CELESTE","20020321","ECM","LNA",,-,-


Thanks for the help,
Eric
 
Once you get your data out of the file you can use this regex on the name:
$string = s/,//;
 
Oooh OOOh OOOOOOOhhhh! Pick Me Pick ME!
Page 31 of the Perl CookBook
formula 1.15: Parsing Comma-Separated Data

#===== Code =======

use Text::parseWords;

sub parse_csv { return quoteword(",",0, $_[0]); }

# now, use the magic wand you just created:

$line = q<ZYZZY,&quot;&quot;,&quot;O'Reilly, INC&quot;,&quot;Wall, Larry&quot;,&quot;a \&quot;glug\&quot; bit,&quot;,5,&quot;Error, Core Dumped&quot;>;

# q<> is just a fancy quote so they didn't
# have to backslash everything.
# So $line is my test data.

@fields = parse_csv($line);

# Voila! @fields contains your data!

++$x and print &quot;$x : $_\n&quot; for @fields;

# Yea baby! Someone else did all the work!

#======== END Code ======

Hope this works for ya.

--Jim


 
Oh, yea - I forgot the second part.
Once you've successfully put the elements of the record into the array, you could run through the array using the regex yauncin provided.


s/,//g for @fields;


Don't forget to write the record back to the file when you are done. Flat files are a huge pain to update, so
you could either use a module that does the hard stuff in that respect, or work out your own routine to update the file.

--Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top