Re: Need help w/ reqexp, capture and substitue
- From: rob.dixon@xxxxxxx (Rob Dixon)
- Date: Mon, 30 Oct 2006 00:55:40 +0000
Dennis G. Wicks wrote:
Greetings;
I frequently have to convert some address books from one format to another. This is what I need to do.
Input records are like this:
wix@xxxxxxxxxx&Thu Oct 26 07:20:03 2006&1&&&&&&&
The output record needs to be like this:
wix@xxxxxxxxxx&Thu Oct 26 07:20:03 2006&1&wix&&&&&&
In other words, get the user name from the email address and insert it between the 2 ampersands after the number that follows the year. The ampersands are field separators
and there will always be an email, a date, and a number.
I can capture the user name and I can substitute something in between the ampersands, but I can't substitute the user name between the two ampersands.
Here is what I have up now:
perl -i -p -e 'm/^(.*)?@/;s/\&\&/\&$1\&/;' test.txt
There is a key piece of this that I can't figure out.
Any help greatly appreciated!
Hi Dennis
This is failing because the match part of the substitution is succeeding and so
wiping the contents of $1 since it doesn't contain a capture. Save it in another
variable first:
m/^(.*)?@/;
my $user = $1;
s/&&/&$user&/;
(you don't need to escape the ampersands.)
Cheers,
Rob
.
- References:
- Need help w/ reqexp, capture and substitue
- From: Dennis G. Wicks
- Need help w/ reqexp, capture and substitue
- Prev by Date: Yay, I finally learned how to get screwed by Perl
- Next by Date: Re: Need help w/ reqexp, capture and substitue
- Previous by thread: Re: Need help w/ reqexp, capture and substitue
- Next by thread: Re: Need help w/ reqexp, capture and substitue
- Index(es):
Relevant Pages
|