Splunk Search

Length of every column in a table?

bofasplunkguy
Explorer

I have a table with ~50 columns. I am doing an addcoltotals on the table, but this only adds up the numeric fields. Can someone please suggest an elegant way to take the length of every field in the table so addcoltotals populates for each column? Right now, the best I can think of is:

|eval field1Len = len(field1)
| eval field2len = len(field2)
...
|eval field50len = len(field50)

Would love a len(*) sort of approach if possible. Ideally I won't have to rewrite 50 eval statements. Hoping to build a sort of "checksum" so that we can measure table updates without looking through the rows. Any ideas appreciated!

0 Karma
1 Solution

vasanthmss
Motivator

Hi Bofasplunkguy,

Try using foreach command. This will save multiple eval statements..

... your base search  | foreach field* [| eval <<FIELD>>_Len=len(<<FIELD>>)] 

Description:

field* will iterate the all the field1, field2..
<<FIELD>> will be the actual field field1, field2 ...
<<FIELD>>_Len will be a new field with the eval len() accordingly.

Foreach syntax,

 foreach <wc-field>... [fieldstr=<string>] [matchstr=<string>] [matchseg1=<string>] [matchseg2=<string>] [matchseg3=<string>] <subsearch> 

For more details https://docs.splunk.com/Documentation/SplunkCloud/7.2.7/SearchReference/Foreach

Hope this helps you

Cheerss!!!

V

View solution in original post

sandeepmakkena
Contributor

You can use for loop,

 index=****
    | table ColumnName
    | foreach ColumnName
        [eval length =len(ColumnName)]
    | table ColumnName, length

Hope this will help. Thanks!

vasanthmss
Motivator

Hi Bofasplunkguy,

Try using foreach command. This will save multiple eval statements..

... your base search  | foreach field* [| eval <<FIELD>>_Len=len(<<FIELD>>)] 

Description:

field* will iterate the all the field1, field2..
<<FIELD>> will be the actual field field1, field2 ...
<<FIELD>>_Len will be a new field with the eval len() accordingly.

Foreach syntax,

 foreach <wc-field>... [fieldstr=<string>] [matchstr=<string>] [matchseg1=<string>] [matchseg2=<string>] [matchseg3=<string>] <subsearch> 

For more details https://docs.splunk.com/Documentation/SplunkCloud/7.2.7/SearchReference/Foreach

Hope this helps you

Cheerss!!!

V

bofasplunkguy
Explorer

Thank you! This worked and my code is way cleaner. I added quotation marks in the eval command portion like the docs recommend just to be safe:

| foreach * [eval <>Len=len('<>')]

Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...