Search an array and return index (bash)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search an array and return index (bash)
# 1  
Old 11-09-2011
Search an array and return index (bash)

Hi all,

In bash, is there any way of searching an array and returning the index?

For example, how could I write a script that would do the following:
>> search note_array=(C D E F G A B) for F
return the value 3 (or 4)


Thanks,
R
# 2  
Old 11-09-2011
Like this?...
Code:
#!/bin/bash

arr[1]=A
arr[2]=B
arr[3]=C
arr[4]=D

srch="B"

for (( i=1;i<=${#arr[*]};i++ ))
do
        if [ ${arr[$i]} == $srch ]
        then
                echo "$srch found at index $i"
                break
        fi
done

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 3  
Old 11-09-2011
Thanks, Ahamed.

Rather than writing a script with a for-loop and if-statement, I was wondering if there was a single utility that could do it. I know in awk there's the index utility, but that only works for strings rather than arrays.

Is there an analogous utility for arrays?
# 4  
Old 11-09-2011
in awk you can split a string into an array and index the array:
Code:
$ nawk 'BEGIN { n=split("A|B|C|D",a,"|"); print a[3]}' < /dev/null
C

Array can contain ANY kind of data...
This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 11-09-2011
Thanks vgersh99.

Is there a way of searching the array and returning the index, though?
# 6  
Old 11-09-2011
Quote:
Originally Posted by RMontenegro
Thanks vgersh99.

Is there a way of searching the array and returning the index, though?
sure, but you have to search/iterate:
Code:
$ nawk -v s='C' 'BEGIN { n=split("A|B|C|D",a,"|"); for(i=1;i in a;i++) if (a[i]==s) {print i;break}}' < /dev/null

or you can invert the split array to make a truly associative to be index by your A|B|etc letters with value of 1,2,3,etc... And then you can simply do: invertedArray["A"] and it will return the 'index'.
But either way you have iterate through the array...

Or as an alternative... your string are in the file strings.txt:
Code:
A
B
C
D
E

you could do:
Code:
nawk s='C' '{a[$1]=FNR}END{print a[s]}' strings.txt

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Updated Forum Search Index Min Word Length to 2 Chars and Added Quick Search Bar

Today I changed the forum mysql database to permit 2 letter searches: ft_min_word_len=2 I rebuilt the mysql search indexes as well. Then, I added a "quick search bar" at the top of each page. I have tested this and two letter searches are working; but it's not perfect,... (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Copy of array by index value fails

Hello, I have a complicated situational find and replace that I wrote in bash because I didn't know how to do everything in awk. The code works but is very slow, as expected. To create my modified file, I am looping through an array that was populated earlier and making some replacements at... (6 Replies)
Discussion started by: LMHmedchem
6 Replies

3. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies

4. Shell Programming and Scripting

awk Search Array Element Return Index

Can you search AWK array elements and return each index value for that element. For example an array named car would have index make and element engine. I want to return all makes with engine size 1.6. Array woulld look like this: BMW 1.6 BMW 2.0 BMW 2.5 AUDI 1.8 AUDI 1.6 ... (11 Replies)
Discussion started by: u20sr
11 Replies

5. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

6. Shell Programming and Scripting

how to search array and print index in ksh

Hi, I am using KSH shell to do some programming. I want to search array and print index value of the array. Example.. nodeval4workflow="DESCRIPTION ="" ISENABLED ="YES" ISVALID ="YES" NAME="TESTVALIDATION" set -A strwfVar $nodeval4workflow strwfVar=DESCRIPTION=""... (1 Reply)
Discussion started by: tmalik79
1 Replies

7. Shell Programming and Scripting

dynamic index for array in while loop

Hi, I'm just trying to use a dynamic index for some array elements that I'm accessing within a loop. Specifically, I want to access an array at variable position $counter and then also at location $counter + 1 and $counter + 2 (the second and third array positions after it) but I keep getting... (0 Replies)
Discussion started by: weak_code-fu
0 Replies

8. Shell Programming and Scripting

awk array index help

$ cat file.txt A|X|20 A|Y|20 A|X|30 A|Z|20 B|X|10 A|Y|40 Summing up $NF based on first 2 fields, $ awk -F "|" 'BEGIN {OFS="|"} { sum += $NF } END { for (f in sum) print f,sum } ' file.txt o/p: A|X|50 A|Y|60 A|Z|20 (4 Replies)
Discussion started by: uwork72
4 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question