0

i'm translating a bash script to a Lua program. In bash script there is a line:

mapfile -t vol < <( cat csv_file | head -$id | grep locateme | tail -3 | cut -f6 -d\,)

the result of that is:

vol[0]=22
vol[1]=33
vol[2]=44

the csv_file is like:

16,a,b,c,d,9,16,0,3,65,0,0,locateme
16,a,b,c,d,11,16,0,3,65,0,0,notme
16,a,b,c,d,22,16,0,3,65,0,0,locateme
16,a,b,c,d,33,16,0,3,65,0,0,locateme
16,a,b,c,d,32,16,0,3,65,0,0,notme
16,a,b,c,d,44,16,0,3,65,0,0,locateme

I need a table with the same results than bash:

vol[1]=22
vol[2]=33
vol[3]=44

please, i have no idea how to start with this

Juanmol
  • 11
  • 1

1 Answers1

0

Instead of a Bash array you're going to use a Lua table.

local vol = {}

You'll need a generic for loop and the file:lines(...) iterator. It is a good idea to read through the whole io library.

This will allow you to get each line of the csv file as a string for further processing.

No you'll need Lua's string library. There are multiple ways to do this. One option is to use another generic for loop with string.gmatch and a suitable string pattern that captures the value you're interested in.

Piglet
  • 27,501
  • 3
  • 20
  • 43