Bash array manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash array manipulation
# 1  
Old 01-15-2013
Bash array manipulation

seeking assistance on comparing two arrays using bash:
Code:
array1=(disk1, disk2, disk3, disk5, disk7, vol1, vol2, vol3, vol4, vol5)
array2=(disk2, disk5 vol2, vol4 )

1) if two arrays have same elements; EXIT
else populate array3 & array4 with elements that are different between array1 & array2 as:
Code:
array3=(vol1, vol3, vol5)
array4=(disk1, disk3, disk7)

thanks in advance

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 01-15-2013 at 05:44 AM..
# 2  
Old 01-15-2013
Try this and assign to arrays 3 & 4 accordingly:
Code:
$ grep -vf <(echo "${array2[*]}"|tr -s ', ' '\n') <(echo "${array1[*]}"|tr -s ', ' '\n')
disk1
disk3
disk7
vol1
vol3
vol5

# 3  
Old 01-16-2013
looks like there is no "f" flag that goes along with grep (Solaris).Without the f flag, dont think i got what i wanted:
Code:
Code:
#!/usr/bin/bash
array1=( `cat /tmp/array1 `)
array2=( `cat /tmp/array2 `)
echo "Elements in array1: ${array1[@]}"
echo "Elements in array2: ${array2[@]}"
grep -v <(echo "${array2[*]}"|tr -s ', ' '\n') <(echo "${array1[*]}"|tr -s ', ' '\n')
./test-array.sh
Elements in array1: disk1 disk2 disk3 disk5 disk7 vol1 vol2 vol3 vol4 vol5
Elements in array2: disk2 disk5 vol2 vol4
disk1 disk2 disk3 disk5 disk7 vol1 vol2 vol3 vol4 vol5

so the output is exactly array1 not the non-commom elements between the two.
I need: disk1 disk3 disk7 vol1 vol3 vol5
also i need (disk1 disk3 disk7) & (vol1 vol3 vol5) as two separate arrays from the output.
# 4  
Old 01-16-2013
From the man pages in this forum:
Quote:
/usr/xpg4/bin/grep [-E | -F] [-c | -l | -q] [-bhinsvwx] -e pattern_list... [-f pattern_file]... [file]...

-f pattern_file Reads one or more patterns from the file named by the path name pattern_file. Patterns in pattern_file are terminated by a NEWLINE character. . . .
Watch out - there's two greps in solaris!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Array manipulation with awk?

Dear friends, I'm wondering if we could do some simple math on two arrays with the same size? a1 Fe -0.21886700 -0.01417600 -0.24390300 C 2.20529400 0.89434100 -0.61061000 C -1.89657700 -0.74793000 -0.07778200 C ... (8 Replies)
Discussion started by: liuzhencc
8 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. Shell Programming and Scripting

Bash - file manipulation

I need to change a file like this: John Smith;http://www.profile1.com John Smith;http://www.profile2.com Frank Olsen;http://www.profile3.com Frank Olsen;http://www.profile4.com Into: John Smith;http://www.profile1.com;http://www.profile2.com Frank... (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

Bash string manipulation

In my script I'm retrieving a parameter through an API call. I need to trim some things out of the result so I can use it as a parameter to pass to another process. I've got it working but it's pretty kludgy and I'm hoping someone can help me with a better way. This is the code that retrieves... (2 Replies)
Discussion started by: withanh
2 Replies

6. Shell Programming and Scripting

bash string manipulation

Hello guys, here is my problem: I got a shell script which is called by an external piece of software, the external software is not under my control. The software passes data as an argument to my script like ./bla.sh 'service;1234567890;ok;hostname;some text here' I need to pass the... (3 Replies)
Discussion started by: snoogie
3 Replies

7. Shell Programming and Scripting

AWK manipulation in bash script

EDIT: This has been SOLVED. Thanks! Greetings everyone, I've posted a few threads with some quick help questions, and this is another one of those. I can't post enough gratitude for those much more knowledgeable than myself who are willing to give good advice for my minor issues. Now,... (2 Replies)
Discussion started by: Eblue562
2 Replies

8. Shell Programming and Scripting

bash, help with stdout manipulation.

Hey all, Im kind of lost on how to do what I want so I figured I would ask. I want to pipe STDOUT of an app to a log file, but I want to prepend each line of that output with the date and time. Im drawing a complete blank on how to do this?? Any ideas? i.e. output is currently this:... (9 Replies)
Discussion started by: trey85stang
9 Replies

9. Shell Programming and Scripting

Array manipulation in perl

hi all, i am trying to append the output of a find command (for different paths)in an array as below... my $res_array; $i=0; $dir="/orn/ops/regs"; foreach $block("am","xb"){ $bdir="$dir/$block"; $res_array=`find $bdir ! -user mainuser -printf \"\%u \%h\\n\"`; $i++; } i... (6 Replies)
Discussion started by: saapa
6 Replies

10. Shell Programming and Scripting

Bash file manipulation help

Hello, I'm writing a bash script and I have a question. Here's what I'm doing: I have a file called inv.dat which contains the following: B102:Action Figure - Teacher:79 B103:Bike - Purple:23 B104:Baseball:25 B105:Cricket Bat:15 B101:Action Figure - Fireman:15 B100:Flame-Thrower:25 ... (2 Replies)
Discussion started by: netmaster
2 Replies
Login or Register to Ask a Question