Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Extract and print two specific keys and values using %

by mao9856 (Sexton)
on Oct 30, 2017 at 14:22 UTC ( [id://1202330]=perlquestion: print w/replies, xml ) Need Help??

mao9856 has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I am very beginner of perl. I have a large data(around 10,000 columns and 13 rows) in a file with following kind of data: Below is the head -6 lines of data:

A B C . id “ABS0056”; D; E; F; G; name “SAM”; H; I; J; K;

A B C . id “ABS0059”; D; E; F; name “JOE”; G; H; I; J; K;

A B C . id “ABS0060”; D; E; F; G; name “MARY”; H; I; J; K;

A B C . id “ABS0057”; D; E; F; G; H; name “BILL”; I; J; K;

A B C . id “ABS0065”; D; E; name “RONIE”; F; G; H; I; J; K;

A B C . id “ABS0061”; D; E; F; G; name “STEPHAN”; H; I; J; K;

I want my output in following data format:

id “ABS0056”; name “SAM”;

id “ABS0059”; name “JOE”;

id “ABS0060”; name “MARY”;

id “ABS0057”; name “BILL”;

id “ABS0065”; name “RONIE”;

id “ABS0061”; name “STEPHAN”

I have tried using code as below:
#!/usr/bin/perl use strict; use warnings; my$file=shift(@ARGV); my%aa=$_; open(FILE,'<',$file); while(<FILE>) { if($_=~/^# id:/ || $_=~/^# name:/) { print $_; my$entry=<FILE>; print $entry; print %aa; } } close FILE;
Thank you in advance!!

Replies are listed 'Best First'.
Re: Extract and print two specific keys and values using %
by toolic (Bishop) on Oct 30, 2017 at 14:46 UTC
    Here is one way to get the output you specify:
    use warnings; use strict; while (<DATA>) { if (/(id "\w+";).+(name "\w+";)/) { print "$1 $2\n"; } } __DATA__ A B C . id "ABS0056"; D; E; F; G; name "SAM"; H; I; J; K; A B C . id "ABS0059"; D; E; F; name "JOE"; G; H; I; J; K; A B C . id "ABS0060"; D; E; F; G; name "MARY"; H; I; J; K; A B C . id "ABS0057"; D; E; F; G; H; name "BILL"; I; J; K; A B C . id "ABS0065"; D; E; name "RONIE"; F; G; H; I; J; K; A B C . id "ABS0061"; D; E; F; G; name "STEPHAN"; H; I; J; K;

    I made a change to your input data: I changed the funky quotes to simple quotes. Read perlre to understand the capturing parentheses and $1 nd $2.

Re: Extract and print two specific keys and values using %
by hippo (Bishop) on Oct 30, 2017 at 14:52 UTC

    Like toolic I've replaced your duff quotes with double quotes. Here's a solution using the hash (as that looks to be an area confusing you):

    #!/usr/bin/env perl use strict; use warnings; while (<DATA>) { my %aa = (); for my $key (qw/id name/) { ($aa{$key}) = (/$key (\S+)/); print "$key $aa{$key} "; }; print "\n"; } __DATA__ A B C . id "ABS0056"; D; E; F; G; name "SAM"; H; I; J; K; A B C . id "ABS0059"; D; E; F; name "JOE"; G; H; I; J; K; A B C . id "ABS0060"; D; E; F; G; name "MARY"; H; I; J; K; A B C . id "ABS0057"; D; E; F; G; H; name "BILL"; I; J; K; A B C . id "ABS0065"; D; E; name "RONIE"; F; G; H; I; J; K; A B C . id "ABS0061"; D; E; F; G; name "STEPHAN"; H; I; J; K;

    Update: removed the g modifier as it isn't necessary here.

Re: Extract and print two specific keys and values using %
by Discipulus (Canon) on Oct 30, 2017 at 15:05 UTC
    Hello mao9856 and welcome to the monastery and to wonderful world of Perl!

    I propose a solution based on array (even if here the regex is more appropriate and secure imho), just for sake of variety. The problem is that, while first fields are fixed, name and the name are not. So I'll cycle reamining elements of the array to spot name and then I'll print that element and the next one. The syntax $#arr means the number of the last index in the array @arr

    use warnings; use strict; while (<DATA>) { chomp; my @arr = split /\s/,$_; print "$arr[4] $arr[5] "; for (6..$#arr){ if ($arr[$_] eq 'name'){ print "$arr[$_] $arr[$_+1]\n"; last; } } } __DATA__ A B C . id "ABS0056"; D; E; F; G; name "SAM"; H; I; J; K; A B C . id "ABS0059"; D; E; F; name "JOE"; G; H; I; J; K; A B C . id "ABS0060"; D; E; F; G; name "MARY"; H; I; J; K; A B C . id "ABS0057"; D; E; F; G; H; name "BILL"; I; J; K; A B C . id "ABS0065"; D; E; name "RONIE"; F; G; H; I; J; K; A B C . id "ABS0061"; D; E; F; G; name "STEPHAN"; H; I; J; K;

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Extract and print two specific keys and values using %
by kcott (Archbishop) on Oct 31, 2017 at 07:30 UTC

    G'day mao9856,

    Welcome to the Monastery.

    I see very little correlation between your title, data and code. Please see "How do I post a question effectively?".

    Given this data:

    $ cat pm_1202330_input.txt A B C . id “ABS0056”; D; E; F; G; name “SAM”; H; I; J; K; A B C . id “ABS0059”; D; E; F; name “JOE”; G; H; I; J; K; A B C . id “ABS0060”; D; E; F; G; name “MARY”; H; I; J; K; A B C . id “ABS0057”; D; E; F; G; H; name “BILL”; I; J; K; A B C . id “ABS0065”; D; E; name “RONIE”; F; G; H; I; J; K; A B C . id “ABS0061”; D; E; F; G; name “STEPHAN”; H; I; J; K;

    You can get the output you indicate with this code:

    $ perl -nle 'print /(id\s+\S+).*?(\sname\s+\S+)/' pm_1202330_input.txt id “ABS0056”; name “SAM”; id “ABS0059”; name “JOE”; id “ABS0060”; name “MARY”; id “ABS0057”; name “BILL”; id “ABS0065”; name “RONIE”; id “ABS0061”; name “STEPHAN”;

    Note: I've used exactly the data you posted. Quotes have not been changed.

    — Ken

Re: Extract and print two specific keys and values using %
by mao9856 (Sexton) on Oct 31, 2017 at 09:50 UTC

    Thank you all for quick response and help:)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1202330]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-26 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found