Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i want to grep expected output between two string in patter? 1

Status
Not open for further replies.

tcltest

Programmer
Dec 12, 2011
12
US
set output "omppclb> error: fialed due to mismatch of values
Existed value : 20000
expected value: 40000
omppclb>"
from above veriable i wanted to get string between "omppclb "
can you please suggest me how to fetch the string between that?
 
Try this:
# this is your string
set x {omppclb> error: fialed due to mismatch of values Existed value : 20000 expected value: 40000 omppclb>}

# set variable to hold everything in between omppclb> and omppclb>:
set inBetween ""

# get your data:
regexp {omppclb>([^<]*)omppclb>} $x n inBetween

#print your result
puts $inBetween
 
no sir..
your solution not working

SHRIKANT BADIGER
SJCE MYSORE
 
I'm not sure if your string is on single line or on multiple lines.
For the latter case use -lineanchor option in regexp

This works for me in both cases:
Code:
[COLOR=#0000ff]# matching single line string[/color]
[COLOR=#804040][b]set[/b][/color] output [COLOR=#ff00ff]"omppclb> error: fialed due to mismatch of values Existed value : 20000 expected value: 40000 omppclb>"[/color]

[COLOR=#804040][b]set[/b][/color] result [[COLOR=#804040][b]regexp[/b][/color] {omppclb>\s*(.+)\s*omppclb>} [COLOR=#008080]$output[/color] match str_extr];

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$output[/color][COLOR=#ff00ff]'"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"result=[/color][COLOR=#008080]$result[/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$result[/color]} {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"This was matched:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$match[/color][COLOR=#ff00ff]'"[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String extracted:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$str_extr[/color][COLOR=#ff00ff]'"[/color]
}

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#0000ff]# matching multi line string[/color]
[COLOR=#804040][b]set[/b][/color] output [COLOR=#ff00ff]"omppclb> error: fialed due to mismatch of values[/color]
[COLOR=#ff00ff]                       Existed value : 20000[/color]
[COLOR=#ff00ff]                        expected value: 40000[/color]
[COLOR=#ff00ff]                        omppclb>"[/color]

[COLOR=#804040][b]set[/b][/color] result [COLOR=#ff0000]\[/color]
  [[COLOR=#804040][b]regexp[/b][/color] -lineanchor {omppclb>\s*(.+)\s*omppclb>} [COLOR=#008080]$output[/color] match str_extr];

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$output[/color][COLOR=#ff00ff]'"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"result=[/color][COLOR=#008080]$result[/color][COLOR=#ff00ff]"[/color]
[COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$result[/color]} {
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"This was matched:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$match[/color][COLOR=#ff00ff]'"[/color]
  [COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String extracted:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$str_extr[/color][COLOR=#ff00ff]'"[/color]
}
Output:
Code:
String to apply the pattern:
'omppclb> error: fialed due to mismatch of values Existed value : 20000 expected
 value: 40000 omppclb>'
result=1
This was matched:
'omppclb> error: fialed due to mismatch of values Existed value : 20000 expected
 value: 40000 omppclb>'
String extracted:
'error: fialed due to mismatch of values Existed value : 20000 expected value: 4
0000 '



String to apply the pattern:
'omppclb> error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        omppclb>'
result=1
This was matched:
'omppclb> error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        omppclb>'
String extracted:
'error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        '
 
t hanks its working but sometimes omppch root> will come instead of omppclb> so can u modify . i tried to modify and run but failed:

set output "omppclh root > error: mismatch in cells
expected cell:40000
existed cell :50000
omppclh root>

some times server may display omppclv4 root> instead of omppclh> can you plz help me.

SHRIKANT BADIGER
SJCE MYSORE
 
then change the reelvant part of code to
Code:
set result   [regexp -lineanchor {omppclb[COLOR=red]\s*[root]*[/color]>\s*(.+)\s*omppclb>} $output match str_extr];
 
I overlooked the characters omppclh and omppclv4. Maybe this does what you need:
Code:
[COLOR=#804040][b]proc[/b][/color] extract_string {str} {                       
  [COLOR=#804040][b]set[/b][/color] result [COLOR=#ff0000]\[/color]
  [[COLOR=#804040][b]regexp[/b][/color] -lineanchor [COLOR=#ff0000]\[/color]
    {omppcl(b|h|v4)\s*[root]*\s*>\s*(.+)\s*omppcl(b|h|v4)\s*[root]*\s*>} [COLOR=#ff0000]\[/color]
    [COLOR=#008080]$str[/color] match tmp str_extr];

  [COLOR=#804040][b]if[/b][/color] {[COLOR=#008080]$result[/color]} {
      [COLOR=#804040][b]return[/b][/color] [COLOR=#008080]$str_extr[/color]
  }
}

[COLOR=#0000ff]#------------------------------------------------------------------------[/color]
[COLOR=#0000ff]# matching single line string[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Matching single line string:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#804040][b]set[/b][/color] my_string [COLOR=#ff00ff]"omppclb> error: fialed due to mismatch of values Existed value : 20000 expected value: 40000 omppclb>"[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_string[/color][COLOR=#ff00ff]'"[/color]
[COLOR=#804040][b]set[/b][/color] my_substring [extract_string [COLOR=#008080]$my_string[/color]]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Substring extracted:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_substring[/color][COLOR=#ff00ff]'"[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#0000ff]#------------------------------------------------------------------------[/color]
[COLOR=#0000ff]# matching multi line strings[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Matching multi line strings:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#804040][b]set[/b][/color] my_string [COLOR=#ff00ff]"omppclb > error: fialed due to mismatch of values[/color]
[COLOR=#ff00ff]                       Existed value : 20000[/color]
[COLOR=#ff00ff]                        expected value: 40000[/color]
[COLOR=#ff00ff]                        omppclb>"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_string[/color][COLOR=#ff00ff]'"[/color]
[COLOR=#804040][b]set[/b][/color] my_substring [extract_string [COLOR=#008080]$my_string[/color]]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Substring extracted:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_substring[/color][COLOR=#ff00ff]'"[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#804040][b]set[/b][/color] my_string [COLOR=#ff00ff]"omppclh root > error: fialed due to mismatch of values[/color]
[COLOR=#ff00ff]                       Existed value : 20000[/color]
[COLOR=#ff00ff]                        expected value: 40000[/color]
[COLOR=#ff00ff]                        omppclh root>"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_string[/color][COLOR=#ff00ff]'"[/color]
[COLOR=#804040][b]set[/b][/color] my_substring [extract_string [COLOR=#008080]$my_string[/color]]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Substring extracted:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_substring[/color][COLOR=#ff00ff]'"[/color]

[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color]

[COLOR=#804040][b]set[/b][/color] my_string [COLOR=#ff00ff]"omppclv4 root> error: fialed due to mismatch of values[/color]
[COLOR=#ff00ff]                       Existed value : 20000[/color]
[COLOR=#ff00ff]                        expected value: 40000[/color]
[COLOR=#ff00ff]                        omppclv4 root >"[/color]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"String to apply the pattern:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_string[/color][COLOR=#ff00ff]'"[/color]
[COLOR=#804040][b]set[/b][/color] my_substring [extract_string [COLOR=#008080]$my_string[/color]]
[COLOR=#804040][b]puts[/b][/color] [COLOR=#ff00ff]"Substring extracted:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]'[/color][COLOR=#008080]$my_substring[/color][COLOR=#ff00ff]'"[/color]
Output:
Code:
Matching single line string:

String to apply the pattern:
'omppclb> error: fialed due to mismatch of values Existed value : 20000 expected
 value: 40000 omppclb>'
Substring extracted:
'error: fialed due to mismatch of values Existed value : 20000 expected value: 4
0000 '



Matching multi line strings:

String to apply the pattern:
'omppclb > error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        omppclb>'
Substring extracted:
'error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        '


String to apply the pattern:
'omppclh root > error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        omppclh root>'
Substring extracted:
'error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        '


String to apply the pattern:
'omppclv4 root> error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
                        omppclv4 root >'
Substring extracted:
'error: fialed due to mismatch of values
                       Existed value : 20000
                        expected value: 40000
 
thanks.. again our requirement changed..
now the ouput is in following format:

"omppclh> perl pcmd_verify.pl 2000 20000 2000 4567 343455 545
ERROR: cell mismatch:
expected cell: 10000
existed cell : 20000
omppclh>

now i want grep the output from ERROR
i don't want perl pcmd_verify.pl 2000 20000 2000 4567 343455 545
only i want to fetch
ERROR: cell mismatch:
expected cell: 10000
existed cell : 20000



plz help me


SHRIKANT BADIGER
SJCE MYSORE
 
ok.. thanks,,
i changed..its running properly..


SHRIKANT BADIGER
SJCE MYSORE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top