0

I have a requirement to retrieve 239, 631 etc from the below output and store it in a variable in linux -- this is output of impala results..

+-----------------+
| organization_id |
+-----------------+
| 239             |
| 631             |
| 632             |
| 633             |
+-----------------+

below is the query I am running.

x=$(impala-shell -q "${ORG_ID}" -ki "${impalaserver}");

How to do it?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Yogesh
  • 47
  • 1
  • 10
  • So is it like after `organization_id` 1st and 2nd integer you need each time, please confirm once? – RavinderSingh13 Mar 07 '19 at 16:46
  • No, there can be any number of organization_id in it. Basically it is running a code which retrieves org ID and I want to store the distinct values in a variable. In this case I need all 4 of them. In other case there can be more or less than 4... – Yogesh Mar 07 '19 at 16:48

2 Answers2

0

Could you please try following. This will be deleting duplicates for all Input_file(even a single id comes in 1 organization_id it will NOT br printed in other stanza then too)

your_command | awk -v s1="'" 'BEGIN{OFS=","} /---/{flag=""} /organization_id/{flag=1;getline;next} flag && !a[$2]++{val=val?val OFS s1 $2 s1:s1 $2 s1} END{print val}'


In case you need to print ids(which are coming in 1 stanza and could come in other stanza of organization_id then try following):

your_command | awk -v s="'" 'BEGIN{OFS=","} /---/{print val;val=flag="";delete a} /organization_id/{flag=1;getline;next} flag && !a[$2]++{val=val?val OFS s1 $2  s1:s1 $2 s1} END{if(val){print val}}'
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Okay, I think this is working but what if I have to delimit each of org id value by a comma? Like 239,631,632,633 ? – Yogesh Mar 07 '19 at 16:55
  • @Yogesh, please check my EDIT codes now and let me know then? – RavinderSingh13 Mar 07 '19 at 16:58
  • 1
    yes sir, this is working perfect! Thank you!! Is there a way to shorten the code? Any substitute? I am new to linux and stuck with this. – Yogesh Mar 07 '19 at 16:59
  • Hmm. I understand what you are saying. Also what if I have to put them in quotes? like '632','239' etc....I am still trying to figure out this code. – Yogesh Mar 08 '19 at 08:18
  • @Yogesh, kindly mention all your requirements on first time it self, I have edited codes now let me know? – RavinderSingh13 Mar 08 '19 at 08:34
0

What about this :

x=$(impala-shell -B -q "${ORG_ID}" -ki "${impalaserver}")

I just added the -B option which removes the pretty-printing and the header.

If you want comma-separated values you can pipe the result to tr :

echo $x | tr ' ' ','
cheseaux
  • 5,187
  • 31
  • 50
  • I used below script and though this seems to have tab delimiters, but I cant seem to have comma separator... is there a syntax issue? x=$(impala-shell -B --output_delimiter="," -q "${sql1}" -ki "${impalaserver}") – Yogesh Mar 08 '19 at 11:10