Get header and its matched value in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get header and its matched value in perl?
# 1  
Old 08-16-2010
Get header and its matched value in perl?

Hi,

I have the file contents:

Code:
Start,End,Req,Resp
12:39,12:40,4,5

The sting to be matched is: Req and Resp.

Code:
parsefile("Req,Resp");

Here is the code.
Code:
sub parsefile ($)
{
$header=shift;
open(FH,"file.txt");
@lines=<FH>;
#stuck up here.....
}

I am not able to parse the file badly stuck up.

How to get it values?

The output should be:

Code:
Req: 4
Resp: 5
How can i get the above output in perl? Regards vanitha

# 2  
Old 08-16-2010
Something like this,

hdr_fld.pl

Code:
#!/usr/bin/perl
use strict;

my $hdr1=shift;
my $hdr2=shift;

my $hdr_pos1;
my $hdr_pos2;

chomp($hdr1,$hdr2);

open(FH,"<","inputfile") || die " cannot open file \n";
while (<FH>) {
        chomp;
        if ($. == 1 ) {
                if (/(\w+),(\w+),(\w+),(\w+)/) {
                        if ( $1 eq $hdr1 ) { $hdr_pos1 ='$1';}
                        if ( $2 eq $hdr1 ) { $hdr_pos1 = '$2'; }
                        if ( $3 eq $hdr1 ) { $hdr_pos1 ='$3'; }
                        if ( $4 eq $hdr1 ) { $hdr_pos1 ='$4'; }

                        if ( $1 eq $hdr2 ) { $hdr_pos2 ='$1';}
                        if ( $2 eq $hdr2 ) { $hdr_pos2 = '$2'; }
                        if ( $3 eq $hdr2 ) { $hdr_pos2 ='$3'; }
                        if ( $4 eq $hdr2 ) { $hdr_pos2 ='$4'; }
                }
        }
        if (/(\d+\:\d+),(\d+\:\d+),(\d+),(\d+)/) {
                print $hdr1,":",eval($hdr_pos1),"\n",$hdr2,":",eval($hdr_pos2),"\n";
        }
}

invocation
Code:
perl hdr_fld.pl field1 field2
e.g 
perl hdr_fld.pl Req Resp

# 3  
Old 08-17-2010
Hi Vanitha,

Here is the script that could help...This can find any header in the script.

Code:
#!/usr/bin/perl -w

parsefile("Req","Resp");

sub parsefile
{
my $header1 = shift;
my $header2 = shift;


open(FH,"file.txt");
@lines=<FH>;
close(FH);
my %head = ();

foreach $line (@lines) {
   chomp $line;
   if($line =~ m/$header1/) {
     @break = split(",", $line);
   }
   else {
     @val = split(",", $line);
     foreach(@break) {
       $head{"$_"} = shift @val;
     }
    print "$header1 = $head{$header1}\n";
    print "$header2 = $head{$header2}\n";
   }
}
}


-Raja
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to display the header of a matched row in a file?

Hi, So I am trying to print the first row(header) first column alongwith the matched value. But I am not sure how do I print the same, by matching a pattern located in the file eg File contents Name Place Jim NY Jill NJ Cathy CA Sam TX Daniel FL And what I want is... (2 Replies)
Discussion started by: sidnow
2 Replies

2. Shell Programming and Scripting

Print only matched pattern in perl

Hi, I have script like below: #!/usr/local/bin/perl use strict; use warnings; while (<DATA>) { ( my ($s_id) = /^\d+\|(\d+?)\|/ ) ; if ( $s_id == 1 ){ s/^(.*\|)*.*ABC\.pi=(+|+)*.*ABC\.id=(\d+|+).*$/$1$2|$3/s; print "$1$2|$3\n"; (2 Replies)
Discussion started by: sol_nov
2 Replies

3. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

4. Linux

Perl program to print previous set of lines once a pattern is matched

Hi all, I have a text data file. My aim here is to find line called *FIELD* AV for every record and print lines after that till *FIELD* RF. But here I want first 3 to four lines for very record as well. FIELD AV is some where in between for very record. SO I am not sure how to retrieve lines in... (2 Replies)
Discussion started by: kaav06
2 Replies

5. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

6. Shell Programming and Scripting

How to get the lines matched of a file in perl?

Hi, I want to match the time in the file and retrieve those contents of the file. I am taking only first two parameters of localtime(time) function minutes and seconds so partial match i am performing. For Example $start = "14:23"; $end = "14:30"; I am matching file contents... (3 Replies)
Discussion started by: vanitham
3 Replies

7. Shell Programming and Scripting

Get all the values matched in perl

HI, I have sentences like this: @str=("An ribonucleic acid (RNA)-binding protein, started its expression in the daughter cells","Elucidation of the mechanism of retinal degeneration of RNA-binding protein","Rna binding protein is an important protein","In the retinal degenerative process... (1 Reply)
Discussion started by: vanitham
1 Replies

8. Shell Programming and Scripting

How to match all array contents and display all highest matched sentences in perl?

Hi, I have an array with 3 words in it and i have to match all the array contents and display the exact matched sentence i.e all 3 words should match with the sentence. Here are sentences. $arr1="Our data suggests that epithelial shape and growth control are unequally affected depending... (5 Replies)
Discussion started by: vanitham
5 Replies

9. Shell Programming and Scripting

print last matched pattern using perl

Hi, If there exist multiple pattern in a file, how can I find the last record matching the pattern through perl. The below script searches for the pattern everywhere in an input file. #! /usr/bin/perl -s -wnl BEGIN { $pattern or warn"Usage: $0 -pattern='RE' \n" and exit 255;... (5 Replies)
Discussion started by: er_ashu
5 Replies

10. Shell Programming and Scripting

HELP! PERL script to find matched pattern

Hi all, I just learnt Perl and I encountered a problem in my current project. For a verilog file, i am required to write a PERL script that could match pattern to output nitrolink and nitropack. I wont know what name to grep except the pattern below. the verilog file: nitrolink nitrolink... (1 Reply)
Discussion started by: kimhuat
1 Replies
Login or Register to Ask a Question