Splunk Search

Remove spacing from records after inputlookup

moinyuso96
Path Finder

The contents of my lookup file, test12345.csv is shown below.

ProductNumber,SerialNumber,StatusDateTime,Status

"A12345 ","MA00000001 ","2021-08-31 01:30:47 ","SHIPPED "

 

There is some space found "   " at the end of each record. The inputlookup would capture all the records with a spacing at the end which disrupts my joins to work properly. Is there anyway I can remove the spacing at the end of each record after using inputlookup?

ProductNumber =A12345

SerialNumber =MA00000001

StatusDateTime =2021-08-31 01:30:47

Status=SHIPPED

Labels (2)
0 Karma
1 Solution

gcusello
SplunkTrust
SplunkTrust

Hi @moinyuso96,

inside square brackets:

 ... 
|  join type=left ProductNumber Serial [ 
     | inputlookup test12345.csv|  rename SerialNumber as Serial
     | rex field=ProductNumber "(?<ProductNumber>.*)\s+$"
     | rex field=SerialNumber "(?<SerialNumber>.*)\s+$"
     | rex field=StatusDateTime "(?<StatusDateTime>.*)\s+$"
     | rex field=Status "(?<Status>.*)\s+$"
     ] 
| ...

Remember only one thing:

in this case, you have the problem of space in each field, so you have to modifiy fields using regexes or evals, but usually you can use the lookup command instead the join (the lookup command is a left join with a lookup).

In other words, remeber that Splunk isn't an SQL DB, so use join only when you haven't any other solution, because it's a very slow command.

For this reason, I'd try to solve the problem in the lookup, eventually modifying values in lookup.

I don't know how lookup is generated but you could correct the values in the lookup using a search, eventually to schedule.

Something like this:

| inputlookup test12345.csv
| rex field=ProductNumber "(?<ProductNumber>.*)\s+$"
| rex field=SerialNumber "(?<SerialNumber>.*)\s+$"
| rex field=StatusDateTime "(?<StatusDateTime>.*)\s+$"
| rex field=Status "(?<Status>.*)\s+$"
| table ProductNumber SerialNumber StatusDateTime Status 
| outputlookup test12345.csv

In this way, you can use a simpler lookup command:

| index=your_index
| lookup test12345.csv ProductNumber SerialNumber AS Serial OUTPUT StatusDateTime Status
| ...

Ciao.

Giuseppe

View solution in original post

moinyuso96
Path Finder

Hi @gcusello , thanks for answering.

However, I am not really sure where to insert the queries you gave me. Currently I am still have issues with the joins and inputlookup.

My join and inputlookup query is as follows:

 

 ... |  join type=left ProductNumber Serial [ | inputlookup test12345.csv|  rename SerialNumber as Serial] | ...

 

 




0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @moinyuso96,

inside square brackets:

 ... 
|  join type=left ProductNumber Serial [ 
     | inputlookup test12345.csv|  rename SerialNumber as Serial
     | rex field=ProductNumber "(?<ProductNumber>.*)\s+$"
     | rex field=SerialNumber "(?<SerialNumber>.*)\s+$"
     | rex field=StatusDateTime "(?<StatusDateTime>.*)\s+$"
     | rex field=Status "(?<Status>.*)\s+$"
     ] 
| ...

Remember only one thing:

in this case, you have the problem of space in each field, so you have to modifiy fields using regexes or evals, but usually you can use the lookup command instead the join (the lookup command is a left join with a lookup).

In other words, remeber that Splunk isn't an SQL DB, so use join only when you haven't any other solution, because it's a very slow command.

For this reason, I'd try to solve the problem in the lookup, eventually modifying values in lookup.

I don't know how lookup is generated but you could correct the values in the lookup using a search, eventually to schedule.

Something like this:

| inputlookup test12345.csv
| rex field=ProductNumber "(?<ProductNumber>.*)\s+$"
| rex field=SerialNumber "(?<SerialNumber>.*)\s+$"
| rex field=StatusDateTime "(?<StatusDateTime>.*)\s+$"
| rex field=Status "(?<Status>.*)\s+$"
| table ProductNumber SerialNumber StatusDateTime Status 
| outputlookup test12345.csv

In this way, you can use a simpler lookup command:

| index=your_index
| lookup test12345.csv ProductNumber SerialNumber AS Serial OUTPUT StatusDateTime Status
| ...

Ciao.

Giuseppe

gcusello
SplunkTrust
SplunkTrust

Hi @moinyuso96,

you have two ways:

  • the trim function in the eval command,
  • the regex.

using trim

| eval ProductNumber=trim(ProductNumber)
| eval SerialNumber=trim(SerialNumber)
| eval StatusDateTime=trim(StatusDateTime)
| eval Status=trim(Status)

using regex you have to add a row for each field:

| rex field=ProductNumber "(?<ProductNumber>.*)\s+$"
| rex field=SerialNumber "(?<SerialNumber>.*)\s+$"
| rex field=StatusDateTime "(?<StatusDateTime>.*)\s+$"
| rex field=Status "(?<Status>.*)\s+$"

Ciao.

Giuseppe

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!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Implementing Zero Trust (ZT) across complex environments often falters at the very beginning due to a ...