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?
Best Answer
awk
has anin
operator. It may be used to access the indexes in an array (arrays are associative arrays/hashes inawk
).If the names of the fruits are keys in the array
market
then you may useto check whether the string in
fruit_name
is a key inmarket
.For example
Running this on two files:
where
market_prices
is a two-column tab delimited file with items and prices, andmylist
is a list of items. The script would read the items and their prices from the first file and populatemarket
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:The ordering of the indexes may not be sorted.