Questions tagged [array]

A array is the simplest data-structure for storing items in continuously memory

Filter by
Sorted by
Tagged with
1
vote
0answers
7 views

What means the definition of path and leaf array in jq --stream man page

In the jq manual page, --stream option, says: Parse the input in streaming fashion, outputing arrays of path and leaf values (scalars and empty arrays or empty objects). For ple, "a" ...
0
votes
1answer
31 views

Copying an array fails with sed (unterminated `s' command)

i'm trying to copy an array from a running bash script to an external file, but i run into trouble with sed. After hours of searching i still couldn't find an answer, so i thought i post a question ...
0
votes
2answers
39 views

Storing array as Environment variable to noninteractive shell with xargs

This is in continuation of Storing array as Environment variable to non-interactive shell - Unix & Linux Stack Exchange. Kusalananda, asked me to ask another question. I want to find all markdown ...
-1
votes
1answer
54 views

Storing array as Environment variable to noninteractive shell

There are several questions, which solve the problem, but I wanted to try the following approach: In this question I am basically having an array as an environment variable and then trying to store ...
0
votes
0answers
20 views

Print value from key in bash associative array

I'm trying to match the first three characters of a specific hostname to a key in an associative array. And then print the matching value. The code I have below prints all the values in the array. !/...
1
vote
0answers
24 views

Bash: Parameter Expansion | Parameter Transformation on List of Assoc.Array Keys

Is this possible(bash v5.0.3)? I'd like to dynamically populate a select list using the keys of an associative array (for further code reuse & script automation). Some keys simply require ...
0
votes
2answers
97 views

Shell script with a for loop and an “array”

How can I use this sh script with for loop and an array. I would like to call create condition for quality gate creation of sonarqube with a for loop. Example: #!/bin/bash --login echo "Creating ...
0
votes
1answer
27 views

Use bash to create array from files using ? wildcard? [duplicate]

I need to iterate through files in a directory that match a specific pattern using an array. For example: ./dirx/file1b_t1.tif ./dirx/file2b_z_t2_tif ./dirx/file3b_t3.tif ./dirx/file4b_x_t4.tif I'm ...
1
vote
2answers
160 views

Split string on newline and write it into array using read

Given string foo, I want to store each of its field seperated by \n into elements of array bar. And I want to do this using read command, or any other command that is compatible with older versions of ...
4
votes
1answer
41 views

In ZSH, what arithmetic expressions can appear as array subscript?

The ZSH manual (zshparam(1)) reads: Array Subscripts Individual elements of an array may be selected using a subscript. A subscript of the form `[exp]' selects the single element exp, ...
0
votes
2answers
59 views

Multiply element of bash array and set array element to the new value

In bash, if an element in an array is found to contain a K, I want to multiply that element by 1000 and set that element to the product. for i in "${stats_array[@]}" do if echo "...
0
votes
0answers
10 views

Writing a wrap around script to submit multiple versions of the same script on a HPC

I have a script that sets off a long workflow using wdl and cromwell which is limited to working on a single chromosome at any one time. For each chromosome the entire workflow needs to be in its own ...
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 '' ...
0
votes
1answer
65 views

If Bash arrays can't contain a null string, how does this array do it?

I've recently discovered that you can't pass a null string to a Bash array. This is tricky, as I'm trying to insert an empty string value into an array. I've noticed that the Bash completion variable ...
2
votes
2answers
157 views

2D bash arrays: is this possible?

If I want a 2D bash array, is there any recommended way of doing this for say, a N x M arrangement? On several occasions, I've had to define array1, array2, arrayN each of M elements in conjunction ...
0
votes
1answer
59 views

Bash array appending element issue

I have an array declare -a her=("ger" "blr" "tyg" "") for i in "${her[@]}"; do echo $i done I get ger blr tyg But when I try and append to an ...
1
vote
1answer
166 views

Using jq, how can I flatten to tsv and have nested array flattened as comma delimited value?

I have the following JSON format: { "page":1, "total_results":1, "total_pages":1, "results":[ { "id":6037, "genre_ids":[35,80,...
0
votes
1answer
22 views

Unable to print individual elements from an array

I am writing a test script to see how arrays work in an UNIX script. #! /bin/bash arr=(one two three) echo "${arr[0]}" I was expecting the above will print 'one' which is the 0th position ...
1
vote
1answer
34 views

How to define arrays in zsh with user supplied key names

I just started using zsh and I can't figure out how to define arrays with user defined keys. (I'm following the documentations on arrays: http://zsh.sourceforge.net/Doc/Release/Parameters.html#Array-...
6
votes
2answers
1k views

How to iterate over array indices in zsh?

In bash we can iterate over index of an array like this ~$ for i in "${!test[@]}"; do echo $i; done where test is an array, say, ~$ test=(a "b c d" e f) so that the output looks ...
2
votes
1answer
73 views

Would the inclusion of arrays in POSIX cause the POSIX standard to break?

The ability to use arrays in bash and ksh93 is very convenient when the need arises to quickly access elements from multiple arrays. Here is a quick example: #!/bin/ksh93 -- array1=(1 2 3 4 5 6 7) ...
1
vote
3answers
80 views

Indirect access in bash arrays

I am trying to do the following indirect task: host_1=(192.168.0.100 user1 pass1) host_2=(192.168.0.101 user2 pass2) hostlist=( "host_1" "host_2" ) for item in ${hostlist[@]}; do ...
0
votes
3answers
71 views

Remove elements from an array not containing a certain character

I have an array c_arr containing table columns with table aliases. But there are some elements in the array which are actually not columns and so they don't have the format alias.column_name. I need ...
0
votes
1answer
103 views

“2-dimension array” access in a loop in bash [duplicate]

I want to implement a selection menu in bash (version 3.2 on macOS) which would output something like this: Select a fruit: 0 Nothing 1 A banana 2 An orange When the user will choose an item,...
3
votes
1answer
208 views

What is the difference between (*) and (“$(ls)”)?

What is the difference between (*) and ("$(ls)")? Are they essentially the same except the delimiters are different?
2
votes
2answers
130 views

bash - Add an element before each item on array

I have an array containing strings to exclude with grep from the output of another program. I need to add an -e before each element. For instance: exclude=("$0" /usr/sbin/crond) needs-...
1
vote
1answer
59 views

appending values in a dynamic array

#!/bin/bash range=$(seq -f "cen%04g" 1052 1099) range1=$(seq -f "rh%04g" 1052 1099) check () { for node1 in ${range};do ping -q -c 1 -w 3 -s 10 $node1 >/dev/null if [ ...
2
votes
3answers
306 views

Save output of command in array

I am executing this command ar=($( ssh -i id_rsa -T -y root@1.1.1.1 sh -c "id;whoami;ps aux")) I need to save the output of each command executed into the array and call the array, but i ...
0
votes
1answer
42 views

Bash array arent adding elements to last

root@kali-linux:~/Softwares/Softwares/Tools/dirsearch# array=() root@kali-linux:~/Softwares/Softwares/Tools/dirsearch# for i in 1 2 3 4 > do > array+=($i) > done root@kali-linux:~/Softwares/...
0
votes
2answers
52 views

Why isn't $ARRAY+=$var working for me?

I am trying to add an element to a bash array. I looked at this question and tried to follow its advice. This is my code: selected_projects=() for project_num in ${project_numbers[@]}; do ...
2
votes
1answer
135 views

How to use IFS to create multiple arrays from a text file?

Suppose I have a text file filename.txt cat filename.txt abc#123 def#456 Now I want to use IFS in such a way that two arrays are created such as: source=(abc def) dest=(123 456) What would be the ...
1
vote
2answers
27 views

Searching a file for a string in an array

I am trying to check if items in an array options are in a file $file using the following simplified code: options=( A B C ) for i in "${options[@]}"; do if grep -F $file &...
1
vote
2answers
26 views

Why can't I convert a string variable into an array when some items include spaces?

For example, in the snippet below, (how) is it possible to make array2 identical to array1 while still using a str variable? ~$ { str='a "b c" d'; array1=(a "b c" d); array2=( $str ) echo "${array1[...
8
votes
3answers
1k views

Remove all empty strings from an array in Zsh

How do I remove all empty strings from a Zsh array? a=('a' '' 'b' 'c') # remove empty elements from array echo ${(j./.)a} Should output a/b/c
1
vote
2answers
158 views

Passing arrays in to the function in bash script

I am new to a bash script and writing the bash script. I have declared an array and I have given the values. But When I pass the array in the loop values are not getting inside the loop. Below is my ...
4
votes
3answers
597 views

What is the difference between double-quoting and not double-quoting an array in Bash?

While tracking down an error in my shellscript, I found the following behavior in this code snippet: declare -a filelist readarray filelist < <(ls -A) readonly filelist for file in "${filelist[@...
1
vote
2answers
250 views

Got bad array subscript when wring a function printing the fibonacci numbers

This is my code to write a function which prints the Fibonacci numbers function fib { fib_array=( 0 1 ) count=3 while [[ $count -le $1 ]] do fib_new=$...
0
votes
2answers
87 views

Filtering rows of a dataframe with AWK to only show entries in a given field with X instances

I have a large dataframe, in which one of the fields("ID") correspond to a unique identifier. However, there are multiple instances of that value, depending on the binary combination of additional ...
0
votes
1answer
384 views

Using cat on array elements

Is it possible to cat array elements that are text files passed as input? I have something like this: array=("$@") cat array[3] Where first I put all my arguments (some files.txt) into an array to ...
0
votes
0answers
17 views

Create iCal VEVENT's from all values in array

So I've figured out how to parse the JSON data from a site to get the dates and times for when I'm scheduled to be at an event, and I've created a template to append to a .ics calendar file. The ...
2
votes
3answers
402 views

How to sort a zsh array by modification date?

How to sort a zsh array by modification date? files=( ~/a ~/b ~/c ) # how to sort files by date? PS: Here is my exact usecase, (fz is almost fzf) v () { local files files=() command rg '^>' ~...
0
votes
3answers
366 views

using sed with variables defined in 2 arrays

I have followed the very many posts on this site regarding the subject but I am still clearly doing something wrong... My goal is to define values in two different arrays, then use sed to search a ...
0
votes
1answer
41 views

I would like to analyze a TXT file, but the data is a 1 column array, and it needs to be a in a multi-column array

Is there a way for me to manipulate the data (i.e.; change some feature of, so that it looks like it should, a multi-column array) without having to manually go through it? There are thousands of rows ...
0
votes
1answer
199 views

Extract CSV column containing blank space to Bash array

I would like to export a CSV bookmarks file as Physics,physics.stackexchange.com Stack Overflow,stackoverflow.com Unix & Linux,unix.stackexchange.com into two Bash arrays. Each array would ...
3
votes
2answers
320 views

Assign $@ to another variable in a shell function

I'm making an intelligent alias gc that can differentiate git commit/checkout. If gc is called without any arguments or with the -a, -m arguments, then git commit is run. Otherwise, git checkout is ...
0
votes
0answers
20 views

why does matching a value in an array work on the CLI but not in a script?

So I have an array: the contents of $tmp would be like "00-value" "00-derp" in the script the array is input into a variable and then later called: inst_array=$(cat $tmp) for x in "${inst_array[@...
0
votes
1answer
774 views

Pass Bash associative arrays as argument to jq filter

I am working with some location data and am querying an API with U.S. postal codes and returns a result like: { "resultStatus":"SUCCESS", "zip5":"30301", "defaultCity":"ATLANTA", "...
0
votes
2answers
146 views

Is there a way to modify $@ array in a POSIX shell script

I may know how to use readlink to get normal files from SymLinks. I had this very basic idea to replace any SymLink in $@ passed to my function as follows: for file in "$@"; do [ -L "$file" ] &...
1
vote
1answer
190 views

Copy bash array to a variable which name is hold by another variable

There are many similar questions, but I didn't understand any answer as solution of the following problem: I want to create a bash function which holds two arguments, first is a variable name and ...
-1
votes
2answers
151 views

Changing permission on multiple files within bash

I am trying to write a script to change the permissions of a number of other scripts, in multiple locations, to be executable. I've searched all over and can't seem to get the syntax correct. It is ...

1
2 3 4 5
11