Splunk Search

How to sort a field list which below with an uppercase letter followed by "- N"?

jip31
Motivator

hi

I need to sort a field list which below with an uppercase letter followed by "- N"

How to do please?

Tags (1)
0 Karma
1 Solution

yuanliu
SplunkTrust
SplunkTrust

Even so, @VatsalJagani's method should still work, e.g.,

 

| eval order=case(match(KPI, "^[A-Z].*- Nb"), 0, match(KPI, "^[a-z]"), 1, match(KPI, "^[A-Z]"), 2)
| sort order, KPI
| fields - order

 

 Using the sample input you listed, output is

KPI
CAP CXX - Nb d'erreurs
CAP CXX - Nb d'utilisateurs
DOSSIER XX - Nb d'erreurs
DOSSIER XX - Nb d'utilisateurs
SHARE - Nb d'erreurs
SHARE - Nb d'utilisateurs
WEST UNITED - Nb d'utilisateurs
WEST UNTED- Nb d'erreurs
EDGE - Crash
Nb d'utilisateurs ABC
Nb d'utilisateurs CDE
Nb d'utilisateurs EFG
Signal
Vélocité RTT (% de CPU)

View solution in original post

0 Karma

VatsalJagani
SplunkTrust
SplunkTrust

@jip31 How about the below query:

| makeresults | eval data="abc
C - N
A - N
def
B - N
ghi" | eval data=split(data,"
") | mvexpand data

| eval is_capital=if(match(data,"^[A-Z].*"), 1, 0)
| sort is_capital, data
| fields - is_capital

 

@richgalloway - Sorry I steal makeresults part of your query. 😀

somesoni2
Revered Legend

Please provide some sample values and corresponding expected output.

0 Karma

jip31
Motivator

Hi

I put an example of my need in one post

0 Karma

jip31
Motivator

Hi

Sorry it doesnt works but i need to share more explanations

The fields I need to sort correspond to the column-name of a transpose command

| transpose 0 header_field=time column_name=KPI

Nb d'utilisateurs CDE		
CAP CXX - Nb d'erreurs		
CAP CXX - Nb d'utilisateurs 			
DOSSIER XX - Nb d'erreurs		
DOSSIER XX - Nb d'utilisateurs 			
Nb d'utilisateurs ABC		
Nb d'utilisateurs EFG					
SHARE - Nb d'erreurs		
SHARE - Nb d'utilisateurs	
Signal 		
EDGE - Crash		
Vélocité RTT (% de CPU)		
WEST UNTED- Nb d'erreurs		
WEST UNITED - Nb d'utilisateurs

Here is a example of my fields not sorted 

first, I need to display the field which begin with an uppercase folowed by - N

after i nedd to sort the field with begin with a lowercase

And at the end I need to sort the fields which begins with an uppercase but followed by something else than "- N"

 

CAP CXX - Nb d'erreurs
CAP CXX - Nb d'utilisateurs
DOSSIER XX - Nb d'erreurs
DOSSIER XX - Nb d'utilisateurs
SHARE - Nb d'erreurs
SHARE - Nb d'utilisateurs
WEST UNTED- Nb d'erreurs
WEST UNITED - Nb d'utilisateurs
Nb d'utilisateurs ABC
Nb d'utilisateurs CDE
Nb d'utilisateurs EFG
Signal
Vélocité RTT (% de CPU)
EDGE - Crash

 which di 

0 Karma

yuanliu
SplunkTrust
SplunkTrust

Even so, @VatsalJagani's method should still work, e.g.,

 

| eval order=case(match(KPI, "^[A-Z].*- Nb"), 0, match(KPI, "^[a-z]"), 1, match(KPI, "^[A-Z]"), 2)
| sort order, KPI
| fields - order

 

 Using the sample input you listed, output is

KPI
CAP CXX - Nb d'erreurs
CAP CXX - Nb d'utilisateurs
DOSSIER XX - Nb d'erreurs
DOSSIER XX - Nb d'utilisateurs
SHARE - Nb d'erreurs
SHARE - Nb d'utilisateurs
WEST UNITED - Nb d'utilisateurs
WEST UNTED- Nb d'erreurs
EDGE - Crash
Nb d'utilisateurs ABC
Nb d'utilisateurs CDE
Nb d'utilisateurs EFG
Signal
Vélocité RTT (% de CPU)
0 Karma

jip31
Motivator

could you help me please concerning my last question?

0 Karma

jip31
Motivator

thanks it's almost perfect

I just forgotten the case where the field sarts with an uppercase follewed by " - F"

I wrote this but it changes nothing 

