I'm not entirely sure I understand the rules by which you want newlines to be added to the output or not. In the following, if you don't want newlines in the output, change the two occurrences of "\n" to " ".
#!/usr/bin/env perl use warnings; use strict; my %data; my $cur_tag; while (<DATA>) { chomp; if ( my ($tag,$line) = /^(X\S+)\s+(.+)$/ ) { $data{$tag} .= "\n" if exists $data{$tag}; $data{$tag} .= $line; $cur_tag = $tag; } elsif ($cur_tag && !/^BEGIN_TAG/) { $data{$cur_tag} .= "\n".$_; } } for my $tag (sort keys %data) { print "$tag: $data{$tag}\n"; } __DATA__ BEGIN_TAG X1 test1 test1 test1 X2 no1 no1 no1 no1 X3 yes1 yes1 yes1 BEGIN_TAG X1 test2 test2 test2 test3 test3 test3 X2 no2 no2 no2 no2 no2 no3 no3 no no3 no3 no4 no4 no4 no4 no4 no4 no4 X3 hi2 hi2 hi2 hi2 hi2 hi2 hi3 hi3 hi3 hi3 hi4 hi4
Output:
X1: test1 test1 test1 test2 test2 test2 test3 test3 test3 X2: no1 no1 no1 no1 no2 no2 no2 no2 no2 no3 no3 no no3 no3 no4 no4 no4 no4 no4 no4 no4 X3: yes1 yes1 yes1 hi2 hi2 hi2 hi2 hi2 hi2 hi3 hi3 hi3 hi3 hi4 hi4
Update: Inverted "\n" vs " ".
In reply to Re^2: Scanning a file and extracting certains values within one or multiple lines
by haukex
in thread Scanning a file and extracting certains values within one or multiple lines
by Arengin
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |