Skip to main content
edited tags
Link
RavinderSingh13
  • 134.6k
  • 14
  • 61
  • 100
deleted 94 characters in body; edited title
Source Link
John Kugelman
  • 364.5k
  • 70
  • 555
  • 600

CSV file to array in shell Parse columns into separate arrays

I am having CSVhave a space-delimited file as below:

Pool    Library Name    Email   Subject
Finland lib1    Guru    [email protected],[email protected]   Finland Media Rotation
Tucson  lib2    Albert  [email protected] Tucson Media Rotation
Vancouver   lib3    Jeff    [email protected] Vancouver Media Rotation

I want it to have an array through themparse the columns into arrays like:

declare -a Pool=(Finland Tucson Vancouver)
declare -a Library=(lib1 lib2 lib3)
declare -a Name=(Guru Albert Jeff)
declare -a Email=("[email protected],[email protected]" [email protected] [email protected]) 

and so based on column.

My code is:

column=1
for arrayname in Pool Library; do
     mapfile -t "$arrayname" < <(awk "NR > 1 {print \$$column}" file.txt)
     ((column++))
done

But itsit's failing in case of multiple items like in Email.

I am novice so please help me with complete codeEmail.

CSV file to array in shell

I am having CSV file as below:

Pool    Library Name    Email   Subject
Finland lib1    Guru    [email protected],[email protected]   Finland Media Rotation
Tucson  lib2    Albert  [email protected] Tucson Media Rotation
Vancouver   lib3    Jeff    [email protected] Vancouver Media Rotation

I want it to have an array through them like:

declare -a Pool=(Finland Tucson Vancouver)
declare -a Library=(lib1 lib2 lib3)
declare -a Name=(Guru Albert Jeff)
declare -a Email=("[email protected],[email protected]" [email protected] [email protected]) 

and so based on column.

My code is

column=1
for arrayname in Pool Library; do
     mapfile -t "$arrayname" < <(awk "NR > 1 {print \$$column}" file.txt)
     ((column++))
done

But its failing in case of multiple items like in Email.

I am novice so please help me with complete code.

Parse columns into separate arrays

I have a space-delimited file:

Pool    Library Name    Email   Subject
Finland lib1    Guru    [email protected],[email protected]   Finland Media Rotation
Tucson  lib2    Albert  [email protected] Tucson Media Rotation
Vancouver   lib3    Jeff    [email protected] Vancouver Media Rotation

I want to parse the columns into arrays like:

declare -a Pool=(Finland Tucson Vancouver)
declare -a Library=(lib1 lib2 lib3)
declare -a Name=(Guru Albert Jeff)
declare -a Email=("[email protected],[email protected]" [email protected] [email protected]) 

My code is:

column=1
for arrayname in Pool Library; do
     mapfile -t "$arrayname" < <(awk "NR > 1 {print \$$column}" file.txt)
     ((column++))
done

But it's failing in case of multiple items like in Email.

edited title
Link
larsks
  • 318.2k
  • 49
  • 473
  • 482

CSV FILE TO ARRAY IN SHELLfile to array in shell

deleted 15 characters in body
Source Link
RavinderSingh13
  • 134.6k
  • 14
  • 61
  • 100
Loading
Source Link
Loading