noobi
noobi

Reputation: 97

extract 1st line from a text with special pattern using regexp

I have a string

set text {tools show stats 
Resource Manager info at 023 h 06/01/20 09:14:16.105:

Hardware Resource Usage #1, abc iom-32-100g, Pipe #0:
                                 |   Total   | Allocated |    Free    
  -------------------------------+-----------+-----------+------------
            stat vale abc/ghj(0) |        256|          0|        256
            stat vale abc/ghj(1) |        256|          0|        256

                   new Statistic |    Count    
---------------------------------+-------------
                           first |         0
                          seconds|         0

                      third info |    Count    
                           fourth|         0
                           fifth |       125

     Forwarding Table Statistics |   Used /  Free  /  Max  /  Reject  
---------------------------------+-----------------------------------
              third stat Entries |      2 /  36856 /  36864
                      Important  |      1 /  13133 /  13136 / 0
                fifth entry < 56 |      1 /   5743 /   5744 / 0
                sixth entry > 5 |      0 /    112 /    112 / 0

Service manager resources
-------------------------
Total count:           2/  3072
Total info:            2/   401

ABC Group Statistics |   Used /  Free  / Max
---------------------------------+-----------------------
              Entries |      0 /    256 / 256
}

I have to extract

1. Important  |      1 /  13133 /  13136 / 0
2. fifth entry < 56 |      1 /   5743 /   5744 / 0

both can be done separetly I'm using the following regex in tcl to extract the same

1. regexp -nocase -line -- {^Important.*} $text match
2. regexp -nocase -line -- {^fifth entry \< 56.*} $text match

its not working, can someone help here ?

Upvotes: 3

Views: 77

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

You have whitespace before the matches, the lines are indented, so you need to match the whitespace, say with [[:space:]]* that matches both regular spaces and tabs.

Or, just remove ^ if you do not need to match at the start of the line. ^ matches a position at the start of a line.

To "exclude" it from the match, capture the text you need without the initial whitespaces using a pair of round brackets.

You may use

regexp -nocase -line -- {^[[:space:]]*(Important.*)} $text - match1
regexp -nocase -line -- {^[[:space:]]*(fifth entry < 56.*)} $text - match2

See the Tcl online demo

Note you do not have to escape < if you want to match a literal < char.

Details

  • ^ - start of a line (here, due to -line)
  • [[:space:]]* - any 0 or more whitespaces
  • (Important.*) - Capturing group 1: Important and any 0 or more chars other than line break chars.

Upvotes: 2

Jerry
Jerry

Reputation: 71538

  1. regexp -nocase -line -- {^Important.*} $text match

You have spaces before Important, so you should add them to your expression like thus:

regexp -nocase -line -- {^ *Important.*} $text match
  1. regexp -nocase -line -- {^fifth entry \< 56.*} $text match

Same here (and < does not need to be escaped):

regexp -nocase -line -- {^ *fifth entry < 56.*} $text match

Upvotes: 3

Related Questions