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

learn more… | top users | synonyms

5
votes
1answer
195 views

Single parenthesis in bash variable assignment

So this might seem like a stupid question, but I kinda have OCD about this kind of stuff and I was wondering about single parentheses in bash. I know that they are used for executing commands in ...
3
votes
1answer
209 views

Loop through a multidimensional array in bash 4

I want to define a hash list in bash (version 4.3.30): 4 gateways each gateway has an IP an IP6 a name ... and I want to walk through this list in a loop and do stuff to each gateway. I ...
1
vote
1answer
27 views

Handle wildcards matching no file in bash

I am trying to read files from a directory into an array but even when the file doesn't exist, its saved in array. I want to exclude the file name if it doesn't exist. a=(/tmp/nofileexists) ...
3
votes
4answers
221 views

Processing array values in bash

I am trying to create an array based upon filenames, and get in trouble with whitespaces. This seems common. But - as far as I can see - the quotes are set correctly, I guess it must be the way the ...
4
votes
3answers
58 views

Go from a String to an Array of “words” in Bash

I need to go from a string to an array where each entry is each word on that string. For example, starting with: VotePedro="Vote for Pedro" I need the array: Vote For Pedro Which I should then ...
1
vote
3answers
224 views

Put big data of heterogenous byte offset into arrays by AWK

Assume the data consists of byte offset which is not fixed i.e. the distance of two subsequent file headers varies. The point of this thread is to go through each size of events separately in arrays. ...
5
votes
2answers
126 views

In a loop over an array, add an element to the array

