search and replace basics

I

IJALAB

Hi,

My file is like

(20:34:08.747) Rx 48 bytes
<F0><00><10><00><05><18><00><73><1B><0D><00>
(20:34:08.810) Rx 48 bytes
From a file(Text1.txt), I have to remove the text before 48 bytes and
retain the remaining string.I am trying to do the following
open(FILE1, ">Text1.txt") || die "Can't open output file.";

while(<FILE1>)
{
s/*.*bytes //g;
print FILE1 ;

}

I am unable to achieve what I want. In the manual, it is given as $'
will print everything after the match string. but that also doesn't
work.

Also, I need to remove the angle brackets. I did the same thing

s/[<>]//g;

in vain.

can some one help me out?

-bala
 
P

Paul Lalli

IJALAB said:
My file is like

(20:34:08.747) Rx 48 bytes
<F0><00><10><00><05><18><00><73><1B><0D><00>
(20:34:08.810) Rx 48 bytes

retain the remaining string.I am trying to do the following
open(FILE1, ">Text1.txt") || die "Can't open output file.";

This attempts to open the file for WRITING, not reading. It will
clobber your input file.
while(<FILE1>)
{
s/*.*bytes //g;

This is a compilation error, as you can't have a * following nothing.
It makes no sense.
print FILE1 ;

Now you're trying to print to the filehandle you jsut read from. That
does't make any sense. Open a file for reading, read from the file,
make changes, and print the changed lines to a new file.
}

I am unable to achieve what I want. In the manual, it is given as $'
will print everything after the match string.

Yes, and? Why do you think you want that? Your regexp is
theoretically intended to change the string to eliminate the parts you
don't need, leaving you with just the parts you do need.
but that also doesn't work.

"doesn't work" is the worst possible error description.
Also, I need to remove the angle brackets. I did the same thing

s/[<>]//g;

in vain.

"in vain" is the same as "doesn't work". What is that supposed to
mean? How did it go wrong?
can some one help me out?

You would do well to read a decent Perl tutorial....

#!/usr/bin/perl
use strict;
use warnings;

open my $fh, '<', 'Text1.txt' or die "Cannot open Text1.txt: $!";
open my $ofh, '>', 'Text1.mod.txt' or die "Cannot open Text1.mod.txt:
$!";

while (my $line = <$fh>) {
$line =~ s/^.*?bytes//;
$line =~ tr/<>//d;
print $ofh $line;
}

close $fh;
close $ofh;
__END__
 
J

John W. Krahn

IJALAB said:
My file is like

(20:34:08.747) Rx 48 bytes
<F0><00><10><00><05><18><00><73><1B><0D><00>
(20:34:08.810) Rx 48 bytes
From a file(Text1.txt), I have to remove the text before 48 bytes and
retain the remaining string.I am trying to do the following
open(FILE1, ">Text1.txt") || die "Can't open output file.";

while(<FILE1>)
{
s/*.*bytes //g;
print FILE1 ;

}

I am unable to achieve what I want. In the manual, it is given as $'
will print everything after the match string. but that also doesn't
work.

Also, I need to remove the angle brackets. I did the same thing

s/[<>]//g;

in vain.

( $^I, @ARGV ) = ( '.bak', 'Text1.txt' );

while ( <> ) {
s/.*?bytes *//;
tr/<>//d;
print;
}



John
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top