perl: search and replace help

B

bhooshan.dixit

Hi all,
I have a file which looks like this
^$-=(BILL_STG.BILL
^$-=(BILL_STG.BILL_SQ
...
...
...

i need help in perl to open the file, clean out the wierd charcters
before the 'BILL' on each line and write back to file. The characters
appear on start of each line. I need to elminate everything until and
including the '(' character

Can some one help me build a reg exp for this.

Thanks
bcd
 
M

Matt Garrish

Hi all,
I have a file which looks like this
^$-=(BILL_STG.BILL
^$-=(BILL_STG.BILL_SQ

i need help in perl to open the file, clean out the wierd charcters
before the 'BILL' on each line and write back to file. The characters
appear on start of each line. I need to elminate everything until and
including the '(' character

What have you tried so far? What are "weird" characters? Which of the two
BILLs on the above lines do you remove until?

I would suggest you start with a little reading:

http://perldoc.perl.org/perlopentut.html
http://perldoc.perl.org/perlre.html

Matt
 
J

John Bokma

Hi all,
I have a file which looks like this
^$-=(BILL_STG.BILL
^$-=(BILL_STG.BILL_SQ
..
..
..

i need help in perl to open the file, clean out the wierd charcters
before the 'BILL' on each line and write back to file. The characters
appear on start of each line. I need to elminate everything until and
including the '(' character

If each and every line looks like that, get a text editor with macro
recording [1]. Works faster then write the perl script.
Can some one help me build a reg exp for this.

perldoc -f substr


[1] TextPad for example
 
B

bhooshan.dixit

I am running the script to clean a file that is generated via some
other process.
I have opened a file handle for the file and am reading the contents
line by line using a while loop into $_

so something like this
$_ =~ s/\W+\\;

but this doesnot help

can anybody send me a better search and replace command.
 
X

Xicheng Jia

I am running the script to clean a file that is generated via some
other process.
I have opened a file handle for the file and am reading the contents
line by line using a while loop into $_

so something like this
$_ =~ s/\W+\\;

but this doesnot help

can anybody send me a better search and replace command.

This looks like being kinda FAQ in this group.. :), try this:

s/^(.*?BILL)/my $x=$1; $x=~s#\W##g; $x/e;

Xicheng
 
J

John Bokma

Xicheng Jia said:
This looks like being kinda FAQ in this group.. :), try this:

s/^(.*?BILL)/my $x=$1; $x=~s#\W##g; $x/e;

Wow!

"I need to elminate everything until and including the '(' character"

s/.+?\(//;

yet if weirdness is always the same amount of weirdness, I probably would
go for the substr, or use an editor :-D
 
X

Xicheng Jia

John said:
Wow!

"I need to elminate everything until and including the '(' character"

I guess he just wants to filter out non-word characters before the
first instance of BILL, so:

^$I_A%%ND-_=(BILL_STG.BILL_SQ%%%

will become:

I_AND_BILL_STG.BILL_SQ%%%

using back reference and then another s/// or tr/// expression on the
replacement part is exactly like working on a substr, isn't it? while
in the real code, one can write a subroutine instead of an embedded
s/// or tr/// expression, the way to solve this kind of problems is
pretty much clear and easy to be extended.:)

Xicheng
 
J

John Bokma

Xicheng Jia said:
John said:
"Xicheng Jia" <[email protected]> wrote:
[..]
Wow!

"I need to elminate everything until and including the '('
character"

I guess he just wants to filter out non-word characters before the
first instance of BILL, so:

As I read it: remove all characters up until and including the (, which
can be done with a single regexp, or if the amount of characters is always
the same, a substr.
using back reference and then another s/// or tr/// expression on the
replacement part is exactly like working on a substr, isn't it? while
in the real code, one can write a subroutine instead of an embedded
s/// or tr/// expression, the way to solve this kind of problems is
pretty much clear and easy to be extended.:)

It sounds like using a canon to kill a mosquito :-D

Please delete lines you don't comment on. It's odd that you left them,
especially since they clarify what I was writing :)
 
X

Xicheng Jia

John said:
As I read it: remove all characters up until and including the (, which
can be done with a single regexp, or if the amount of characters is always
the same, a substr.

consider strings like:

^$-(=(BILL_STG.BILL_SQ

If he wanted to remove all characters(both word and non-word) up until
the first instance of the string "BILL", it might be better to use
something like:

s/.*?\)(?=BILL)//;

and it can also be done by using index() and substr() to find the
sub-string up untill the first "BILL", no need to be a fixed-length
substring. :)
It sounds like using a canon to kill a mosquito :-D

I didnt say that a function is necessary, but for better maintenance, a
function does not hurt. :)

Xicheng
 
X

Xicheng Jia

Xicheng said:
consider strings like:

^$-(=(BILL_STG.BILL_SQ

If he wanted to remove all characters(both word and non-word) up until
the first instance of the string "BILL", it might be better to use
something like:

s/.*?\)(?=BILL)//;

sorry for the typo(an opening parenthesis not closing parenthesis):

s/.*?\((?=BILL)//;

also, if there are characters between this opening parenthesis "(" and
the string "BILL", it doesnot work..that's why I wouldnot like this
method..:)

Xicheng
 
X

Xicheng Jia

jb> As I read it: remove all characters up until and including the (,
which
jb> can be done with a single regexp, or if the amount of characters is
always
jb> the same, a substr.

I guess we were thinking about different problems, hehe :)
xc> If he wanted to remove all characters(both word and non-word) up
until
xc> the first instance of the string "BILL", it might be better to use
xc> something like:
err, my big fault, for the above purpose, the parenthesis is completely
not necessary.

s/.*?(?=BILL)//;

Xicheng
 
J

John Bokma

Xicheng Jia said:
consider strings like:

^$-(=(BILL_STG.BILL_SQ

Yes, lets make up examples.
I didnt say that a function is necessary, but for better maintenance,
a function does not hurt. :)

Maybe reread the OP again, instead of making up examples and dreaming up
complicated code.
 
A

axel

I am running the script to clean a file that is generated via some
other process.

What do you mean by 'clean a file'?
I have opened a file handle for the file and am reading the contents
line by line using a while loop into $_
so something like this
$_ =~ s/\W+\\;
but this doesnot help
can anybody send me a better search and replace command.

Add a 'g' modifier to the replace string, _vide_ perldoc perlre.

Axel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top