I have a problem with for loop in bash. For example: I have an array ("etc" "bin" "var"). And I iterate on this array. But in the loop I would like append some value to the array. E.g. array=("etc" ...
3
votes
1answer
45 views

bash silently does function return on (re-)declare of global associative read-only array

Obviously cut out of a much more complex script that was more meaningful: #!/bin/bash function InitializeConfig(){ declare -r -g -A SHCFG_INIT=( [a]=b ) declare -r -g -A SHCFG_INIT=( [c]=d ) ...
1
vote
2answers
68 views

How to add all values in an array

I have been stuck on this for way to long now. Tried googling and couldn't find what I was looking for. I simply need to add all the values in an array.(array called packets) I have gotten to the ...
2
votes
1answer
24 views

Why does this array related code print the index at the end of each line?

I'm having problems working with an array in BASH. I've simplified the problem down to the following code: #! /bin/bash A1[0]="user1 user2 user3" A1[1]="user4 user5 user6" for each in ${!A1[*]} ...
2
votes
1answer
32 views

How is the order for which keys are stored and reteived determined in awk?

I am just starting to learn awk and was familiarizing myself with associative arrays only to discover that the order in which the keys were retrieved (or stored) didn't make a whole lot of sense. As ...
1
vote
3answers
76 views

Find the complement set of an array?

Following from this question, where I wish to extract 10 random lines from a file, I now wish to also have the remaining 90 lines as a separate file. Since the document has 100 lines, indexing from 1 ...
1
vote
2answers
42 views

Re-print an array in a certain format?

I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with ind=$(shuf -i 1-100 -n 10 | sort -n) Then, I wish to use ...
2
votes
3answers
76 views

How can I prepend and append to each member of an array?

I have an array: CATEGORIES=(one two three four) I can prepend to each array member using parameter expansion: echo ${CATEGORIES[@]/#/foo } I can append to each array member the same way: echo ...
2
votes
2answers
59 views

bash + how to define array variable with instance number

Is it possible to define variable that is called for example machine1 as machine$counter ( while counter=1 ) ? For example, I created the /tmp/config.txt file and set the machine1 as array: $ more ...
1
vote
1answer
72 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" - ...
1
vote
2answers
135 views

Dynamically create array in bash with variables as array name

This has been asked several times, but none of the methods work. I would like to dynamically create arrays with array names taken from the variables. So lets start with just one array for now: ...
0
votes
2answers
29 views

How to print arguments from a known element until an unknown element of an array with Bash

I am having a blonde moment. This is my data from myfile.csv 1429829254,e,SE,StckXchg,HDCU3000623,d,scan,253,47968,94,1420824420,JSSE-213,199,BS,KIT,*OUT* ...
4
votes
3answers
1k views

Is there a way of reading the last element of an array with bash?

If I have an array with 5 elements, for example: [a][b][c][d][e] Using echo ${myarray[4]} I can see what it holds. But what if I didn't know the number of elements in a given array? Is there a way ...
1
vote
1answer
52 views

create an array

I want to learn about arrays and how to assign values to them, so I found this tutorial While running the following script: #!/bin/bash $names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad ...
-1
votes
1answer
71 views

Accessing array elements within process substitution bash

I've got a text file which looks like this: b4238ca2-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress b4238f0e-cb8d-11e4-8731-1681e6b88ec1,https,username,password,ipaddress ...
1
vote
1answer
41 views

Setting Qualifiers for Bash Array

I have a bash script that is running an SNMPGET of two values. I want to take the results and put them in an array. Here is the code: OUTPUT=`snmpget -v2c -c public -Oqv 192.168.0.33' \ ' sysName'\ ...
1
vote
2answers
63 views

Splitting the working directory in a bash script

If a do: IFS="/" read -ra PARTS And type in a path manually, it creates the array "PARTS" as hoped, however: IFS="/" read -ra PARTS <<< $(pwd) creates an array with a single element, ...
3
votes
3answers
187 views

Bash : Native way to get rid of quotation around each array member

I read an array from another script. This array needs to put " " around all array members since some members are empty. in_file=./data vector=($(./readdata.sh 0 $in_file)) for index in ${!vector[@]} ...
2
votes
1answer
850 views

how to count the length of an array defined in bash?

I'm new to bash and can't find a good tutorial to answer my question. array=( item1 item2 item3) for name in ${array[@]}; do echo current/total ... some other codes done I want to calculate ...
1
vote
2answers
60 views

Read file remove spaces and store in array

I have a file with the following content: list: blue,none red,none orange,plot baseball , none university,none school,none desk,plot monitor,none earphone,none I need to read this file, ...
1
vote
3answers
66 views

access elements in string array - stray @ symbol

I have created an array (3 elements), and each element contains a comma delimited string. The array was created by reading a file line by line - the file contains fields output from a database. I have ...
2
votes
2answers
174 views

Create array in bash with variables as array name

I'm not sure if this has been answered, I've looked and haven't found anything that looks like what I'm trying to do. I have a number of shell scripts that are capable of running against a ksh or ...
2
votes
1answer
55 views

Push onto array with find exec

I want to iterate over all the files found by find and add each one to an array. Here's what I have so far: myarray=() find . -name '*.php' -exec myarray\+=\({}\) \; echo "${myarray[@]}" Instead, ...
4
votes
1answer
440 views

Bash Shell Script Array Length Off By One

The length of an array in my bash shell script appears to be off by one. I have 11 elements in a text file that I am reading in to an array, but my array length seems to be 12. (( count = 0 )) while ...
1
vote
1answer
136 views

Processing output from an sqlite db into a ksh array with spaces

I am querying an SQLite3 database like so: input=$(-separator "," "SELECT field1,field2,field3 FROM table1") and get this result: Red,Yellow is a color,Blue I need to insert this into an array, ...
3
votes
1answer
157 views

How to sort an associative array and retain the keys?

I have an array with filenames as keys and a numerical value as values. MYARRAY[00001.jpg] = 31 MYARRAY[00002.jpg] = 200 MYARRAY[00003.jpg] = 98 I need to sort them so they are ordered by value. ...
0
votes
2answers
83 views

Multiple variable expansion modifiers in the same expression

Why does the following idiom not work in bash 4.1.0? if [[ "${FUNCNAME[*]:1/$FUNCNAME/}" != "${FUNCNAME[*]:1}" ]] Here it is in context... function isCircularRef_test () { # ### Seems like ...
-1
votes
2answers
75 views

if else statement

I have a case statement inside some of the if statement, but after doing the case statement inside the if statement, the loop ends. After the case statement, it is supposed to go to the next value on ...
1
vote
1answer
45 views

Whats wrong in creating/printing this array?

To find the average time taken to create certain files, I'm using this minutes array, then would simply use bash arithmetic to find the average. However I'm unable to get the difference except for the ...
2
votes
3answers
292 views

Loop over associative arrays by substring

In the following code, I create some associative arrays in a loop. The consist of two strings, a string identifier and a year. After creation, I want to access the arrays in a loop based on the ...
2
votes
1answer
37 views

CInt Configure Script: Syntax error at left parenthesis

I am trying to install CInt. When running ./configure, I get the following error: ./configure: 23: ./configure: Syntax error: "(" unexpected Here is the relevant section of configure: # configure ...
0
votes
1answer
597 views

Bash script: array elements containing space character [duplicate]

I'm getting started with bash scripting. I'm working with array elements that contain space characters. In the code below, the third array element is "Accessory Engine". I tried to set the space ...
1
vote
1answer
151 views

Why does the printf statement in this loop output the array out of sequence?

When I execute the following code, the output of the printf statement appears to show array out of sequence. In the declare statement the Source item is declared before Destination and in the output ...
0
votes
2answers
172 views

Delete last character of last item in a bash array

I have such array: Array={123},{456} Now I want to delete the last item 6.
1
vote
1answer
449 views

Compare two Arrays in KSH and output the difference

I am not extremely familiar with KSH (Actually just started using it) and I am having problems with trying to create a script that will essentially compare two arrays that have been stored and then ...
0
votes
1answer
144 views

Insert variable value as element in array

If I have a variable that has a value stored and I want to place that value into an array using the name of the variable, how would I do this? e.g.: variable="Hello there" array[0]=$variable does ...
1
vote
3answers
68 views

Use a variable as part of name of an array name?

I essentially have a for loop where the variable i that I am iterating with will take on each letter of the alphabet, for example. I want to use each value of i to create an array called "$i"array ...
0
votes
1answer
58 views

For value falls within a range get corresponding value

I am thinking to use awk to search a value from input file in a reference file and get a corresponding value from reference file. Both reference and input files can be sorted based on $3 and $2 ...
2
votes
2answers
1k views

parse one field from an JSON array into bash array

I have a JSON output that contains a list of objects stored in a variable. (I may not be phrasing that right) [ { "item1": "value1", "item2": "value2", "sub items": [ { ...
1
vote
3answers
1k views

How do I test if an item is in a bash array?

Help for a simple script #!/bin/bash array1=( prova1 prova2 slack64 ) a="slack64" b="ab" if [ $a = $b ] then echo "$a = $b : a is equal to b" else echo "$a = $b: a is not equal to b" ...
2
votes
2answers
232 views

Create arrays with brace expansion in loop

I would like to generate a number of arrays that differ only by year. In the loop, I create the arrays with brace expansion and a variable. I tried the following code without success: ...
2
votes
0answers
57 views

Generate array of strings combined with number

I would like to generate a local array in a shell/bash script that consists of the following elements: JF-1998, JF-1999, JF-2000,... , JF-2011. That is, a string JF- combined with the years from 1998 ...
1
vote
1answer
99 views

awk array using number as value did not work

I have a reference file: Refrence File Dpse\GA30012 FBgn0000447 chr2 26607738 26607962 -1 Dpse\GA19764 FBgn0085819 chrX 28571020 28571736 -1 Dpse\ttk FBgn0000100 chr2 ...
0
votes
2answers
981 views

How can I store each seperate entire line in a text file into an array?

I have a file called "threewords". It contains the information: \#gray speedy bee gr-A | sp-E-d-E | b-E \#gray greedy pea gr-A | gr-E-d-E | p-E When I run the command: cat threewords | grep ^# ...