3

In R, We have IN operator to check whether or not the element is present in the specific column.

For example: If we have fruits and market dataframe with fruit_name and products as the column name respectively. And, say, we have to check what fruits are present in the market.

In R,

available_fruit <- fruits$fruit_name %in% market$products

Is there any operator in bash or AWK which does similar action like %in% in R?

3

3 Answers 3

4

awk has an in operator. It may be used to access the indexes in an array (arrays are associative arrays/hashes in awk).

If the names of the fruits are keys in the array market then you may use

if (fruit_name in market) { ... }

to check whether the string in fruit_name is a key in market.

For example

BEGIN { FS = "\t" }

NR == FNR { market[$1] = $2; next }

!($1 in market) { printf("No %s in the market\n", $1 ); next }

{ sum += market[$1] }

END { printf("Total sum is %.2f\n", sum ) }

Running this on two files:

$ awk -f script.awk market_prices mylist

where market_prices is a two-column tab delimited file with items and prices, and mylist is a list of items. The script would read the items and their prices from the first file and populate market with these, and then calculate the total cost of the items in the second file, if they existed in the market, reporting the items that can't be found.

The in operator may also be used to loop over the indexes of an array:

for (i in array) {
    print i, array[i]
}

The ordering of the indexes may not be sorted.

3

Awk and Bash have associative arrays, which do provide a way to find if a certain key/index is in the array.

in awk:

awk 'BEGIN{ a["foo"]=1; if ("foo" in a) print "yea"; }'

in Bash:

bash -c 'declare -A a=([foo]=1); if [[ ${a[foo]+x} = x ]]; then echo "yea"; fi'

(${a[foo]+x} evaluates to x if a[foo] is set, to the empty string otherwise)


But you can't easily find if a particular value exists in the values of array elements. Here, a[1] is foo, but the test doesn't find it (it would find the indices 1, 2 and 3):

awk 'BEGIN{ split("foo bar doo", a); if ("foo" in a) print "foo exists?"; }'

you'll have to walk the array manually:

awk 'BEGIN{ split("foo bar doo", a); for (i in a) if (a[i] == "foo") print "foo exists"; }'

or in Bash:

bash -c 'a=(foo bar doo); for v in "${a[@]}"; do 
         if [[ $v = "foo" ]]; then echo "foo exists"; fi; done;'
2

In Awk, you can check if an index is in an array with the in operator:

$ awk 'BEGIN {arr["foo"]=1; arr["bar"]=2; print "foo" in arr}'
1

What this is doing is defining an array arr[] with two values: arr[foo]=1; arr[bar]=2.

Then, "foo" in arr checks if the index foo is in the set of indeces defined in such array. Since this is true, the print returns 1.

This is described in GNU Awk User's Guide → 8.1.2 Referring to an Array Element:

To determine whether an element exists in an array at a certain index, use the following expression:

indx in array

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .