Splunk Search

How do I remove all special characters from all field names?

Jason
Motivator

I have a CSV with numerous fields with bad field names. They have spaces and special characters such as up and down arrows.

I don't know ahead of time what the field names will be.

How do I locate and rename all of them to more "safe" Splunk field names that work easily in all Splunk commands without funky syntax?

Labels (2)
0 Karma
1 Solution

Jason
Motivator

Use the power of foreach to loop through the different fields, combined with | eval {a}=b syntax to create a field whose name is the value of  a and value is the value of b.

 

Here is my search. As all my problem fields had spaces in them, I aimed foreach just at field names with spaces ("* *").

 

This copy has comments to explain each part, and the one below is comment-free:

 

``` in every event/result row, loop over all field names with spaces in them ```
| foreach "* *" [ 
    ``` create a temporary string with the name of the field to be renamed ```
    eval x="<<FIELD>>", 
    ``` tidy up the field name by doing string substitution - put any regular expression in the center. (This one drops all characters that are not spaces or in Splunk's set of allowable characters.) First it does a regex replace, then trims leading/trailing spaces, then replaces all remaining spaces with underscores ```
      y=replace(trim(replace(x, "[^A-Za-z0-9_ ]", "")), " ", "_"), 
    ``` create a new field with the tidied name and the value of the original - use single quotes because <<FIELD>> has spaces and special characters, and we need to get its value (single quotes), not its name (double quotes). ```
      {y}='<<FIELD>>' 
  ``` remove the temporary fields and the poorly-named original field ```
  | fields - x y "<<FIELD>>" ]

 

 

Clean version:

 

| foreach "* *" [ eval x="<<FIELD>>", y=replace(trim(replace(x, "[^A-Za-z0-9_ ]", "")), " ", "_"), {y}='<<FIELD>>' | fields - x y "<<FIELD>>" ]

 

 

Another improvement/variant you can do is: before running the inner "replace all special characters with nothing", you could "replace all dot and dash to underscore" - then continue with the rest as written.

 

View solution in original post

0 Karma

Jason
Motivator

Use the power of foreach to loop through the different fields, combined with | eval {a}=b syntax to create a field whose name is the value of  a and value is the value of b.

 

Here is my search. As all my problem fields had spaces in them, I aimed foreach just at field names with spaces ("* *").

 

This copy has comments to explain each part, and the one below is comment-free:

 

``` in every event/result row, loop over all field names with spaces in them ```
| foreach "* *" [ 
    ``` create a temporary string with the name of the field to be renamed ```
    eval x="<<FIELD>>", 
    ``` tidy up the field name by doing string substitution - put any regular expression in the center. (This one drops all characters that are not spaces or in Splunk's set of allowable characters.) First it does a regex replace, then trims leading/trailing spaces, then replaces all remaining spaces with underscores ```
      y=replace(trim(replace(x, "[^A-Za-z0-9_ ]", "")), " ", "_"), 
    ``` create a new field with the tidied name and the value of the original - use single quotes because <<FIELD>> has spaces and special characters, and we need to get its value (single quotes), not its name (double quotes). ```
      {y}='<<FIELD>>' 
  ``` remove the temporary fields and the poorly-named original field ```
  | fields - x y "<<FIELD>>" ]

 

 

Clean version:

 

| foreach "* *" [ eval x="<<FIELD>>", y=replace(trim(replace(x, "[^A-Za-z0-9_ ]", "")), " ", "_"), {y}='<<FIELD>>' | fields - x y "<<FIELD>>" ]

 

 

Another improvement/variant you can do is: before running the inner "replace all special characters with nothing", you could "replace all dot and dash to underscore" - then continue with the rest as written.

 

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...