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

regular expression find and replace

Status
Not open for further replies.

ppeel

Programmer
Dec 7, 2004
18
US
I am a newbie to perl and trying to find a good method to replace data at the end of input records.
Input Data looks like this:
xxxxxxxxxxxxxxxxxxxxxxx*TL*12345CTOM~
xxxxxxxxxxxxxxxx*TL*92349A~
xxxxxxxxxxxxxxxxxxx*TL*46975M~
xxxxxxxxxx*TL*73124W9~

I need the output to look like this:
xxxxxxxxxxxxxxxxxxxxxxx*TL*12345~
xxxxxxxxxxxxxxxx*TL*92349~
xxxxxxxxxxxxxxxxxxx*TL*46975~
xxxxxxxxxx*TL*73124~

So I need to remove the text after the 5-digit number but leave the tilda.

Thanks for any suggestions.




 
Since we don't know what's in the xxx portion, and we might as well anchor to the end of the line, I'd go with the following

Code:
perl -pe 's/\D+~$/~/' /input/file

or

Code:
while (<DATA>) {
	s/\D+~$/~/;
	print;
}

__DATA__
xxxxxxxxxxxxxxxxxxxxxxx*TL*12345CTOM~
xxxxxxxxxxxxxxxx*TL*92349A~
xxxxxxxxxxxxxxxxxxx*TL*46975M~
xxxxxxxxxx*TL*73124W9~

Just note that your last example contains both a letter and number after the initial sequence. If you want that stripped too, then you'd have to do the following:

Code:
perl -pe 's/\*\d*\K\w*~$/~/' /input/file
 
Thank you so much!
MillerH, the last set of code ('s/\*\d*\K\w*~$/~/') works perfectly for my needs.
You all rock!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top