Questions tagged [associative-array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
24 views

Replace last column of a piped result by the help of lookup array

I already have a shell command which creates lines like this one (from a long ss -an....|...|...): 5 10.1.1.20 3307 (Nb IP port) I have an echo command to simulate the answer: echo -e "5 10.1.1....
0
votes
1answer
40 views

How to retrieve items from an array of arrays?

Hello StackExchange pros! I am working on a zsh project for macOS. I used typeset to create three associative arrays to hold values, and a fourth array to reference the individual arrays. Is it ...
5
votes
2answers
198 views

Set a key with spaces in an associative array in Zsh

In Bash (4 or above), if I have an associative array dict, I can set its value like dict[apple count]=1 and I would be able to access it with ${dict[apple count]}. Does Zsh allow space in key names? ...
5
votes
2answers
844 views

Bash: value too great for base when using a date as array key

I have read about specifying "10#", but I don't think it's my case as I am not doing number comparison. I am trying to create an associative array in Bash, and the code worked fine until ...
16
votes
1answer
585 views

How to use associative arrays safely inside arithmetic expressions?

A few Bourne-like shells support associative arrays: ksh93 (since 1993), zsh (since 1998), bash (since 2009), though with some differences in behaviour between the 3. A common use is for counting ...
5
votes
1answer
198 views

In ZSH, how do I unset an arbitrary associative array element?

An associative array I have has arbitrary keys, including keys containing backquotes, brackets, etc: $ typeset -A arr $ key='`' $ arr[$key]=backquote $ echo $arr[$key] backquote I now need to unset ...
1
vote
1answer
255 views

How to create JSON from associative array

I am writing a bash script on CentOS 7.5 that will execute some MongoDB commands. One of these commands will set replication servers. According to project, number of servers can be different. I have ...
0
votes
0answers
17 views

Unexpected behavior during index assignment in a Bash array [duplicate]

I'm having trouble assigning values to a specific bash index, but apparently only when the index variable is set using a while read loop. Taking this code as a test example: #!/bin/bash read -d '' ...
3
votes
1answer
72 views

Indirect parameter expansion in associative array

Following this answer, I want to apply the approach on my script. The basics of it is: foo="bar" baz="foo" echo "${!baz}" bar I want to provide translated strings, like ...
3
votes
3answers
398 views

zsh testing existence of a key in an associative array via indirect expansion

So I know that you can test for the existence of a regular parameter via indirect expansion by doing something like: foo=1 bar=foo (( ${(P)+bar} )) && print "$bar exists" And I know you can ...
2
votes
3answers
708 views

Getting the key corresponding to a particular value in an associative array

I declare an associative array: declare -A array=([a]=blue [b]=red [c]=yellow) now I can do: echo ${array[@]} --> blue red yellow or echo ${array[b]} --> red or echo ${!array[@]} -->...
1
vote
0answers
485 views

How to make a simple menu in bash from an associative array

I am refactoring a script that displays a text menu similar to this: Select a mounted BTRFS device on your local machine to backup to. 1) 123456789abc-def012345-6789abcdef012 (/) 2) 123456789abc-...
0
votes
1answer
154 views

How to declare -A MYASSOCIATIVEARRY globally? [duplicate]

I have the same exact problem as described in this SO post ("bash associative array key string with colon is giving error"): https://stackoverflow.com/q/40406187/10639803 The solution is to use ...
2
votes
2answers
101 views

I am having difficulties with back-reference in awk

Recently, I am into security logs and want to make it better way on bash-shell. I found out in awk back-references are only stored by 9. But I need to use 10 back-references. Tried awk '{print ...
1
vote
1answer
39 views

I am having difficulties during manipulating log file with awk and shellscript [closed]

I have a difficulty with awk. I want to concatenate columns if first fields and 3field matches from 2 different files with awk. Probably, It'd better to use array function, I am very confused with it. ...
2
votes
1answer
2k views

how to combine 2 arrays into one associative array

i need to combine ARRAY1 and ARRAY2 into an associative array like ARRAY. i'm using this code: mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch) mapfile -t ARRAY2 < <(...
0
votes
0answers
78 views

bash: how to pass variables to a loop within a process substitution

How do you pass variable values within a process substitution to a loop within that process substitution? I'm reading a csv file into an associative array, and while reading each line, I would like ...
0
votes
0answers
18 views

associative array execution order problem [duplicate]

in a bash script i'm using an associative array to create tables, tablespaces, indexes and partitions in my database. this is the script (the main script is too long with complicated queries so i ...
2
votes
1answer
546 views

Global associative array only visible when the script is sourced a second time

I have the following script called .bash_functions.test which is already sourced by my .bash_functions script: # vim: set syn=sh noet: mp4Options_BIS="-movflags +frag_keyframe" declare -A ...
0
votes
1answer
668 views

updating one file based on values in another with AWK

I have two files file1.csv (20 columns 410k rows) and data.csv (4 columns 1800 rows). What I am trying to do is if data.csv 1st column matches file1.csv 2nd column overwrite 1st column in file1.csv ...
1
vote
1answer
81 views

Assoc. Array not being redeclared?

I have a program that acts like a menu. It has an associative array called config such as: declare -A config=( [h]="?" [c]="?" [x]="?" [l]="?" [t]="?" [n]="?" ) In the main loop there's a check to ...
2
votes
2answers
777 views

no matches found when using associative arrays in zsh

I am encountering no matches found when using map in zsh: #!/bin/zsh declare -A map=(["8761"]="Eureka服务" ["11001"]="用户微服务") Why would this happen, and how can I fix it? This is the error: ~/source/...
1
vote
2answers
617 views

Merge duplicate keys in associative array BASH

