0

I have a space-delimited file:

Pool    Library Name    Email   Subject
Finland lib1    Guru    guru@abc.com,Narendra@abc.com   Finland Media Rotation
Tucson  lib2    Albert  abc@def.com Tucson Media Rotation
Vancouver   lib3    Jeff    ghi@abc.com 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=("guru@abc.com,Narendra@abc.com" abc@def.com ghi@abc.com) 

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.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
GURU SINGH
  • 149
  • 7
  • @GURUSINGH, Will `Name` field have spaces in between them or any other field? – RavinderSingh13 Nov 30 '19 at 15:25
  • If there are multiple names in EMAIL then it will be a comma in between them. – GURU SINGH Nov 30 '19 at 15:30
  • @GURUSINGH, No, I meant for `Name` column. I got the email part, how about Name column? – RavinderSingh13 Nov 30 '19 at 15:31
  • your project will be much easier to manage if you can use either tab-char or `|` as the field separator for your data. Good luck. – shellter Nov 30 '19 at 15:45
  • What is the problem with multiple email, given that they are separated with comma ? – dash-o Nov 30 '19 at 18:55
  • Is this related to https://stackoverflow.com/questions/59098794/array-values-from-text-or-csv-file-in-shell ? – dash-o Nov 30 '19 at 18:55
  • yes @dash-o its the same. – GURU SINGH Nov 30 '19 at 19:32
  • I am trying to get the solution for it from long but not able to get it – GURU SINGH Nov 30 '19 at 19:32
  • 1
    You are likely to get better feedback if you will clarify what is failing. 'Failing in case of multiple items` is not clear. Splitting the problem over multiple posts unlikely to get any complete answer: https://stackoverflow.com/questions/58998007/picking-up-and-matching-multiple-values-from-array-shell-scripting, https://stackoverflow.com/questions/59021512/how-to-remove-space-from-word-while-passing-it-in-array-in-shell – dash-o Dec 01 '19 at 00:36

1 Answers1

2

Could you please try following.

awk -v s1="\"" '
FNR==1{
  for(i=1;i<=NF;i++){
    header[i]=$i
  }
  val=length(header)
  next
}
{
  match($0,/.*\.com +/)
  string=substr($0,RSTART,RLENGTH)
  num=split(string,array," ")
  for(i=1;i<=num;i++){
    values[i]=(values[i]?values[i] OFS:"")s1 array[i] s1
  }
  values[num+1]=(values[num+1]?values[num+1] OFS:"")substr($0,RSTART+RLENGTH)
  delete array
}
END{
  for(i=1;i<=val;i++){
    print "declare -a " header[i] "=(" values[i]")"
  }
}
'  Input_file

This will print the array creation commands on terminal, you could use the as per your need.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    thanks for your help. your code works like charm.I need one little help too, that now how to pass these arguments to my script. – GURU SINGH Dec 01 '19 at 04:46
  • 1
    @GURUSINGH, Np. Please have 1 question and 1 thread only, you could open a new thread with your EFFORTS which you have put in order to solve your own problem, cheers and happy learning. – RavinderSingh13 Dec 01 '19 at 05:02