1

I am new to writing shell scripts. I currently have some reports that we run in HIVE SQL and I am trying to automate them the best I can. Currently I use crontab within our UNIX environment to have these queries run automatically everyday. Right now I have to paste that data into excel then filter and create separate documents for each "end-user".

What I am trying to accomplish is this:

I have a column in my output query that shows the name of a company, CompanyA, CompanyB, CompanyC etc. Depending on the details in there it could be anywhere from 12-20 different "companies". I don't want to hard code a query for each one to create it's own output. What I would like to do, is have a query that selects each unique company in that field(agency_name) and then run my select statement that would say:

select * from output_results where agency_name = "name here" and then write this output to a csv named Balance_Detail_"NAME"_Date then loop through and run this query and create an output for each name it found in the agency_name field.

3
  • 1
    Sorry we can't write the code for you. What you're asking sounds very possible. I would first create a query that just generated of Company names that you'll be processing on this run of your script, saving those values as plainly as possible to a file. Then you can wrap your select statment above in a loop, while read co_name ; do printf "select .... $co_name ....\n" | sqlClient ...args... | awk '...post filtering...' > $co_name.txt < co_nameFileList.txt Would be the general outline, but will require a fair amout of work on your part to get it to work and then bullet-proof it for errors. Commented Jun 22, 2020 at 18:16
  • 1
    Thank you for this. I apologize if it seemed like I was looking to have it written for me. I was more just trying to understand if the thoughts I had in my head were possible and how to even start that process. Thank you for your suggestion. I will start looking into that and see how I get along. Commented Jun 22, 2020 at 18:49
  • 1
    Very good! Best to post a new Q or anything that is slowing you up, trying to keep it focused on one problem at a time. Best if you can include small sample set of data, required output from that sample, current output, code, and exact text of error messages. Use the {} tool from the Edit menu on mouse-selected text to get code/data/output/errMsg formatting. Try to make it so it is copy/pasteable by readers. Also recall that we won't have access to your databases, but include which client you are using from cmdline (no GUIs!). sqlplus or isql are common. Good luck. Commented Jun 22, 2020 at 22:41

1 Answer 1

3

Here is a template how you can loop query result and call another hive script and to save in a CSV. Needs debugging. And of course use your queries:

#!/bin/bash

dt=$(date +'%Y_%m_%d')

for NAME in $(hive -S -e "select distinct agency_name from ..... ") 
 do 
  #echo "Company name is $NAME"
  hive -S -e " select * from output_results where agency_name = $NAME" | sed 's/[[:space:]]\+/,/g' > Balance_Detail_${NAME}_${dt}
 done
Sign up to request clarification or add additional context in comments.

Comments

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.