Scanning array for partial elements in Bash Script

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Scanning array for partial elements in Bash Script
# 1  
Old 11-13-2016
Scanning array for partial elements in Bash Script

Example of problem:

Code:
computerhand=(6H 2C JC QS 9D 3H 8H 4D)
topcard=6D

How do you search ${computerhand[@]} for all elements containing either a "6" or a "D" then
save the output to a file?

This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash.

Any suggestions and advice would be Greatly appreciated.

Thank you,
Cogiz



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-14-2016 at 03:10 AM.. Reason: Added CODE tags.
# 2  
Old 11-13-2016
Quote:
Originally Posted by cogiz
Example of problem:

computerhand=(6H 2C JC QS 9D 3H 8H 4D)
topcard=6D

How do you search ${computerhand[@]} for all elements containing either a "6" or a "D" then
save the output to a file?

This is a part of a Terminal game of Crazy 8's that I'm attempting to write in Bash.

Any suggestions and advice would be Greatly appreciated.

Thank you,
Cogiz

Code:
for e in ${computerhand[@]}; do
    if [[ $e =~ 6|D ]]; then
        echo "$e" >> test.output
    fi
done


Code:
#!/bin/bash

computerhand=(6H 2C JC QS 9D 3H 8H 4D)
topcard=6D

search="${topcard:0:1}|${topcard:1:1}"
for e in ${computerhand[@]}; do
    if [[ $e =~ $search ]]; then
        echo "$e" >> test.output
    fi
done


Last edited by Aia; 11-13-2016 at 09:59 PM..
This User Gave Thanks to Aia For This Post:
# 3  
Old 11-15-2016
thank you very much. It worked like a charm

Cogiz
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array to array scanning

trying a little bit of array scanning for open ports. my code looks like below: /bin/netstat -lntp|\ awk 'BEGIN { split("25 80 2020 6033 6010",q); } $1 == "tcp" { split($4,a,":"); p]++; } $1 == "tcp6" { split($4,a,":");p]++ } END { for ( i in q ) { if (! q in p ) {... (8 Replies)
Discussion started by: busyboy
8 Replies

2. UNIX for Beginners Questions & Answers

Inaccurate scanning of Bash array elements

username=cogiz #!/bin/bash shuffle() #@ USAGE: shuffle { #@ TODO: add options for multiple or partial decks Deck=$( printf "%s\n" {2,3,4,5,6,7,8,9,T,J,Q,K,A}{H,S,D,C} | awk '## Seed the random number generator BEGIN { srand() } ## Put a random number in front... (4 Replies)
Discussion started by: cogiz
4 Replies

3. UNIX for Beginners Questions & Answers

Random choice of elements in bash array

example of problem: #!/bin/bash P=(2 4 7) How would you randomly choose one of these 3 numbers in this array? either 2 or 4 or 7 is needed...but only one of them. Thanks in advance Cogiz Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: cogiz
3 Replies

4. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

5. Shell Programming and Scripting

How to print elements between letters bash script?

Hello to all, May somebody help me to fix my bash code below, I'd like to do it only using loops and if statements is possible. I the content of the arrays are represented by the following 2 strings, where each character is an element inside the array. String for case 1:... (10 Replies)
Discussion started by: Ophiuchus
10 Replies

6. Shell Programming and Scripting

How can I print array elements as different columns in bash?

Hi Guys, I have an array which has numbers including blanks as follows: 1 26 66 4.77 -0.58 88 99 11 12 333 I want to print a group of three elements as a different column in a file as follows:(including blanks where there is missing elements) for.e.g. array element #7... (4 Replies)
Discussion started by: npatwardhan
4 Replies

7. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

8. UNIX for Dummies Questions & Answers

How can i read array elements dynamically in bash?

Hi friends how can we read array elements dynamically in bash shell? (1 Reply)
Discussion started by: haisubbu
1 Replies

9. UNIX for Advanced & Expert Users

Percent complete error while scanning RAID array during 5.0.6 load

Percent complete SCO 5.0.6 / No longer an issue (0 Replies)
Discussion started by: Henrys
0 Replies

10. Shell Programming and Scripting

how to return an array of elements from oracle to shell script

Hi all, I have a interresting problem. My application is as follows: From a shell script i will conn to a oracle database and fetch few rows using a select statement. Then i want to sort these rows and return a column (collection of values of a column from the selected results) as array... (3 Replies)
Discussion started by: satyakiran
3 Replies
Login or Register to Ask a Question