<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How can I copy a field value based on another field value ? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236870#M70377</link>
    <description>&lt;P&gt;Nice.  works for me too.  &lt;/P&gt;

&lt;P&gt;Now, how can we handle a NULL result? &lt;/P&gt;

&lt;P&gt;maybe wrap with coalesce(... ,"(NULL)") ?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach * [eval field_to_copy=coalesce(if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"='field_to_copy','&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;',field_to_copy),"(NULL)")]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 17 Jan 2017 17:29:33 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-01-17T17:29:33Z</dc:date>
    <item>
      <title>How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236860#M70367</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I have the following table:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ID, Team, Department
1, Manager, A65
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;After performing a lookup, I've got the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ID, Team, Department, field_to_copy
1, Manager, A65, Team
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;As you can see, the lookup tells me which field is interesting and I'd like to copy.&lt;BR /&gt;
The final result I'd like is:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ID, Team, Department, field_to_copy
1, Manager, A65, Manager
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is there an "eval" statement that could copy the value of "Team" and place it in "field_to_copy" ?&lt;BR /&gt;
In my example it is "Team" but it could anything (i.e. "Department", but not only).&lt;/P&gt;

&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:26:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236860#M70367</guid>
      <dc:creator>ctaf</dc:creator>
      <dc:date>2020-09-29T12:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236861#M70368</link>
      <description>&lt;P&gt;I think &lt;A href="https://answers.splunk.com/answers/186312/method-to-rename-field-to-value-of-another-field.html"&gt;this answer&lt;/A&gt; may help you to do that?&lt;/P&gt;

&lt;P&gt;Dave&lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 11:31:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236861#M70368</guid>
      <dc:creator>davebrooking</dc:creator>
      <dc:date>2017-01-12T11:31:18Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236862#M70369</link>
      <description>&lt;P&gt;Hi Dave, I've seen this answer, but it copies the field value to create a field name, not a field value. &lt;/P&gt;</description>
      <pubDate>Thu, 12 Jan 2017 12:46:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236862#M70369</guid>
      <dc:creator>ctaf</dc:creator>
      <dc:date>2017-01-12T12:46:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236863#M70370</link>
      <description>&lt;P&gt;If it's not doable, can I just use the value of "field_to_copy" in a where clause?&lt;/P&gt;

&lt;P&gt;For example, I would like:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;where field_A=$field_to_copy$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And the "$" sign would translate "$field_to_copy$" into "Team" so that it filter this way:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;where field_A=Team
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:27:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236863#M70370</guid>
      <dc:creator>ctaf</dc:creator>
      <dc:date>2020-09-29T12:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236864#M70371</link>
      <description>&lt;P&gt;At first this sounded like an easy question. However, after reading it several times I am not so sure about it anymore.&lt;/P&gt;

&lt;P&gt;So, let's talk about your example. First of all, I need to understand what exactly you want to archieve, that's not clear to me. Your "field_to_copy" naming kinda confuses me.&lt;/P&gt;

&lt;P&gt;So this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Column names: ID, Team, Department, field_to_copy
Values: 1, Manager, A65, Team
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Becomes this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Column names: ID, Team, Department,  field_to_copy
Values: 1, Manager, A65, Manager
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Which means simply changing the &lt;STRONG&gt;values&lt;/STRONG&gt; of field_to_copy to the values of Team?&lt;BR /&gt;
Am I correct?&lt;/P&gt;

&lt;P&gt;If so:&lt;/P&gt;

&lt;P&gt;Your search with lookup first followed by an eval:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search [...] | fields ID, team, Department, field_to_copy | eval field_to_copy=$team$ | table ID, team, Department, field_to_copy as Whatever_its_real_column_name_is
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is that what you are trying to do?&lt;BR /&gt;
When I'm wrong I'll convert to comment. But maybe you can clear my confusion. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;Skalli&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:23:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236864#M70371</guid>
      <dc:creator>skalliger</dc:creator>
      <dc:date>2020-09-29T12:23:50Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236865#M70372</link>
      <description>&lt;P&gt;Hi Skalli,&lt;/P&gt;

&lt;P&gt;First, thank you for trying to help me!&lt;/P&gt;

&lt;P&gt;You're correct but your solution works only if, at the beginning, field_to_copy=Team.&lt;BR /&gt;
Let's say field_to_copy=Department. In that case, I would like the result to be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Column names: ID, Team, Department,  field_to_copy
 Values: 1, Manager, A65, A65
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:27:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236865#M70372</guid>
      <dc:creator>ctaf</dc:creator>
      <dc:date>2020-09-29T12:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236866#M70373</link>
      <description>&lt;P&gt;So, the trivial answer, assuming you have a small number of fields, is to use the case statement.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval field_to_copy = case(field_to_copy=="Team",Team,field_to_copy=="Department",Department,...) 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;CURLY BRACES ON THE &lt;STRONG&gt;RIGHT&lt;/STRONG&gt; OF THE ASSIGNMENT DOES NOT WORK.&lt;/P&gt;

&lt;P&gt;Deleted all but the following example of curly braces on the LEFT to avoid confusing people with erroneous guesswork.&lt;/P&gt;

&lt;P&gt;For instance, if field_to_copy had the value "Team", this would cause the variable Team to be set to "George".&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval {field_to_copy} = "George" 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:27:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236866#M70373</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2020-09-29T12:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236867#M70374</link>
      <description>&lt;P&gt;Nice! But the list of fields is not predefinied (my example was simplified so that the question would not be too hard to understand) and I always avoid to hardcode things that can change. Is there another possibility to generalize it?&lt;/P&gt;

&lt;P&gt;The following, unfortunately, does not work...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval field_to_copy = if(field_to_copy==*,*,"null")
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2017 16:48:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236867#M70374</guid>
      <dc:creator>ctaf</dc:creator>
      <dc:date>2017-01-17T16:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236868#M70375</link>
      <description>&lt;P&gt;My answer was just updated to include curly braces, which I just found.  &lt;/P&gt;

&lt;P&gt;Tested, and it does not work.  Still looking for a solution for you.&lt;/P&gt;

&lt;P&gt;see somesoni2's answer, which works perfectly.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Jan 2017 16:52:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236868#M70375</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-01-17T16:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236869#M70376</link>
      <description>&lt;P&gt;Pheww.. this was fun. Something like this works for me (run anywhere query with sample data. replace first line with your query)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| gentimes start=-1 | eval ID=1 | table ID | eval Team="Team1" | eval Manager="Manager2" | eval field_to_copy="Team"  
| foreach * [eval field_to_copy=if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"='field_to_copy','&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;',field_to_copy)]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2017 17:19:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236869#M70376</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-01-17T17:19:15Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236870#M70377</link>
      <description>&lt;P&gt;Nice.  works for me too.  &lt;/P&gt;

&lt;P&gt;Now, how can we handle a NULL result? &lt;/P&gt;

&lt;P&gt;maybe wrap with coalesce(... ,"(NULL)") ?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach * [eval field_to_copy=coalesce(if("&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;"='field_to_copy','&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;',field_to_copy),"(NULL)")]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Jan 2017 17:29:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236870#M70377</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-01-17T17:29:33Z</dc:date>
    </item>
    <item>
      <title>Re: How can I copy a field value based on another field value ?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236871#M70378</link>
      <description>&lt;P&gt;NULL in field_to_copy field??&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 12:23:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-copy-a-field-value-based-on-another-field-value/m-p/236871#M70378</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2020-09-29T12:23:56Z</dc:date>
    </item>
  </channel>
</rss>

