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

Converting Text 1

Status
Not open for further replies.

mfreeze

Programmer
Sep 11, 2003
16
0
0
US
Could someone post a code snippet of how to do the following? I just need a little help getting started since I am fairly new to Perl. Also, does anyone know of any good websites with Perl tutorials? Thanks in advance, Mark.

What I need to do is this:

On my Linux box, watch a certain directory for files, and when they appear, convert them from ascii fixed-length to ascii tab delimited. Could someone show me how to open a file in Perl, read from line to line in fixed length input (looping until eof) and writing these same records out to a tab delimited file. (Assuming the input data would look something like this:)

0010Mark 10/18/2003
0001Ted 11/04/2003
9091Billy 01/01/2001

(Code c(4), Name c(10), Date c(10))

Again, thanks for any help anyone may be able to provide.
 
this wil read line to line
from bla.txt in current dir
and in the wile you can do what ever you want with the input

i also know a good tutorial for perl you can find it in "man perl"
there are a couple of tutotials

i hope this is what you mean
#!/usr/bin/perl

open F,"bla.txt";
while (<F>) {
print $_;
}
 
Fietse solution won't get you very far. It will open your file and print its contents to the screen.

Here is something that should suit you a little better. Assuming that the code field is always 4 chars, the name field is always 10, and the date field is always 10, do this:

use strict;
use warnings;

open (IN, &quot;original_file.txt&quot;); #open your text file for reading
open (OUT, &quot;>temp.txt&quot;); #open a temp file for writing the tabbed output
while (<IN>) {
my ($code,$name,$date) =~ /(.{4})(.{10})(.{10})/; #search for 4, 10, 10 chars respectively and assign each set to code, name, date respectively
print OUT &quot;$code\t$name\t$date\n&quot;; #write code,name,date to the temp file with tabs between each
}
close (IN); #close the read file
close (OUT); #close the write file

rename (&quot;original_file.txt&quot; &quot;original_file.bak&quot;); #backup the original file
rename (&quot;temp.txt&quot; &quot;original_file.txt&quot;); #rename the temp file to the original file name

Cheers


 
yeah i understant that it will only print
but we are not going to write whole scripts are we ?? i mean i thought that wasnt what this form is about
 
The forum is about helping people. Period. When it comes to writing whole scripts or just offering advice - to each there own. As a general practice I do not provide scripts for people, but occassionally if I am feeling bored or generous then I will write one.

Regardless of whether I write scripts or not, I at least try to make my advice helpful. Your post didn't even steer mfreeze in the right direction.

If you wanted to be truly helpful without providing a script, then you could have done something like this:

open F,&quot;bla.txt&quot;;
while (<F>) {
...use a regex or split statement to separate text into various elements
...use a print statement to write each element out to a new file. use &quot;\t&quot; between variables to insert tabs.
}

...close your file
...rename the written file to the name of the original file

Oh, by the way I have only opened a handle to your original file for reading. You will need to open a second handle for writing.

Anyway, that is something along the lines of what I consider helpful when I don't provide a complete script.
 
Thanks for the help. The code snip really got me going as it was the only thing holding me up. You wouldn't believe how much that little code helped out.

As far as the rest goes, I appreciate the extended help from raklet. Without it I would not have been able to complete my project in the time that I did. The response that I received is the reason that I joined this forum in the first place. Not to say that I don't appreciate fietse's reply, but raklet's was EXACTLY what I was looking for and I don't understand why, if you know what someone needs, you would not provide that exact help to them. If it is too long for the forum, send them a private reply. If you dont have time to answer, don't reply. More often than not, half an answer is worse than no answer at all.

Thanks to both of you for your help,
Mark.

P.S. has good stuff also
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top