Extract specific text from variable and put it into array

Dear community,
I have to do something too hard for me :rolleyes:. I hope you can help me.

This is an output coming from Oracle query, stored in a file called query.out, there are many rows, but I read them, one by one, using while/read/done. Assuming each row is contained into $line variable that are valued as:

1234[12345678=3]; 9999[98765432=3]; 1234[57382939=3]; 8888[984903020=3]; 77777[0583758738=3]; 8888[670986767=3]

What I need to do is extract the following value in uniquely way.

What I need to extract (in bold/red):

1234[12345678=3]; 54321[98765432=3]; 1234[57382939=3]; 8888[984903020=3]; 77777[0583758738=3]; 8888[670986767=3]

In this case I have 6 occurrences, but sure this can be lower or greater (depends on oracle output query).

What I'm expecting from extraction:

1234
54321
8888
77777

These values should go into an array (later I'll cycle the array to put them into a log file togheter with other info).

Hope someone can help me to solve this since I'm gonna crazy! :frowning:

sed 's/;/\n/g' filename | sed 's/\[/\n\[/g' | grep -v '^\[' | sed 's/^ *//'

Thanks rajkumarin, this works to extract the right value, but extract all the values and not in uniquely way. Plus, is it possibile to "sed" a variable instead of file (text)?

# sed 's/;/\n/g' text | sed 's/\[/\n\[/g' | grep -v '^\[' | sed 's/^ *//'
1234
54321
1234
8888
77777
8888
echo '1234[12345678=3]; 54321[98765432=3]; 1234[57382939=3]; 8888[984903020=3]; 77777[0583758738=3]; 8888[670986767=3]'|awk -F\; '{for(i=1;i<=NF;i++) if(!c[$i+0]++) print $i+0}'
1 Like

@elixir sinary, you are the master!!! All in one row!!! :eek:

Just a stupid question (I know), how can I put the result values into an array?
I can redirect the result to another tmp file and then read it row by row, but I prefer to use an array to read with a for cycle...

line='1234[12345678=3]; 54321[98765432=3]; 1234[57382939=3]; 8888[984903020=3]; 77777[0583758738=3]; 8888[670986767=3]'
arr=($(echo "$line"|awk -F\; '{for(i=1;i<=NF;i++) if(!c[$i+0]++) print $i+0}'))
for i in "${arr[@]}"
do
 echo "I AM " $i
done
1 Like

Dear elixir sinary,
I'm experiencing problem

#echo '1234[12345678=3]; 54321[98765432=3]; 8888[984903020=3]'|awk -F\; '{for(i=1;i<NF;i++) if(!c[$i+0]++) print $i+0}'
1234
54321

As you can see the latest value is not shown, this because most probably it miss the ';' at the end.
This works:

# echo '1234[12345678=3]; 54321[98765432=3]; 8888[984903020=3];'|awk -F\; '{for(i=1;i<NF;i++) if(!c[$i+0]++) print $i+0}'
1234
54321
8888

I can simply add manually the ; but do you have any other idea?

Aw shucks...I made a slight mistake..change i<NF to i<=NF . Rectified now in the other posts.

1 Like

hehehe!, it was hard to find because in my first example, the latest value is discarded because already present in the list! :wink:

BTW, I have no words to thank you, you saved my a lot of headache :rolleyes:

:b::b::b::b::b::b::b::b: