Alerting

Alert script : HOW TO fix the column order in the result.csv.gz

Micmac
Path Finder

Hi,

I actually execute a script when a schedule saved search is triggered.

My problem is :

I run multiple schedule saved search with the same number of fields and in result.csv.gz. I get this fields in random order in the result.gz.csv of the different saved searches ... this is a problem when I want to use the results.

Do you now if there is a way to fix the order of the column that we have in result.csv.gz ??

For more detail :

My saved searches end with the fields command like below :

[...]
| fields myfield_1,myfield_2,myfield_3,myfield_4

And sometimes I get in result.cvs.gz like below :

  • myfield_1,myfield_4,myfield_3,myfield_2,__mvfields...
  • myfield_1,myfield_2,myfield_3,myfield_4,__mvfields... (this is what I want all of the time)
  • myfield_1,__mvfield,myfield_2,_mvfield,myfield_3,_mvfield,myfield_4,__mvfield...

Thanks for your help.

0 Karma
1 Solution

Micmac
Path Finder

I've coded a workaround that works for the moment.

I've modified my script to get the position of the field that I want in the first line of the result.csv.gz header :

result_content=fResult.readlines()

if result_content:
  for line in result_content:
    aFields = line.split(",") 
    # get fields position
    if "__mv" in line: # the header line 
      iPos = 0
      for sField in aFields:
        if "__mv" not in sField:
          if "myfield_1" in sField:
            iPosfield_1 = iPos
          elif "myfield_2" in sField:
            iPosfield_2 = iPos
          [...]
        iPos += 1

I hope my solution will help ! If you have better please share 🙂

View solution in original post

Micmac
Path Finder

I've coded a workaround that works for the moment.

I've modified my script to get the position of the field that I want in the first line of the result.csv.gz header :

result_content=fResult.readlines()

if result_content:
  for line in result_content:
    aFields = line.split(",") 
    # get fields position
    if "__mv" in line: # the header line 
      iPos = 0
      for sField in aFields:
        if "__mv" not in sField:
          if "myfield_1" in sField:
            iPosfield_1 = iPos
          elif "myfield_2" in sField:
            iPosfield_2 = iPos
          [...]
        iPos += 1

I hope my solution will help ! If you have better please share 🙂

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Observability Simplified: Combining User Experience, Application Performance & ...

Tech Talk Observability Simplified: Combining User Experience, Application Performance & Network ...

Event Series May & June: From Network Visibility to Service Intelligence

Unifying the Network: Moving from Alert Noise to Service Intelligence with Splunk ITSI In today’s hybrid ...