1

I have a file in below mentioned text format: cat test.txt

"perl-Test::DNS" : [
         {
            "environment" : "test1",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test1-Test::DNS",
         },
         {
            "environment" : "Test2",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test2-Test::DNS",
         },
         {
            "environment" : "Test3",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test3-Test::DNS",
         },
]

"perl-Test::SSH" : [
         {
            "environment" : "test1",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test1-Test::SSH",
         },
         {
            "environment" : "Test2",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test2-Test::SSH",
         },
         {
            "environment" : "Test3",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test3-Test::SSH",
         },
],

I need to grep particular string i.e "perl-Test::SSH : [" from file and print/extract whole lines between '[' and ']' of that string .

I found similar type of question here: Perl: How to extract a string between brackets but this link only extract words between two brackets and i need to extract lines.

Anything that works will be accepted but explanations would help greatly.

Community
  • 1
  • 1

1 Answers1

0

You can do bracket capturing, but it's messy when you don't have to. (JSON is slightly easier to handle than XML but it's still not a good idea).

This however, looks like it might be YAML - it's pretty close - I only have to remove a trailing comma from your source, which I have assumed is because you've given us a sample of your config.

Loading YAML looks a bit like this:

#!/usr/bin/env perl

use strict;
use warnings;
use Data::Dumper;

use YAML::XS; 

my $yaml = Load ( do { local $/; <DATA> } );
print Dumper $yaml -> {"perl-Test::DNS"};


__DATA__
"perl-Test::DNS" : [
         {
            "environment" : "test1",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test1-Test::DNS",
         },
         {
            "environment" : "Test2",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test2-Test::DNS",
         },
         {
            "environment" : "Test3",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test3-Test::DNS",
         },
]

"perl-Test::SSH" : [
         {
            "environment" : "test1",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test1-Test::SSH",
         },
         {
            "environment" : "Test2",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test2-Test::SSH",
         },
         {
            "environment" : "Test3",
            "hash" : "c8d149b4fc895b214276ca5c90d1181e",
            "identifier" : "test3-Test::SSH",
         },
]

As you can see - it correctly parses your file aside from that trailing comma. (Which would work too, provided you had additional entries after it). The above outputs:

$VAR1 = [
          {
            'environment' => 'test1',
            'hash' => 'c8d149b4fc895b214276ca5c90d1181e',
            'identifier' => 'test1-Test::DNS'
          },
          {
            'hash' => 'c8d149b4fc895b214276ca5c90d1181e',
            'environment' => 'Test2',
            'identifier' => 'test2-Test::DNS'
          },
          {
            'hash' => 'c8d149b4fc895b214276ca5c90d1181e',
            'environment' => 'Test3',
            'identifier' => 'test3-Test::DNS'
          }
        ];

But which is a 'normal' perl data structure that you can traverse as you wish.

Sobrique
  • 51,581
  • 6
  • 53
  • 97
  • it's not a YAML or XML file , it is a '.conf' file format – – user3498040 Feb 04 '16 at 13:25
  • The file extension is irrelevant. What's in it is what matters. You would have to be extremely lucky to find - as your file is - that it matches the YAML spec quite as closely as that. – Sobrique Feb 04 '16 at 13:26