?
Solved

perl search and print regular expression

Posted on 2003-03-27
6
Medium Priority
?
202 Views
Last Modified: 2010-03-05
file1 contains:
rownum   id             gender  names
---      --------       ----    -----
<1> <1.2.3.4.5.0.0>    <male>  <john>
<1> <1.2.3.4.5.0.3>    <male>  <joe>
<1> <1.2.3.4.5.0.9>    <male>  <mary>


I want the result to store in file2 as:
john 0
joe 3
mary 9


file2 should have two columns:
column 1 is name without <>
column 2 the last digit of the id


can someone help me write this in perl?
I spend about one hour and could not get it to work.

thanks in advance

0
Comment
Question by:aauser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 8221049
#!/usr/bin/perl
use strict;

open FILE1, "file1.txt" or die "Can not open file1.txt $!\n";
open FILE2, ">file2.txt" or die "Can not open file2.txt $!\n";

while (<FILE1>) {
  chomp;
  next unless (/</);
  my ($id,$name) = /<\d+>\s+<\S+(\d+)>\s+<\w+>\s+<(\w+)>/;
  print FILE2 "$name $id\n";
}

close FILE1;
close FILE2;
0
 
LVL 1

Expert Comment

by:OKSD
ID: 8221089
Hmm... won't that erase file2.txt each time the program is run? If you want to update suff often, that could be a problem, what about useing ">>file2.txt"?

OKSD
0
 

Author Comment

by:aauser
ID: 8221646
excellent job- thanks.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aauser
ID: 8221700
if I have /tim under the name column, it does not
print /tim.  How can I fix this?  thanks
0
 

Author Comment

by:aauser
ID: 8221732
the last line under name is /
I need to print this too.
so file2.txt would look something like:

john 0
joe 3
mary 9
.
.
/tim 8
/ 0




0
 

Author Comment

by:aauser
ID: 8221764
forget it.
I got it to work.  I don't need to display /tim.
just tim is ok.

thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question