Search in paragraph's text
The goal is to verify the presence of a word or regular expression within several paragraphs of text (structured or not) and to print the relevant paragraphs on the standard output.
So here, let’s imagine that we are trying to verify the presence of a keyword "SystemError" within the paragraphs "Traceback (most recent call last):" in the file Traceback.txt
cat Traceback.txt : 2018-06-19 23:19:34,877 ERROR Processes plugin raised an exception. Traceback (most recent call last): File "/usr/lib/python3/dist-packages/landscape/sysinfo/sysinfo.py", line 99, in run result = plugin.run() File "/usr/lib/python3/dist-packages/landscape/sysinfo/processes.py", line 18, in run for process_info in info.get_all_process_info(): File "/usr/lib/python3/dist-packages/landscape/lib/process.py", line 39, in get_all_process_info process_info = self.get_process_info(process_id) File "/usr/lib/python3/dist-packages/landscape/lib/process.py", line 61, in get_process_info cmd_line = file.readline() File "/usr/lib/python3.6/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 152: ordinal not in range(128) 2018-06-19 23:19:34,877 ERROR Processes plugin raised an exception. Traceback (most recent call last): vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } [Tue Jan 21 16:16:19.250245 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] Traceback (most recent call last): [Tue Jan 21 16:16:19.252221 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] SystemError: unable to access /home/dir [Tue Jan 21 16:16:19.249067 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Failed to exec Python script file '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. [Tue Jan 21 16:16:19.249609 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Exception occurred processing WSGI script '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. [Tue Jan 21 17:16:19.250245 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] Traceback (most recent call last): [Tue Jan 21 17:16:19.250679 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] File "/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi", line 5, in <module> [Tue Jan 21 17:16:19.251735 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] from test import app as application [Tue Jan 21 17:16:19.252221 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] ImportError: cannot import name app 2021-04-23 17:13:14,425 ERROR Network plugin raised an exception. Traceback (most recent call last): File "/usr/lib/python3/dist-packages/landscape/sysinfo/sysinfo.py", line 99, in run result = plugin.run() File "/usr/lib/python3/dist-packages/landscape/sysinfo/network.py", line 36, in run device_info = self._get_device_info() File "/usr/lib/python3/dist-packages/landscape/lib/network.py", line 163, in get_active_device_info speed, duplex = get_network_interface_speed( File "/usr/lib/python3/dist-packages/landscape/lib/network.py", line 249, in get_network_interface_speed res = status_cmd.tostring() AttributeError: 'array.array' object has no attribute 'tostring' 11/01 18:24:57.726 ERROR| log:0072| post-test sysinfo error: 11/01 18:24:57.727 ERROR| traceback:0013| Traceback (most recent call last): 11/01 18:24:57.728 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/common_lib/log.py", line 70, in decorated_func 11/01 18:24:57.729 ERROR| traceback:0013| fn(*args, **dargs) 11/01 18:24:57.730 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/bin/base_sysinfo.py", line 286, in log_after_each_test 11/01 18:24:57.731 ERROR| traceback:0013| old_packages = set(self._installed_packages) 11/01 18:24:57.731 ERROR| traceback:0013| TypeError: 'NoneType' object is not iterable 12/01 19:24:57.726 ERROR| log:0072| post-test sysinfo error: 11/01 18:24:57.727 ERROR| traceback:0013| Traceback (most recent call last): 11/01 18:24:57.728 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/common_lib/log.py", line 70, in decorated_func 11/01 18:24:57.729 ERROR| traceback:0013| fn(*args, **dargs) 11/01 18:24:57.730 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/bin/base_sysinfo.py", line 286, in log_after_each_test 11/01 18:24:57.731 ERROR| traceback:0013| old_packages = set(self._installed_packages) 11/01 18:24:57.731 ERROR| traceback:0013| SystemError: no such file or directory Traceback (most recent call last): File "/usr/lib/vmware-vpx/vsan-health/pyMoVsan/VsanClusterPrototypeImpl.py", line 1492, in WaitForUpdateTask WaitForTask(task) File "/usr/lib/vmware-vpx/pyJack/pyVim/task.py", line 123, in WaitForTask raise task.info.error vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' }
The expected result must be (with ---------------- for paragraphs matched's sep) :
Traceback (most recent call last): vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } ---------------- Traceback (most recent call last): [Tue Jan 21 16:16:19.252221 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] SystemError: unable to access /home/dir [Tue Jan 21 16:16:19.249067 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Failed to exec Python script file '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. [Tue Jan 21 16:16:19.249609 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Exception occurred processing WSGI script '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. ---------------- Traceback (most recent call last): 11/01 18:24:57.728 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/common_lib/log.py", line 70, in decorated_func 11/01 18:24:57.729 ERROR| traceback:0013| fn(*args, **dargs) 11/01 18:24:57.730 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/bin/base_sysinfo.py", line 286, in log_after_each_test 11/01 18:24:57.731 ERROR| traceback:0013| old_packages = set(self._installed_packages) 11/01 18:24:57.731 ERROR| traceback:0013| SystemError: no such file or directory ---------------- Traceback (most recent call last): File "/usr/lib/vmware-vpx/vsan-health/pyMoVsan/VsanClusterPrototypeImpl.py", line 1492, in WaitForUpdateTask WaitForTask(task) File "/usr/lib/vmware-vpx/pyJack/pyVim/task.py", line 123, in WaitForTask raise task.info.error vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } ----------------
AWK
Using the awk "Record Separator" : <lang awk>
awk -v ORS='\n\n' '/SystemError/ { print RS $0 }' RS="Traceback" Traceback.txt |\ awk -v ORS='\n----------------\n' '/Traceback/' RS="\n\n"
</lang> Note : 1st awk is used to search for an expression (regular or not) within paragraphs, and 2nd awk is used for formatting
J
Implementation:
<lang J>NB. read file, separate on blank lines paragraphs=: ((LF,LF)&E. <;.2 ])fread 'traceback.txt'
NB. ignore text preceeding Traceback (most recent call last) cleaned=: {{y}.~{.I.'Traceback (most recent call last)'&E.y}}each paragraphs
NB. limit to paragraphds containing SystemError searched=: (#~ (1 e.'SystemError'&E.)every) cleaned
NB. add paragraph separator and display echo ;searched ,L:0 '----------------',LF</lang>
- Output:
Traceback (most recent call last): vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } ---------------- Traceback (most recent call last): [Tue Jan 21 16:16:19.252221 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] SystemError: unable to access /home/dir [Tue Jan 21 16:16:19.249067 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Failed to exec Python script file '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. [Tue Jan 21 16:16:19.249609 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Exception occurred processing WSGI script '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. ---------------- Traceback (most recent call last): 11/01 18:24:57.728 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/common_lib/log.py", line 70, in decorated_func 11/01 18:24:57.729 ERROR| traceback:0013| fn(*args, **dargs) 11/01 18:24:57.730 ERROR| traceback:... ---------------- Traceback (most recent call last): File "/usr/lib/vmware-vpx/vsan-health/pyMoVsan/VsanClusterPrototypeImpl.py", line 1492, in WaitForUpdateTask WaitForTask(task) File "/usr/lib/vmware-vpx/pyJack/pyVim/task.py", line 123, in WaitForTask raise task.info.error vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } ---------------- </lang> =={{header|Phix}}== <!--<lang Phix>(phixonline)--> <span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">--constant text = get_text("Traceback.txt") -- (not js!)</span> <span style="color: #008080;">constant</span> <span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""" 2018-06-19 23:19:34,877 ERROR Processes plugin raised an exception. <snip> }"""</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">paras</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n\n"</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">tmrcl</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Traceback (most recent call last)"</span> <span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">paras</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #004080;">string</span> <span style="color: #000000;">para</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">paras</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">tdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmrcl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">para</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">if</span> <span style="color: #000000;">tdx</span> <span style="color: #008080;">then</span> <span style="color: #000000;">para</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">para</span><span style="color: #0000FF;">[</span><span style="color: #000000;">tdx</span><span style="color: #0000FF;">..$]</span> <span style="color: #008080;">if</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"SystemError"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">para</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n----------------\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">para</span><span style="color: #0000FF;">})</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #008080;">end</span> <span style="color: #008080;">for</span> <!--</lang>--> Output as requested =={{header|Raku}}== Just use the same technique as already thoroughly explored in tasks [[Retrieve_and_search_chat_history#Raku|Retrieve and search chat history]] and [[XXXX_redacted#Raku|XXXX redacted]] among many others. <lang perl6>.say if $paragraph.contains: 'whatever';</lang> =={{header|Wren}}== {{libheader|Wren-pattern}} <lang ecmascript>import "io" for File import "./pattern" for Pattern var fileName = "Traceback.txt" var p1 = Pattern.new("Traceback (most recent call last):") var p2 = Pattern.new("SystemError") var sep = "----------------" File.read(fileName) .split("\n\n") .where { |para| p1.isMatch(para) && p2.isMatch(para) } .each { |para| var ix = p1.find(para).index System.print(para[ix..-1]) System.print(sep) }</lang> {{out}} <pre> Traceback (most recent call last): vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } ---------------- Traceback (most recent call last): [Tue Jan 21 16:16:19.252221 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] SystemError: unable to access /home/dir [Tue Jan 21 16:16:19.249067 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Failed to exec Python script file '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. [Tue Jan 21 16:16:19.249609 2020] [wsgi:error] [pid 6515:tid 3041002528] [remote 10.0.0.12:50757] mod_wsgi (pid=6515): Exception occurred processing WSGI script '/home/pi/RaspBerryPiAdhan/www/sysinfo.wsgi'. ---------------- Traceback (most recent call last): 11/01 18:24:57.728 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/common_lib/log.py", line 70, in decorated_func 11/01 18:24:57.729 ERROR| traceback:0013| fn(*args, **dargs) 11/01 18:24:57.730 ERROR| traceback:0013| File "/tmp/sysinfo/autoserv-0tMj3m/bin/base_sysinfo.py", line 286, in log_after_each_test 11/01 18:24:57.731 ERROR| traceback:0013| old_packages = set(self._installed_packages) 11/01 18:24:57.731 ERROR| traceback:0013| SystemError: no such file or directory ---------------- Traceback (most recent call last): File "/usr/lib/vmware-vpx/vsan-health/pyMoVsan/VsanClusterPrototypeImpl.py", line 1492, in WaitForUpdateTask WaitForTask(task) File "/usr/lib/vmware-vpx/pyJack/pyVim/task.py", line 123, in WaitForTask raise task.info.error vmodl.fault.SystemError: (vmodl.fault.SystemError) { dynamicType = , dynamicProperty = (vmodl.DynamicProperty) [], msg = 'A general system error occurred: Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.', faultCause = , faultMessage = (vmodl.LocalizableMessage) [], reason = 'Unable to complete Sysinfo operation. Please see the VMkernel log file for more details.: Sysinfo error: Bad parameterSee VMkernel log for details.' } ----------------