0

this is my file:

$ cat head_datafile_pipe_deleimiter.csv
"Rec_Open_Date"|"MSISDN"|"IMEI"|"Data_Volume_Bytes"|"Device_Manufacturer"|"Device_Model"|"Product_Description"|"Data_Volume_MB"
"2016-07-17"|"686"|"630"|"618320"|"Apple Inc"|"Apple iPhone S A1530"|"PREPAY PLUS - $0 -"|"0.589676"
"2016-07-17"|"560"|"570"|"42841779"|"Motorola Mobility LLC, a Lenovo Company"|"Moto X 2nd Generation, X112360445"|"$39.95 Plan"|"40.8571"
"2016-07-17"|"811"|"340"|"2465082"|"Samsung Korea"|"Samsung SM-G900I"|"$69.95 Plan"|"2.35089"
"2016-07-17"|"785"|"610"|"41498628"|"Apple Inc"|"Apple iPhone 6S Plus A1687"|"$29.95 Carryover Plan 1GB"|"39.5762"
"2016-07-17"|"908"|"310"|"6497563"|"Samsung Korea"|"Samsung GT-I9195"|"PREPAY PLUS - $0 -"|"6.19656"
"2016-07-17"|"919"|"610"|"0"|"Samsung Korea"|"Samsung SM-G925I"|"$19 CO COMBO - NOT RECURRENT"|"0"
"2016-07-17"|"356"|"290"|"33189681"|"Apple Inc"|"Apple iPhone 6S A1688"|"$39.95 Plan"|"31.6521"
"2016-07-17"|"009"|"160"|"30340"|"Samsung Korea"|"Samsung SM-J500Y"|"PREPAY PLUS - $1 - #33"|"0.0289345"
"2016-07-17"|"574"|"400"|"549067"|"HUAWEI Technologies Co Ltd"|"HUAWEI Y6"|"PREPAY PLUS - $0 -"|"0.523631"

and here I am close to what I want but i cannot get it to iterate through the differnet elements in the array. if($7==dq"PREPAY PLUS - $0 -"dq) print $7. It is basically a for loop that iterates trhough the elements in the array, and there for filters out the value I want before moving onto the next element in the array and repeating. But I can only get it to work if I hard code the value, I want it to iterate through the array u_vals

$ for i in "${u_vals[@]}"; do awk -F'|' -v var="${u_vals[*]}" -v j=i -v dq='"' 'NR==1{print $7} NR>1{split(var,list,"|"); if($7==dq"PREPAY PLUS - $0 -"dq) print $7 }' head_datafile_pipe_deleimiter.csv; done
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"

these are all the uniq value in column 7

$ printf "%s\n" "${u_vals[@]}"
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"

How do I make this part
dq"PREPAY PLUS - $0 -"dq
of this
if($7==dq"PREPAY PLUS - $0 -"dq) print $7
iterate through the elements of the array?

EDIT1

this is what I was looking for:

awk -F"|" ...

$ for i in "${u_vals[@]}"; do awk -F"|" -v j="$i"  'NR==1{print $7}NR>1 {if($7==j) print $7 }' head_datafile_pipe_deleimiter.csv ; done
"Product_Description"
"$19 CO COMBO - NOT RECURRENT"
"Product_Description"
"$29.95 Carryover Plan 1GB"
"Product_Description"
"$39.95 Plan"
"$39.95 Plan"
"Product_Description"
"$69.95 Plan"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $1 - #33"

awk -v FS="|" ....

$ for i in "${u_vals[@]}"; do awk -v FS="|" -v j="$i"  'NR==1{print $7}NR>1 {if($7==j) print $7 }' head_datafile_pipe_deleimiter.csv ; done
"Product_Description"
"$19 CO COMBO - NOT RECURRENT"
"Product_Description"
"$29.95 Carryover Plan 1GB"
"Product_Description"
"$39.95 Plan"
"$39.95 Plan"
"Product_Description"
"$69.95 Plan"
"Product_Description"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $0 -"
"Product_Description"
"PREPAY PLUS - $1 - #33"
4
  • 1
    Which array are you talking about? Iterating through each fields in a line from above file? Iterating for each line in file? bash array u_vals? Commented Jul 22, 2016 at 4:22
  • 1
    Please rephrase the question. IIUC, you wish to print the record if and only if the 7th field appears in the bash array u_vals? Commented Jul 22, 2016 at 4:26
  • 1
    Please show clearly what your desired output is. Commented Jul 22, 2016 at 5:44
  • I've read this question multiple times over the past 10 minutes and I have absolutely no idea what it is you're trying to do. I strongly suspect this is an example of the XY Problem and a loop on the contents of a shell array is the wrong approach to whatever it is you're trying to accomplish. Commented Jul 22, 2016 at 12:39

1 Answer 1

1

You got a small syntax error. When you dou in the awk

awk ... -v j=i

It should be

awk ... -v j="$i"

This should work:

for i in "${u_vals[@]}"; do awk -v FS="|" -v j="$i"  'NR==1{print $7}NR>1 {if($7==j) print $7 }' datafile_pipe_deleimiter.csv ; done

The following part of the awk:

split(var,list,"|");

I don't know why do you need it for, so I did not put in my answer

Sign up to request clarification or add additional context in comments.

1 Comment

tks. I was using split because that's how I worked out how to pass the array u_vals into awk. but your way is better. Interesting that you can do awk -F"|" ... as well as awk -v FS="|" ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.