in reply to extracting value in a string after checking value after colon ':'

Hello t-rex and welcome back!

To match a line in which nothing follows the colon (other than the terminal newline, of course), simply match on :$:

use strict; use warnings; while (<DATA>) { print "$1\n" if / ^ node \s+ (\d+) \s+ \w+ : $ /x; } __DATA__ available: 8 nodes (0,8,250-255) node 0 cpus: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 +2 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 4 +5 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 6 +8 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 node 0 size: 129733 MB node 0 free: 125997 MB node 8 cpus: 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 1 +05 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 12 +2 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 + 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 +157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 1 +74 175 node 8 size: 129826 MB node 8 free: 126395 MB node 250 cpus: node 250 size: 16128 MB node 250 free: 16127 MB node 251 cpus: node 251 size: 12032 MB node 251 free: 12031 MB node 252 cpus: node 252 size: 16128 MB node 252 free: 16127 MB node 253 cpus:

Output:

16:43 >perl 1971_SoPW.pl 250 251 252 253 16:44 >

But if whitespace is allowed after the colon, change to:

while (<DATA>) { print "$1\n" if / ^ node \s+ (\d+) \s+ \w+ : \s* $ /x; # ^^^ }

Update: Removed unnecessary chomp from the first code block.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: extracting value in a string after checking value after colon ':'
by t-rex (Scribe) on Jan 30, 2019 at 08:08 UTC

    Thanks

    but this is not working, i get error like

    Use of uninitialized value $_ in pattern match (m//) at gpu.pl line 8

      You will not get that if you run the code as written, so you have changed something. Post the full code you are running as an SSCCE if you would like assistance to debug further.

      Please show the code that gave you this Use of uninitialized value $_ warnings. It would not happen with the code as written by Athanasius , so you must have done something differently.

      secondly if you see the file, there are many lines, i only want to extract the node number (250,251..) only if string cpus: is empty after the colon.

      Hope i made my question clear

        i only want to extract the node number (250,251..) only if string cpus: is empty after the colon.
        That's seems to be exactly the output from Athanasius's code. Or, if it is not, then please explain in which respect Athanasius's code does not produce what you want. And also show the code you're using.

        How are you reading the lines from the file ?

        #!/usr/bin/perl use strict; use warnings; my $infile = 'infile.txt'; open my $fh,'<',$infile or die "Could not open $infile : $!"; my $lineno = 0; while (<$fh>){ ++$lineno; if (/^ node \s+ (\d+) \s+ \w+ : $ /x){ print "lineno $lineno : $1\n"; } } close $fh; print "$lineno lines read from $infile\n";
        poj