in reply to Extract and print two specific keys and values using %

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.