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

parsing of text files using perl

Status
Not open for further replies.

kkazakos

Programmer
Joined
Apr 4, 2007
Messages
4
Location
US
Hello friends,

I am exploring write now the PERL language and i must say that i am very excited!!!!

My first personal goal is to write a simple program in perl that does the following :

It reads the info from file1.txt. File1 structure is:
r 250 4 26 960 0 0 88 x1*
There are 64000lines that have the above format with x1....x64000.

I would like to take all the x1..x64000 ONLY and to print them in a new file which i named file2.txt.

Therefore file2.txt will have only the x1...x64000.

Could u please guide me on how to do that?
I have serious problems since i do not know how to recognize the end of each line...

regards
kostas
 
Code:
use strict;
use warnings;

while (<>) {                       # read each line
   my $x64k = (split /\s+/) [-1];  # split and take last item
   print $x64k;                    # print it out
}                                  # er, that's it...
As you are new at this, I've kept it simple. No file opening or closing; it's all implicit. To use it just
Code:
perl myscript.pl myinputfile.txt > myoutputfile.txt

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Hello stevexff,

Thanks. Could you please take a look at the following code and let me know if it is correct? Is this how a PERL program should be constructed?
------------------------------------------------------------
#!/usr/local/bin/perl

$file = 'C:\telemetry_vld.txt';
open(INFO, $file); #opens file telemetry_vld.txt
open(DATA, ">final.txt"); #file to write data to
@lines = <INFO>; #assigns lines to array

while (<>) #go through each line in file
{
my $x64k = (split /\s+/) [-1]; # split and take last item
DATA == $x64k;
print DATA "\n";
}

close(INFO); #closes file
close(DATA);
------------------------------------------------------------

What if i had only 4 lines? Then what will have happened?

regards
kkazakos
 
Also i would like to get rid of the '*' character

What i mean is this:
I have the input file e.g. ==>
r 250 4 26 960 0 0 88 x1*
r 250 5 26 356 0 0 56 x2*

I want in the output file ==>
x1
x2


How do i do this?

thanks
 
Code:
DATA ==  $x64k;
  print DATA "\n";

should be

Code:
 print DATA "$x64k\n";
 
Get rid of the *

$x64k =~ s/\*//g;

but I'm sure someone has a better idea :)
 
$x64k =~ s/\*//g;

i am sorry but you lost me there..

where in the code shall i put that?

thank you
kkazakos
 
borrowing heavily from steves code:

Code:
[gray]#!/usr/local/bin/perl[/gray]
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]


[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$file[/blue] = [red]'[/red][purple]C:/telemetry_vld.txt[/purple][red]'[/red][red];[/red]
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red]IN, [blue]$file[/blue][red])[/red][red];[/red]         [gray][i]#opens file telemetry_vld.txt[/i][/gray]
[black][b]open[/b][/black][red]([/red]OUT, [red]"[/red][purple]>final.txt[/purple][red]"[/red][red])[/red][red];[/red] [gray][i]#file to write data to[/i][/gray]
[olive][b]while[/b][/olive] [red]([/red]<IN>[red])[/red]             [gray][i]#go through each line in file[/i][/gray]
[red]{[/red]
  [black][b]my[/b][/black] [blue]$x[/blue] = [red]([/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]/[/red][purple][purple][b]\s[/b][/purple]+[/purple][red]/[/red][red])[/red] [red][[/red]-[fuchsia]1[/fuchsia][red]][/red][red];[/red]  [gray][i]# split and take last item[/i][/gray]
  [blue]$x[/blue] =~ [red]tr/[/red][purple]*[/purple][red]/[/red][purple][/purple][red]/[/red][red]d[/red][red];[/red] [gray][i]#remove * from string[/i][/gray]
  [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] OUT [red]"[/red][purple][blue]$x[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[url=http://perldoc.perl.org/functions/close.html][black][b]close[/b][/black][/url][red]([/red]IN[red])[/red][red];[/red]
[black][b]close[/b][/black][red]([/red]OUT[red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
kkazakos said:
$x64k =~ s/\*//g;

i am sorry but you lost me there..
Understandable, if you are new to Perl. This is a Regular Expression and is one of the most powerful features of Perl, but unfortunately one of the most cryptic.

In English, it says "look at the contents of variable $x64k, and replace every occurrence of "*" with "". This effectively removes your trailing *, although technically we should say
Code:
$x64k =~ s/\*$//;
which tells it to only replace the * if it is the last character.

There's a little bit of RegEx in my split command, too. The /\s+/ says "split on one or more whitespace characters.

Now's probably not the time, but there is more information in the perlre documentation. There's also a very good RegEx site at
Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::PerlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top