0

I want that output result to grep second and third column:

1       db1    ADM_DAT   300     yes     95.09
2       db2    SYSAUX          400     yes     94.52

and convert them like array for example:

outputres=("db1    ADM_DAT" "db2    SYSAUX")

and after that to be able to read those values in loop for example:

for i in "${outputres[@]}"; do read -r a b <<< "$i"; unix_command $(cat file|grep $a|awk '{print $1}') $a $b;done

file:

10.1.1.1 db1
10.1.1.2 db2

Final expectation:

unix_command 10.1.1.1 db1 ADM_DAT   
unix_command 10.1.1.2 db2 SYSAUX

This is only a theoretical example, I am not sure if it is working.

Kalin Borisov
  • 1,091
  • 1
  • 12
  • 23

1 Answers1

2

I would use a simple bash while read and keep adding elements into the array with the += syntax:

outputres=()
while read -r _ a b _; do
  outputres+=("$a $b")
done < file

Doing so, with your input file, I got:

$ echo "${outputres[@]}"     #print all elements
db1 ADM_DAT db2 SYSAUX
$ echo "${outputres[0]}"     #print first one
db1 ADM_DAT
$ echo "${outputres[1]}"     #print second one
db2 SYSAUX

Since you want to use both values separatedly, it may be better to use an associative array:

$ declare -A array=()
$ while read -r _ a b _; do array[$a]=$b; done < file

And then you can loop through the values with:

$ for key in ${!array[@]}; do     echo "array[$key] = ${array[$key]}"; done
array[db2] = SYSAUX
array[db1] = ADM_DAT

See a basic example of utilization of these arrays:

#!/bin/bash
declare -A array=([key1]='value1' [key2]='value2')

for key in ${!array[@]}; do
    echo "array[$key] = ${array[$key]}"
done

echo ${array[key1]}
echo ${array[key2]}

So maybe this can solve your problem: loop through the file with columns, fetch the 2nd and 3rd and use them twice: firstly the $a to perform a grep in file and then as parameters to cmd_command:

while read -r _ a b _
do
    echo "cmd_command $(awk -v patt="$a" '$0~patt {print $1}' file) $a, $b"
done < columns_file

For a sample file file:

$ cat file
hello this is db1
and this is another db2

I got this output (note I am just echoing):

$ while read -r _ a b _; do echo "cmd_command $(awk -v patt="$a" '$0~patt {print $1}' file) $a, $b"; done < a
cmd_command hello db1, ADM_DAT
cmd_command and db2, SYSAUX
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • did the IFS= read the previous output? Because I don't save that to file I want output which I get automaticaly to turn in that array outputres – Kalin Borisov Feb 13 '15 at 12:52
  • then instead of `while read; do ... done < file` just pipe it: `cat file | things | while read ... done` – fedorqui Feb 13 '15 at 12:54
  • I don't get that information from file is just simple print from previous command which give me that output – Kalin Borisov Feb 13 '15 at 12:55
  • @KalinBorisov it is not clear to me what is the problem here. You can either read from a file or pipe from a previous output. Please update your question with more details about what you are doing and what's your ultimate goal – fedorqui Feb 13 '15 at 12:58
  • the idea is to convert those columns to variables and to use them values for further proposes in that case in the loop to use $a=db1 $b=ADM_DAT that will be able to grep additional things with $a variable or $b – Kalin Borisov Feb 13 '15 at 13:04
  • @KalinBorisov so to get a clear picture: you would like to have the values in the array available to loop through them. Currently I am storing them in a way that they appear together, so item 1 is "db1 ADM_DAT", item 2 "db2 SYSAUX". What about creating an key->value array in which one value is the key and the other one the value? With bash 4+ this is done with `declare -A array=([key1]='value1' [key2]='value2')`. Otherwise, you will be "condemned" to use a matrix. – fedorqui Feb 13 '15 at 13:07
  • @gniourf_gniourf well I do use `IFS=` because I understood from the OP that he wants each pair of fields to be one value in the array, so that field1 is "db1 ADM_DAT", second one "db2 SYSAUX", etc. But as he is commenting here, it may lead to a different solution. Yours sounds good, let's see if this is the desired one! – fedorqui Feb 13 '15 at 13:09
  • @fedorqui I try your example with file but is not working in my environment when I put the whole output and try while nothing happening, the idea of this array for me is to take those two variables $a and $b because after it I want to use them in loop like `cmd_command $(cat file|grep $a|awk '{print $1}') $a $b` in that case $a variable will grep in file which is ex.: 10.1.1.1 db1 and will take the host and put to the loop – Kalin Borisov Feb 13 '15 at 13:18
  • 1
    @gniourf_gniourf you are right. I added `IFS=` after checking and I didn't test it. Updated, thanks! – fedorqui Feb 13 '15 at 13:20
  • @KalinBorisov sorry there was an error in my code. Could you try to the updated one, without `IFS`? It should be fine now. – fedorqui Feb 13 '15 at 13:21
  • now your example is working but that doesn't help to make those values work for my loop, could you please look my latest comment – Kalin Borisov Feb 13 '15 at 13:25
  • @KalinBorisov ok. I added another suggested answer with an associative array. Let me know if this helps better. – fedorqui Feb 13 '15 at 13:31
  • I just edit my question hope now is to be more clear – Kalin Borisov Feb 13 '15 at 13:36
  • 1
    @KalinBorisov that edit was quite helpful. See my last edit with a full answer. It is at the bottom – fedorqui Feb 13 '15 at 13:58
  • I have issue when have uppercase "awk -v" option do not grep properly. I mean if $a=DB1 is not able to get the result – Kalin Borisov Feb 18 '15 at 09:10
  • @KalinBorisov it is not very clear what you mean, try to give an example. What I would say is that the double quotes in `-v patt="$a"` can screw with the rest of the command, since there are other double quotes around. So if `$a` is just a word, you can drop them and say `-v patt=$a`. – fedorqui Feb 18 '15 at 10:20
  • The idea is when I have `10.1.1.1 DB1` when use awk command is not possible to take the IP address because DB1 is uppercase. Is there possible way to catch all kind lower-upper mixed cases? – Kalin Borisov Feb 18 '15 at 10:58