perl or sed for string search and replace
perl or sed for string search and replace
am 10.04.2008 23:49:16 von littlehelphere
I am trying to do a search and replace on a flat file. I have tried
sed and perl but cannot get the exact pattern match due to the fact
that there are multiple possible sets to match. Basically I am doing
a for loop and calling perl or sed to do the replacement. I need to
replace certain user email addresses with /dev/nul -
The file I am running against has entries such as
#joe: joe@domain.com, admin
joe: admin
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: bob@domain.com
The end result I need is for each instance of 'joe' and 'bob' to have
an email address of /dev/null
#joe: /dev/null
joe: /dev/null
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: /dev/null
I have tried
for name in ....
perl -p -e 's/[\t]'$name'@.*$/\/dev\/null/' file > file.tmp
....
The output is
#joe:/dev/null - GOOD
joe: refrates - MISSED
dobob: dobob@domain.com- GOOD
pebob: pebob@domain.com- GOOD
#c: /dev/null- GOOD
I have also tried
for name in ....
sed -e 's/'$name'@.*$/\/dev\/null/g' file > file.tmp
....
The output is
#joe: /dev/null- GOOD
joe: refrates - MISSED
dobob: do/dev/null - BAD
pebob: pe/dev/null - BAD
#bob: /dev/null- GOOD
Any ideas????
Re: perl or sed for string search and replace
am 11.04.2008 00:12:05 von Ed Morton
On 4/10/2008 4:49 PM, littlehelphere@gmail.com wrote:
> I am trying to do a search and replace on a flat file. I have tried
> sed and perl but cannot get the exact pattern match due to the fact
> that there are multiple possible sets to match. Basically I am doing
> a for loop and calling perl or sed to do the replacement. I need to
> replace certain user email addresses with /dev/nul -
>
> The file I am running against has entries such as
> #joe: joe@domain.com, admin
> joe: admin
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: bob@domain.com
>
> The end result I need is for each instance of 'joe' and 'bob' to have
> an email address of /dev/null
> #joe: /dev/null
> joe: /dev/null
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: /dev/null
$ cat file
#joe: joe@domain.com, admin
joe: admin
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: bob@domain.com
$ awk '$1 ~ /^#?(joe|bob):$/{ $0=$1" /dev/null" }1' file
#joe: /dev/null
joe: /dev/null
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: /dev/null
Ed.
Re: perl or sed for string search and replace
am 11.04.2008 00:28:00 von Edward Morton
On 4/10/2008 4:49 PM, littlehelphere@gmail.com wrote:
> I am trying to do a search and replace on a flat file. I have tried
> sed and perl but cannot get the exact pattern match due to the fact
> that there are multiple possible sets to match. Basically I am doing
> a for loop and calling perl or sed to do the replacement. I need to
> replace certain user email addresses with /dev/nul -
>
> The file I am running against has entries such as
> #joe: joe@domain.com, admin
> joe: admin
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: bob@domain.com
>
> The end result I need is for each instance of 'joe' and 'bob' to have
> an email address of /dev/null
> #joe: /dev/null
> joe: /dev/null
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: /dev/null
$ cat file
#joe: joe@domain.com, admin
joe: admin
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: bob@domain.com
$ awk '/^#?(joe|bob):$/{ $0=$1" /dev/null" }1' file
#joe: /dev/null
joe: /dev/null
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: /dev/null
Ed.
Re: perl or sed for string search and replace
am 11.04.2008 06:53:56 von someone
littlehelphere@gmail.com wrote:
> I am trying to do a search and replace on a flat file. I have tried
> sed and perl but cannot get the exact pattern match due to the fact
> that there are multiple possible sets to match. Basically I am doing
> a for loop and calling perl or sed to do the replacement. I need to
> replace certain user email addresses with /dev/nul -
>
> The file I am running against has entries such as
> #joe: joe@domain.com, admin
> joe: admin
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: bob@domain.com
>
> The end result I need is for each instance of 'joe' and 'bob' to have
> an email address of /dev/null
> #joe: /dev/null
> joe: /dev/null
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: /dev/null
$ echo "#joe: joe@domain.com, admin
joe: admin
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: bob@domain.com" | \
perl -pe's{(^\W*(?:joe|bob)\b\S*\s+).+$}{$1/dev/null}'
#joe: /dev/null
joe: /dev/null
dobob: dobob@domain.com
pebob: pebob@domain.com
#bob: /dev/null
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: perl or sed for string search and replace
am 11.04.2008 18:17:53 von littlehelphere
On Apr 11, 12:53 am, "John W. Krahn" wrote:
> littlehelphere@gmail.com wrote:
> > I am trying to do a search and replace on a flat file. I have tried
> > sed and perl but cannot get the exact pattern match due to the fact
> > that there are multiple possible sets to match. Basically I am doing
> > a for loop and calling perl or sed to do the replacement. I need to
> > replace certain user email addresses with /dev/nul -
>
> > The file I am running against has entries such as
> > #joe: joe@domain.com, admin
> > joe: admin
> > dobob: dobob@domain.com
> > pebob: pebob@domain.com
> > #bob: bob@domain.com
>
> > The end result I need is for each instance of 'joe' and 'bob' to have
> > an email address of /dev/null
> > #joe: /dev/null
> > joe: /dev/null
> > dobob: dobob@domain.com
> > pebob: pebob@domain.com
> > #bob: /dev/null
>
> $ echo "#joe: joe@domain.com, admin
> joe: admin
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: bob@domain.com" | \
> perl -pe's{(^\W*(?:joe|bob)\b\S*\s+).+$}{$1/dev/null}'
> #joe: /dev/null
> joe: /dev/null
> dobob: dobob@domain.com
> pebob: pebob@domain.com
> #bob: /dev/null
>
> John
> --
> Perl isn't a toolbox, but a small machine shop where you
> can special-order certain sorts of tools at low cost and
> in short order. -- Larry Wall
how do I substitute a variable from the loop into the perl command?
for example $i - perl -pe's{(^\W*(?:$i)\b\S*\s+).+$}{$1/dev/null}'
Re: perl or sed for string search and replace
am 11.04.2008 18:34:32 von Maxwell Lol
littlehelphere@gmail.com writes:
> how do I substitute a variable from the loop into the perl command?
> for example $i - perl -pe's{(^\W*(?:$i)\b\S*\s+).+$}{$1/dev/null}'
Try
perl -pe's{(^\W*(?:'"$i"')\b\S*\s+).+$}{$1/dev/null}'
Re: perl or sed for string search and replace
am 11.04.2008 20:07:31 von littlehelphere
On Apr 11, 12:34 pm, Maxwell Lol wrote:
> littlehelph...@gmail.com writes:
> > how do I substitute a variable from the loop into the perl command?
> > for example $i - perl -pe's{(^\W*(?:$i)\b\S*\s+).+$}{$1/dev/null}'
>
> Try
> perl -pe's{(^\W*(?:'"$i"')\b\S*\s+).+$}{$1/dev/null}'
Thanks - that seems to have done it. Can you break down the command
for me - for my own referenace and exactly what it does. Thanks.
Re: perl or sed for string search and replace
am 11.04.2008 21:21:44 von someone
littlehelphere@gmail.com wrote:
> On Apr 11, 12:34 pm, Maxwell Lol wrote:
>> littlehelph...@gmail.com writes:
>>> how do I substitute a variable from the loop into the perl command?
>>> for example $i - perl -pe's{(^\W*(?:$i)\b\S*\s+).+$}{$1/dev/null}'
>> Try
>> perl -pe's{(^\W*(?:'"$i"')\b\S*\s+).+$}{$1/dev/null}'
>
> Thanks - that seems to have done it. Can you break down the command
> for me - for my own referenace and exactly what it does. Thanks.
perl -pe
Perl is run with the -p switch which loops over input from either stdin
or from the files listed on the command line and prints out $_ which
contains the contents of the current line. The -e switch tells perl
that the following is perl code.
s{}{}
That is the substitution operator which modifies the contents of $_
using the regular expression pattern in the first part and replacing it
with the string in the second part.
(^\W*(?:joe|bob)\b\S*\s+).+$
The pattern is anchored at the beginning of the line ^ and at the end of
the line $. \W* matches any non-word character ([^[:alnum:]_] in perl)
zero or more times. (?:joe|bob) matches either the string 'joe' or the
string 'bob'. \b is a zero-width assertion that matches between a \w
and a \W character. \S* matches a non-whitespace character ([^
\t\r\n\f] in perl) zero or more times. \s+ matches a whitespace
character one or more times. All of the preceding are enclosed in
capturing parentheses. And finally .* matches any character except
newline one or more times.
$1/dev/null
$1 contains the string from the capturing parentheses in the pattern
followed by the literal string '/dev/null'.
HTH
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Re: perl or sed for string search and replace
am 11.04.2008 21:50:45 von Ed Morton
On 4/11/2008 1:07 PM, littlehelphere@gmail.com wrote:
> On Apr 11, 12:34 pm, Maxwell Lol wrote:
>
>>littlehelph...@gmail.com writes:
>>
>>>how do I substitute a variable from the loop into the perl command?
>>>for example $i - perl -pe's{(^\W*(?:$i)\b\S*\s+).+$}{$1/dev/null}'
>>
>>Try
>> perl -pe's{(^\W*(?:'"$i"')\b\S*\s+).+$}{$1/dev/null}'
>
>
> Thanks - that seems to have done it. Can you break down the command
> for me - for my own referenace and exactly what it does. Thanks.
You're really missing the point that you probably don't need a loop at all...
Ed.
Re: perl or sed for string search and replace
am 12.04.2008 02:23:27 von Maxwell Lol
littlehelphere@gmail.com writes:
> On Apr 11, 12:34 pm, Maxwell Lol wrote:
> > littlehelph...@gmail.com writes:
> > > how do I substitute a variable from the loop into the perl command?
> > > for example $i - perl -pe's{(^\W*(?:$i)\b\S*\s+).+$}{$1/dev/null}'
> >
> > Try
> > perl -pe's{(^\W*(?:'"$i"')\b\S*\s+).+$}{$1/dev/null}'
>
> Thanks - that seems to have done it. Can you break down the command
> for me - for my own referenace and exactly what it does. Thanks.
I just changed
'abcdefgh$ijklmnop'
into
'abcdefgh' "$i" 'jklmnop'
since a $i inside '....' is not treated as a special character.
But if it's inside "..." it means its a variable.