[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

extract data from notepad - perl

can u give me a script for the following?

1.txt
___________________________________________
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXBBBBBBBBBBB
WWWWWOOOOONNNNNNNNNNNN
WwwIIIIIOOOOOOO |quote= "pravin"| SSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSS |/quote| UUUWWWDDDDDDD
XXXWWXXXXXXBBBBXXXBBBBBBB
WWWWWOOSSSSSSSSS
SSSSSSSSS
___________________________________________

Required OUTPUT:

SSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSS
0
pravink22
Asked:
pravink22
1 Solution
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Do your require that perl is used? I'd use a simple grep to get your output:

grep -o [S]* 1.txt

Open in new window


Output:

SSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSSS
SSSSSSSSSSSSSSSSSSSS
SSSSSSSSSSS
SSSSSSSSS
SSSSSSSSS

Open in new window

0
 
pravink22Author Commented:
Hi,

My requirement is to grep data present in between

|quote= "pravin"| to  |/quote|

Thanks
0
 
ozoCommented:
perl  -0777 -pe '($_)=/\|quote= "pravin"\|(.*?)\|\/quote\|/s'  1.txt
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
pravink22Author Commented:
can you write the full program?
0
 
tel2Commented:
Hi pravink22,

Here's one way of rewriting ozo's on-liner as a full script:

    #!/usr/bin/perl
    undef $/;
    $_ = <>;
    ($_) = /\|quote= "pravin"\|\s*(.*?)\s*\|\/quote\|/s;
    print "$_\n";

However, there are some practical differences between this and ozo's, i.e.:
1. The above removes any spaces from the beginning and end of the output.  If you don't want to do that, remove the 2 instances of "\s*".
2. This above puts a newline character at the end of the output.  If you don't want this, change the last line to:
    print $_;
or just:
    print;
since $_ is the default print argument.

If you're going to have other code in the same script after the above code, then it would be safer to do something like this:

    #!/usr/bin/perl
    {
        local $/ = undef;
        $_ = <>;
        ($_) = /\|quote= "pravin"\|\s*(.*?)\s*\|\/quote\|/s;
        print "$_\n";
    }
    # Other code here...

otherwise your input separator ($/) will still be undefined afterwards, which may cause problems in any subsequent code.

If you do accept this answer, remember to give ozo some points, since this is based on his one-liner.

tel2
0
 
tel2Commented:
Thanks for the points, pravink.

Probably some of the points should have gone to others (especially ozo, who needs them so badly), so bear that in mind for the future.  As I had indicated, my scripts were based on ozo's one-liner.  I assume you know how to split points among multiple experts?

tel2
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now