By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

Perl and print text file selectively help!

P: 2
hey perl gurus!
i am new to this forum cause i need help. I have done many scripts.
but i want to use perl to do this:

What I want to do is this. I have a QRP file that I can convert to a txt file field separated by commas or not. I want to pring each line that has an actual date to the right of the word 'DATE:' if it doesnt have a date do not print that line.
Then the next thing i want my perl program to do is print each line that has
a numerical match. Print all fields in that line. What it is is a keyless entry
data file. People come into this gym i attend and it logs the date once for that day and then keeps track of each time someone uses the keyless entry. Shows the time and some words and then their mbr number and then their name. like this

Expand|Select|Wrap|Line Numbers
  1. Date: 01/07/2008
  3. 3:45AM  1 Door One blah blah blah:  23 John Doe
  4. 4:45AM  1 Door One blah blah blah:  299 Jerry Doe
  5. 5:45AM  1 Door One blah blah blah:  223 Sissy Doe
  7. Date: 01/08/2008
  9. 3:45AM  1 Door One blah blah blah:  23 John Doe
  10. 4:45AM  1 Door One blah blah blah:  299 Jerry Doe
  11. 5:45AM  1 Door One blah blah blah:  223 Sissy Doe
  12. 7:45PM  1 Door One blah blah blah:  23 John Doe
what i want this program to do is to parse the text file and print an out put file
that is querried by their member number before their name and then gives
and out put like this: So the user puts in the member number of 23 and gets below text file. It searches entire file above.

Expand|Select|Wrap|Line Numbers
  1. Date: 01/07/2008
  3. 3:45AM  1 Door One blah blah blah:  23 John Doe
  5. Date: 01/08/2008
  7. 3:45AM  1 Door One blah blah blah:  23 John Doe
  8. 7:45PM  1 Door One blah blah blah:  23 John Doe
pseudo code only.

read data.txt
print Date and actual date if date is to the right of the word Date:
(some lines with Date: do not have a date to the right; do not want to print nothing)
Print entire line that has member number that you are searching in only print
this entire line and go on to the next line searching for member number in that line and print that line tooo.
Do this until EOF.

can anyone help me I have a clue but need to get some good code.
I know you guys do it all the time and would appreicate your guidance.
or a great reference to doing this.

i have messed with code like in C++ but I would like to use perl to do this.
I think it can.


Robert ;)
Mar 10 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 2.5K+
P: 3,503
Perl was made for text processing so to say you think you can should be more of an of course it can.

Why not produce some code and post it here (with the code tags as was mentioned by eWish) and then we will help you troubleshoot it if you get stuck.


Mar 10 '08 #2

P: 2
i will try....
i will try....
i will try....
Mar 11 '08 #3

Expert 2.5K+
P: 4,059
I would not normally post code unless you have tried to write some code first but I get the sense this is an honest request for help and the solution is pretty simple. Your file format is a bit vague, especially the blah blah blah part, but this may work:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. my $id = 23;
  4. open( my $data, 'path/to/QRPfile') or die "$!";
  5. while(<$data>){
  6.     print if (m#Date: \d\d/\d\d/\d\d\d\d#); # find the date
  7.     print if (m#:\s+$id[\s\S]+$#); # find the user id
  8. }
  9. close $data or die "$!";
of course you may find that is very basic and you will want a data structure that is more useful, like a hash of the user id that stores the specifics of what the user did on any date, but we can cross that bridge later if you want to.
Mar 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.