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

Sub Help - dealing with hyphens

Status
Not open for further replies.

perlcamp

Programmer
Joined
Aug 12, 2010
Messages
6
Location
US
I have a file of records that look like this:

123-456,RTY,76-78,12-09-87,key

I would like to remove the hyphens from the first, and fourth values as delimited by commas but not the third value. So I would like to have the record look like this:

123456,RTY,76-78,120987,key

Can I do this using a Perl regular expression? I haven't had any luck, and I just end up stripping out all hyphens.
 
Post the code that you have tried.
 
all i have tried was the simple sub command which is removing all hyphens:

perl -pi -e "s#-##g"

I know that isn't the answer. I have been searching for something that would do the subs, but only on the values first and fourth.

If you point me in the right direction I don't mind doing research. I just don't even know what topic to look for at this point.
 
Something like this perhaps?

Code:
perl -pi -e '@a=split(/,/);$a[0]=~s/-//g;$a[3]=~s/-//g;$_=join(",",@a);'

In plain English... split the line up by commas into an array, do the replacements on specific members of the array, then join them together again ready to print.

Annihilannic.
 
Try this: 's/-//; s/(\d\d)-(\d\d)-/$1$2/'


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top