I've got an array that contains duplicate items, e.g. THE_LIST=( "'item1' 'data1 data2'" "'item1' 'data2 data3'" "'item2' 'data4'" ) Based on the above, I want to create an associative array that ...
0
votes
1answer
33 views

calling associative arrays [closed]

Team, am setting some variables in an associative array but its output is not resulting anything.. any hint?> #/bin/bash #IOEngine="psync" #TestType="read" IOEngine="libaio" TestType="randread" ...
2
votes
1answer
178 views

Create persistent array in bash

I want an associative array in my bashrc file and I want to add to or delete from it whenever needed, but if I put the array declaration in the bashrc file it will get redeclared every time bash is ...
7
votes
3answers
775 views

Inverting an associative array

Let's say I have an associative array in bash, declare -A hash hash=( ["foo"]=aa ["bar"]=bb ["baz"]=aa ["quux"]=bb ["wibble"]=cc ["wobble"]=aa ) where both keys and values ...
9
votes
1answer
2k views

Pass associative array as parameter list to script

In a script I have an associative array like: declare -A VARS=( ["key1"]="value1" ["key2"]="value" ) Is there a single command to transform that into a parameter list in the form --key1=value1 --...
3
votes
2answers
7k views

linux bash dictionary check if empty

How to check if a dictionary (associative array) is empty? I just declare one using declare -A dict. I want to know if it is just declared but not have any key.
3
votes
3answers
4k views

Making associative array based on another associative array

I made an associative array as follows. To give a few details, the keys refer to specific files because I will be using this array in the context of a larger script (where the directory containing the ...
4
votes
1answer
6k views

Bash return an associative array from a function and then pass that associative array to other functions [closed]

I'm trying to build an associative array in a function from a list pass in via an arg, but It just isn't working: #!/usr/bin/env bash function cwd { echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" &...
1
vote
1answer
3k views

What does declaring a bash array with -A do?

In Example_1, when i have declared city to be an array with declare -A, why is Bangalore output first when the array is printed in the for loop? Bangalore Remote Kolkata Ahmedabad Hyderabad Pune ...
26
votes
6answers
27k views

BASH associative array printing

Is there a way to print an entire array ([key]=value) without looping over all elements? Assume I have created an array with some elements: declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... ...
1
vote
0answers
379 views

zsh parameter expansion flag (P) with associative arrays

In the zshexpn manual page (zsh version 5.1.1) we have: Parameter Expansion Flags (P): This forces the value of the parameter name to be interpreted as a further parameter name, whose value will be ...
7
votes
4answers
4k views

Return the output of a command into an associative array

I need to put the output of a command into an associative array. For example: dig mx +short google.com Will return: 20 alt1.aspmx.l.google.com. 40 alt3.aspmx.l.google.com. 50 alt4.aspmx.l.google....
0
votes
3answers
5k views

AWK Compare Column 1 from Two Files Print append column to third in output

Looking to compare the first column of two input files that have an identical format. The format looks like the following: FILE1: 0000abc5abc3 GR096 0000def5ae87 GR001 0000cab5aea3 GR001 ...
2
votes
2answers
2k views

Retrieved value from associative array is wrong?

I've been going through the "Bash Pocket Reference" book and I got to the section about associative arrays. Now, trying the example code in the book gives me unexpected answers: istanev@inspiron5559:~...
4
votes
2answers
682 views

Bash -v test doesn't work with associative arrays in 4.3.46

The -v unary operator that was introduced in version 4.2 to test if a variable is set or not does not appear to work in bash 4.3.46 on associative arrays. I have some bash test code that I run ...
2
votes
4answers
8k views

Create an associative array from the output of two commands

I am trying to create user directories based on an imported passwd file, and am trying to load the data into an associative array: array[username]=directory . I can load the fields into a separate ...
9
votes
4answers
11k views

How to read a properties file into an associative array?

I'd like to read the properties in a properties file into an associative array. How can I do it? Specifics on what to parse: hash and equal signs. Everything else is a bonus. Properties file content ...
3
votes
1answer
6k views

Ordered by insertion Map in bash

Is there an ordered (by insertion) map in bash? I know there's an associative array data structure in bash, but when iterating over it, the order of the elements is not by insertion. Example below. ...
2
votes
1answer
4k views

Array indexing using “sed”

I am working on one shell script which search directory modified in last 24 hours and then compress them using tar. Folder Search : find /path to log directory/ -maxdepth 1 -type d -mtime +0 ...
1
vote
0answers
81 views

Linux Mint Corners Musical Sequence Associative Array

I am writing a script which uses hot corners to play specific musical notes when hovering in corners, and depending on the musical sequence to execute a specific command. The script is called with ...
7
votes
3answers
1k views

Set awk array on command line?

I know the -v switch can be used to awk on the command line to set the value for a variable. Is there any way to set values for awk associative array elements on the command line? Something like: ...
6
votes
1answer
14k views

Iterate bash associative array in Makefile

$ bash -version GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu) Consider the following shell script: #!/bin/bash declare -A PROVS=( ["NL"]=10 ["PE"]=11 ["NS"]=12 ["NB"]=13 ["QC"]=24 ["ON"]...
1
vote
1answer
1k views

Combine Bash associative arrays

I am trying make a script that combines arrays on demand. Here is the script: #! /bin/bash declare -A code code=( [H]="h" [E]="e" [L]="l" [P]="p" [M]="m" [E]="e" ) I need to print "help me" - ...
11
votes
4answers
34k views

Associative Arrays in Shell Scripts

I saw a trick for implementing associative arrays in a shell script. For example print array["apples"] could be scripted as echo \$array$key where key=apples. However, there was no mention of how to ...