<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Only first line of python script is getting executed in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241392#M175889</link>
    <description>&lt;P&gt;Thanks DalJeanis&lt;BR /&gt;
This is just part of the code to show what I was doing&lt;BR /&gt;
In fact in the final code ,  I did add test print lines in middle to see if its printing but its not happening&lt;BR /&gt;
But otherwise, apart from the code , is there anything else I am doing wrong with respect to the process of placing the script in the folder, running the script etc&lt;/P&gt;</description>
    <pubDate>Thu, 19 Jan 2017 06:34:04 GMT</pubDate>
    <dc:creator>hkmurali</dc:creator>
    <dc:date>2017-01-19T06:34:04Z</dc:date>
    <item>
      <title>Only first line of python script is getting executed</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241390#M175887</link>
      <description>&lt;P&gt;I had placed a python script in the 'C:\Program Files\Splunk\etc\apps\search\bin\parsing.py'&lt;BR /&gt;
This is a sample of my code.&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;print("Test Message for log1") import&lt;BR /&gt;
numpy lines = [] # Declare an empty&lt;BR /&gt;
list named "lines" i=0 end_position=[]&lt;BR /&gt;
with open ('C:\Program&lt;BR /&gt;
Files\Splunk\20161129_0000.log', 'rt')&lt;BR /&gt;
as in_file: &lt;BR /&gt;
    for line in in_file: # For each line of text in in_file, where the&lt;BR /&gt;
data is named "line",&lt;BR /&gt;
        lines.append(line.rstrip('\n')) # add&lt;BR /&gt;
that line to our list of lines,&lt;BR /&gt;
stripping newlines.&lt;BR /&gt;
        if "-----------------------------------------------------------------" in line:&lt;BR /&gt;
            end_position.append(i)&lt;BR /&gt;
        i=i+1&lt;/P&gt;

&lt;H1&gt;Store all the positions of the hyphens to find out the ending lines&lt;/H1&gt;

&lt;P&gt;for each of the table&lt;BR /&gt;
end_position=numpy.array(end_position)&lt;BR /&gt;
total_lines=i&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;But when I add this script in splunk and hit search, only the first line "Test message for log 1" is getting printed out&lt;BR /&gt;
The rest of the lines are not getting executed. Kindly help&lt;BR /&gt;
All i'm getting is this:&lt;/P&gt;

&lt;P&gt;Time                                             Event&lt;BR /&gt;
1/17/17 2:56:15.000 PM  Test Message for log1&lt;BR /&gt;
host =  ATA1CJSKCR source = C:\Program Files\Splunk\etc\apps\search\bin\parse.py sourcetype =parsing&lt;BR /&gt;
&lt;STRONG&gt;Thanks. Kindly help&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:28:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241390#M175887</guid>
      <dc:creator>hkmurali</dc:creator>
      <dc:date>2020-09-29T12:28:07Z</dc:date>
    </item>
    <item>
      <title>Re: Only first line of python script is getting executed</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241391#M175888</link>
      <description>&lt;P&gt;I'm pretty sure it's your code, and this is a python rather than a splunk question.  &lt;/P&gt;

&lt;P&gt;You need to post the code with the correct line breaks and indentation, since python syntax is critically dependent on indentation rather than block identifiers present in other languages (END-IFs, curly braces and so on).&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;If your code is actually all jumbled up like that, then that's the problem.&lt;/STRONG&gt;  &lt;/P&gt;

&lt;P&gt;I've pulled out the comments and lined up where I believe the code breaks, and it looks something like this -&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;print("Test Message for log1") 
import numpy 
lines = [] 
i=0 
end_position=[]
with open ('C:\Program Files\Splunk\20161129_0000.log', 'rt') as in_file: 
   for line in in_file: 
       lines.append(line.rstrip('\n')) 
       if "-----------------------------------------------------------------" in line:
          end_position.append(i)
       i=i+1

end_position=numpy.array(end_position)
total_lines=i
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now, from looking at that restructured code, I don't see how you would know whether the rest of the lines are being executed.  You are not actually attempting to print out any other diagnostics, and i can't tell without the function wrapper what values are supposed to be passed out.  &lt;/P&gt;

&lt;P&gt;Try placing a few more test lines like  print("Now counting another Dash line")  and  print("Total lines is now {0}", format(total_lines)) and see what you can find out.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 00:15:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241391#M175888</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-01-19T00:15:02Z</dc:date>
    </item>
    <item>
      <title>Re: Only first line of python script is getting executed</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241392#M175889</link>
      <description>&lt;P&gt;Thanks DalJeanis&lt;BR /&gt;
This is just part of the code to show what I was doing&lt;BR /&gt;
In fact in the final code ,  I did add test print lines in middle to see if its printing but its not happening&lt;BR /&gt;
But otherwise, apart from the code , is there anything else I am doing wrong with respect to the process of placing the script in the folder, running the script etc&lt;/P&gt;</description>
      <pubDate>Thu, 19 Jan 2017 06:34:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Only-first-line-of-python-script-is-getting-executed/m-p/241392#M175889</guid>
      <dc:creator>hkmurali</dc:creator>
      <dc:date>2017-01-19T06:34:04Z</dc:date>
    </item>
  </channel>
</rss>

