# Fetching data from a csv file and updating another file accordingly

**URL:** https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281
**Category:** Logstash
**Created:** [June 25, 2019, 9:15am UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281 "2019-06-25T09:15:46Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jatin6418](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jatin6418](https://discuss.elastic.co/u/jatin6418)
#### Post date: [June 25, 2019, 9:15am UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281/1 "2019-06-25T09:15:46Z")

</div>

hey  
I am having two files one is a log file and one is a csv file. From log file I am reading the itemid and I want to add the name of this item which is stored in the csv file can someone help me .

input{

```
 file{
     type => "log"
     path =>"/home/dummy.log"
     start_position =>"beginning"
     sincedb_path => "/dev/null"
     }
   

 file{
     type => "csv"
     path =>"/home/output.csv"
     start_position =>"beginning"
     sincedb_path => "/dev/null"
     }

```

}

filter {

if [type] == "log" {

grok {  
match =\> [  
"message","(?\<item\_id\>[\w-/]+)"  
]  
}

}

if [type] == "csv"{  
csv{  
separator =\> ","  
columns =\> ["item\_id", "item\_name"]  
}

```
   } 

```

}

---

<div class="post-metadata">

### Author: ![sjabiulla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sjabiulla/32/48429_2.png) [@sjabiulla](https://discuss.elastic.co/u/sjabiulla)
#### Post date: [June 25, 2019, 10:48am UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281/2 "2019-06-25T10:48:03Z")

</div>

1. Read only log file in input. Remove the csv file type from input.

2. Extract the item id from the log using grok filter as you already did.

3. Now use the [translate](https://www.elastic.co/guide/en/logstash/current/plugins-filters-translate.html) filter to map the item id with name . Translate filter in your scenario will look like below.

---

<div class="post-metadata">

### Author: ![jatin6418](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jatin6418](https://discuss.elastic.co/u/jatin6418)
#### Post date: [June 25, 2019, 4:16pm UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281/4 "2019-06-25T16:16:07Z")

</div>

Thnx a lot sir

I wanted to ask one more question

if my grook filter looks like  
(?[\d-]+)\s(?[\w-/]+)

(here viewid represents the item\_id)

and my csv file has the following structure  
community\_id,item\_id,community\_name,item\_name

then how should i write the translate filter  
Is there any restriction that my csv file should have only two columns in which mapping needs to be done?

---

<div class="post-metadata">

### Author: ![sjabiulla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sjabiulla/32/48429_2.png) [@sjabiulla](https://discuss.elastic.co/u/sjabiulla)
#### Post date: [June 26, 2019, 3:43am UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281/5 "2019-06-26T03:43:59Z")

</div>

Yes, csv can only have two columns. Hence you can have csv as below. The below has only 2 columns, but the second column has all info that you need.

> item\_id,community\_id|community\_name|item\_name

You can use below filters now.

```
translate {
field => "item_id"
destination => "item_details"
dictionary_path => "/home/output.csv"
}

mutate {
 split => { "item_details" => "|" }
 add_field => { "community_id" => "%{item_details[0]}" }
 add_field => { "community_name" => "%{item_details[1]}" }
 add_field => { "item_name" => "%{item_details[2]}" }
}

```

Now you will have comunity\_id, community\_name, item\_name that are mapped by item\_id via csv in the output

---

<div class="post-metadata">

### Author: ![jatin6418](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jatin6418](https://discuss.elastic.co/u/jatin6418)
#### Post date: [June 28, 2019, 6:20pm UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281/6 "2019-06-28T18:20:23Z")

</div>

thnxx a lot for your help it worked for me .

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 26, 2019, 6:24pm UTC](https://discuss.elastic.co/t/fetching-data-from-a-csv-file-and-updating-another-file-accordingly/187281/7 "2019-07-26T18:24:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