match(KPI, "^[A-Z].*- F"), 3
0 Karma

yuanliu
SplunkTrust
SplunkTrust

@jip31 wrote:

I just forgotten the case where the field sarts with an uppercase follewed by " - F"

I wrote this but it changes nothing 

 

match(KPI, "^[A-Z].*- F"), 3

 


I assume that you did 

 

 

| eval order=case(match(KPI, "^[A-Z].*- Nb"), 0, match(KPI, "^[a-z]"), 1, match(KPI, "^[A-Z]"), 2, match(KPI, "^[A-Z].*- F"), 3)

 

 

This does not change outcome because every match for "^[A-Z].*- F" already matches "^[A-Z]" (2) which precedes that entry in the case statement.  In many regex expressions, this would make some complex study, but because case statement is evaluated sequentially, you can simply force the order of case:

 

 

| eval order=case(match(KPI, "^[A-Z].*- Nb"), 0, match(KPI, "^[a-z]"), 1, match(KPI, "^[A-Z].*- F"), 3, match(KPI, "^[A-Z]"), 2)

 

 

This reads a bit awkward but should do the job.  If the awkwardness impedes maintenance in the future, the following makes the logic clearer:

 

 

| eval order=case(match(KPI, "^[A-Z].*- Nb"), 0, match(KPI, "^[a-z]"), 1, match(KPI, "^[A-Z].*- F"), 3, true(), 2)

 

 

The last statement expresses the idea that  "2" is used as a default. (Semantically this is different from 'match(KPI, "^[A-Z]"), 2', so you want to make a decision based on anticipated data.)

0 Karma

PickleRick
SplunkTrust
SplunkTrust

By default the fileds are sorted alphabetically. There is also one thing we can use - the fields in table command don't repeat.

So you can simply request the table command to print the '* -N*' fields first.

| table '* -N*' *

Unfortunately, if you want to do some more sophisticated reordering (like putting the N* fields sorted in reverse or something similarily unusual), there's no easy way to do that short of transposing all results, sorting them and transposing back (which is very ineffective).

0 Karma

richgalloway
SplunkTrust
SplunkTrust

 It's not clear what you're trying to do.  Please use more words to describe the data and the desired results.

---
If this reply helps you, Karma would be appreciated.
0 Karma

jip31
Motivator

Hi

I need to sort fields which belows with an upper case followed by N -

Something like that

"Test - N"

 

Tags (1)
0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@jip31 

Any sample data or expected out would be appreciated  to help you here.

KV

🙂

0 Karma

jip31
Motivator

For example, imagine that my fields are :

abc

C - N

A - N

def

B - N

ghi

I would like to sort like this :

abc

def

ghi

A - N

B - N

C - N

 

 

0 Karma

richgalloway
SplunkTrust
SplunkTrust

The challenge here is Splunk's sort command sorts strings in lexicographical order, which means upper-case letters will come before lower-case letters.  To get lower-case letters sorted before upper-case letters would require some SPL gymnastics (and I'm not sure where to start).

---
If this reply helps you, Karma would be appreciated.
0 Karma

richgalloway
SplunkTrust
SplunkTrust

After thinking about this for a little while, I came up with this query which should do the job.

| makeresults | eval data="abc
C - N
A - N
def
B - N
ghi" | eval data=split(data,"
") | mvexpand data
```Ignore the above as it is just for test data```
```Prepend text ending in "- N" with "zz" to force it to the end of the list ```
| eval sorter=if(match(data,"- N$"),"zz".data, data)
```Sort on the new field```
| sort sorter 
```Discard the sort field```
| fields - sorter
---
If this reply helps you, Karma would be appreciated.

PickleRick
SplunkTrust
SplunkTrust

That ain't that obvious, I'm afraid. OK, if the values in the field have predictable format, it might work relatively easily, but the lexicographic order is a bit more problematic.

See for yourself

| makeresults 
| eval _raw="a,_b,:c,$d,A,Z,ę,Ń,ę,1,\\,|"
| eval raw=split(_raw,",")
| mvexpand raw
| sort raw
0 Karma
Get Updates on the Splunk Community!

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

 Prepare to elevate your security operations with the powerful upgrade to Splunk Enterprise Security 8.x! This ...

Get Early Access to AI Playbook Authoring: Apply for the Alpha Private Preview ...

Passionate about security automation? Apply now to our AI Playbook Authoring Alpha private preview ...

Reduce and Transform Your Firewall Data with Splunk Data Management

Managing high-volume firewall data has always been a challenge. Noisy events and verbose traffic logs often ...