Obtaining lines from an array and stopping at a particular character

user2572835

I'm looking for some assistance on a script which can go through the lines of an array, printing them to the screen and stopping when the script detects a specific character, in this case the ! mark. I have tried using the foreach statement but ain't getting any success...

Example of the Array (@lines) contents is:

ip vrf test
rd 2856:10000331
export map SetAltMgmtRT
route-target export 2856:10000331
route-target import 2856:10000331
maximum routes 1000 75
!

The script I have so far is:

elsif ( $action eq "show_vrf" ) {
            my $cmd = "show run | begin <VRF_NAME>";
            $cmd = $cmd . " | i $include" if($include) ;
            my @lines = $s->cmd(String => $cmd,
                            Prompt  => "/$enableprompt/",
                            Timeout => 10);
            foreach $lines (@lines) {
                    <statement, this is where I am stuck>
            }
            print $lines;

Any help would be appreciated :)

plusplus

What is the criteria for stopping? Any exclamation mark or just one on its own line? Or just a line beginning with an exclamation mark?

You've also got several things called lines which you need to sort out.

my $output;
foreach my $line (@lines) {
    last if $line =~ m/^!/; # leave loop if line starts with an exclamation mark
    $output .= $line;
}
print $output;

For your additional requirements in your comment below (the data has more than one exclamation mark) you'd need something like this:

use Data::Dumper;
my @output; # assign output chunks into an array
my $i = 0;
foreach my $line (@lines) {
    if ($line =~ m/^!/) {
        $i++;
        next;
    }
    $output[$i] .= $line;
}
print Dumper(\@output);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to find and replace, skipping lines containing a particular character?

分類Dev

Issues with structure arrays and obtaining array string from user integer input

分類Dev

Counting the particular words from array in other array

分類Dev

How to remove particular words from lines of a text file?

分類Dev

How to delete particular no of elements from array in mongodb

分類Dev

How to get a particular attribute from an array of array objects?

分類Dev

GREP lines from file in another file until occurence of a certain character

分類Dev

Printing a selected number from lines stored in an array

分類Dev

Ruby - Extract value of a particular key from array of hashes

分類Dev

how to get a particular object value from nested json array

分類Dev

Fetch particular line content from file stored in an array

分類Dev

Copying elements from one character array to another

分類Dev

Obtaining image from camera intent on Android

分類Dev

ACF: Obtaining info in vars from nested group

分類Dev

Obtaining date from JDateChooser and displaying it in JTextField

分類Dev

Convert date to character in particular format In R

分類Dev

stopping mousewheel event from happening twice in OSX

分類Dev

Stopping kids from using a machine for illegal activites?

分類Dev

Titanium - prevent exitOnClose from stopping the app

分類Dev

C++: "Multi-character character constant" warning when reading from a file into char array

分類Dev

Make a unique list of values from a particular key existing anywhere in a deep array

分類Dev

Make a unique list of values from a particular key existing anywhere in a deep array

分類Dev

Obtaining the fully qualified name from an Annotation using Eclipse JDT AST

分類Dev

obtaining groovy value from map given computed key for jenkins?

分類Dev

Obtaining the QAST of a Perl 6 file from another program

分類Dev

Obtaining an int value of an object from an arrayList containing objects in jsp

分類Dev

Run animation from particular step

分類Dev

Remove bracket from a particular string

分類Dev

Sed delete empty lines after a particular line number

Related 関連記事

  1. 1

    How to find and replace, skipping lines containing a particular character?

  2. 2

    Issues with structure arrays and obtaining array string from user integer input

  3. 3

    Counting the particular words from array in other array

  4. 4

    How to remove particular words from lines of a text file?

  5. 5

    How to delete particular no of elements from array in mongodb

  6. 6

    How to get a particular attribute from an array of array objects?

  7. 7

    GREP lines from file in another file until occurence of a certain character

  8. 8

    Printing a selected number from lines stored in an array

  9. 9

    Ruby - Extract value of a particular key from array of hashes

  10. 10

    how to get a particular object value from nested json array

  11. 11

    Fetch particular line content from file stored in an array

  12. 12

    Copying elements from one character array to another

  13. 13

    Obtaining image from camera intent on Android

  14. 14

    ACF: Obtaining info in vars from nested group

  15. 15

    Obtaining date from JDateChooser and displaying it in JTextField

  16. 16

    Convert date to character in particular format In R

  17. 17

    stopping mousewheel event from happening twice in OSX

  18. 18

    Stopping kids from using a machine for illegal activites?

  19. 19

    Titanium - prevent exitOnClose from stopping the app

  20. 20

    C++: "Multi-character character constant" warning when reading from a file into char array

  21. 21

    Make a unique list of values from a particular key existing anywhere in a deep array

  22. 22

    Make a unique list of values from a particular key existing anywhere in a deep array

  23. 23

    Obtaining the fully qualified name from an Annotation using Eclipse JDT AST

  24. 24

    obtaining groovy value from map given computed key for jenkins?

  25. 25

    Obtaining the QAST of a Perl 6 file from another program

  26. 26

    Obtaining an int value of an object from an arrayList containing objects in jsp

  27. 27

    Run animation from particular step

  28. 28

    Remove bracket from a particular string

  29. 29

    Sed delete empty lines after a particular line number

ホットタグ

アーカイブ