Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [array]

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

1
vote
1answer
36 views

Split string by space in ZSH

Give this file.txt: first line second line third line This works in bash: while IFS=' ' read -a args; do echo "${args[0]}" done < file.txt To produce first second third That is to say, we ...
-1
votes
0answers
26 views

Perl - working with hashes and arrays for CSV processing

I have a perl script I am working on that I am using to process a csv file. There are 55 columns, and for this example, let's say 10 rows. Some records need to be grouped by a particular column. Some ...
2
votes
2answers
80 views

Add to array only within scope of function

I'm writing a function that will make a REST API calls which could be either GET, PUT, DELETE, POST, etc. I would like to feed this method to the function as a parameter and add it to the options ...
1
vote
2answers
138 views

Slicing the output of grep in bash

I'm trying to read a line of text in bash using grep, piped to tail to get the final line of the file, and then slice the first three "words" (i.e. dividing them using space) of that line as elements ...
7
votes
2answers
277 views

How do I specify chance when setting a variable to random item in array?

Currently I do something like this to set a variable to a random item in an array: array=("foo" "bar" "baz") var=${array[$RANDOM % ${#array[@]} ]} How would I do if I want to set $var to one of ...
8
votes
2answers
351 views

How can I create an array from the first line of multiple text sources?

I have an array of paths to files which each have several lines of text. I'd like to produce an array that is populated with the first line of each file processed like so: # this.txt first line is [...
-1
votes
1answer
34 views

Shell Function: Sequence of Pipelines as Argument

I have a shell function (in .bashrc) which creates a temp file, executes the arguments (including all sequence of pipelines), redirects it to a temp file and then open it in VS Code. I invoke the ...
0
votes
1answer
35 views

how to get the indexes of a referenced array in bash?

here is a sample code astr[10]=a astr[20]=b astr[30]=b astrR="astr[@]" echo ${!astr[@]} #the indexes echo ${!astrR} #the values #FAIL: echo ${!!astrR} astrR="!astr[@]" # wild guess echo ${!astrR} # ...
0
votes
2answers
58 views

Bash array script, combine

I'm trying to write bash script, which will automatically read all file names in current and custom directories, then apply new created docker image' names to finded yml files via kubectl, then read ...
1
vote
2answers
31 views

Shell script-How to return maximum value in array?

I have a array: ARRAY=(12.5 6.2) I wish to return the maximum value in ARRAY which Output is 12.5 Anyone can share me ideas? I have try this: max=0 for v in ${ARRAY[@]}; do if (( $v > $max ...
0
votes
1answer
36 views

Reading multiple files and operating on stored Arrays

I have 2 text files "${LinkP}" and "${QuestionP}. I want to read these files and store each complete line in the respective array, IFS=$'\r\n' GLOBIGNORE='*' command eval "LinkA=($(cat "${LinkP}"))" ...
2
votes
2answers
27 views

can bash show one array item id and value using `declare -p`?

$ str="a'\"b" $ declare -p str declare -- str="a'\"b" # see " was escaped, possibly other chars will too $ astr=("$str" "c") $ declare -p astr declare -ax astr='([0]="a'\''\"b" [1]="c")' so, is there ...
1
vote
1answer
33 views

kornShell and AIX unable to create an Array Variable

LS=`ls "$SRC_PATH"/* | grep -i -v *.ignore` I cannot use for loop as I need to check conditions with while as there are multiple conditions. Trying to access the content using ${LS[$CNT]} CNT=`...
0
votes
1answer
33 views

Manipulating Multiple word string in bash array

I have got a simple script as follows. names=("windows" "ubuntu" "raspbian" "debian" "kali linux") echo "names array length: ${#names[@]}" for n in ${names[@]}; do echo $n done My intention and ...
0
votes
1answer
34 views

Start range from 1 onward

I am counting multiple log files with the range starting from 1 onward however it's starting from 0. path=(/home/folder/text/*) for 1 int ${!path}; do printf "$s: $s\n" $i "${list[i]##*/}"...
2
votes
3answers
218 views

How do I sort an array with letters and numbers combined in bash? [duplicate]

I have an array with h4 h5 h1 h2 h3 in it and I would like to sort it according to the numbers, but don't know how. What is the best way to do this? edit1: I would also like to sort an array via the ...
8
votes
1answer
369 views

Referencing array elements by strings, and initialising arrays in awk

#!/usr/bin/env bash awk ' BEGIN { arr[A]=1; arr[B]=1; arr[C]=1; arr[E]=1; arr[J]=8; arr[Q]=10; print arr[J] }' the above command outputs the latest set value for arr['...
0
votes
2answers
42 views

Unix Script, problem with a 2d array

I have 2 arrays that refer to files for example alpha=file 1 beta=file2 Charlie=file3 delta=file4 beta2=file5 beta3=file6 Charlie2=file7 charlie3=file8 delta2=file9 delta3=file10 array1=("$alpha" "$...
0
votes
1answer
21 views

Different result for positional parameters expansion and custom array expansion

There is a difference in the output of commands that I don't understand, as stated below: INPUT="$@" echo "$INPUT ${#INPUT} ${#INPUT[@]} ${#@}" # outputs: a b c 5 1 3 arr=(a b c) echo "$arr ${#arr} $...
0
votes
2answers
78 views

bash: convert array into string preserving white spaces

I am just trying to convert an array into string, while preserving the white spaces. This is what I have: INPUT=$1 readarray -t arr < <(grep -o . <<< "$INPUT") echo "${arr[*]}" I ...
0
votes
1answer
24 views

fold: outputs only the first character

I am using fold to make an array of characters from a string. INPUT=$1 arr=($(fold -w1 <<< "$INPUT")) echo "$arr" echo "$arr[@]" I use command ./myProgram 1234 to execute the script. ...
3
votes
2answers
158 views

How to store (and load) a zsh String array to a file?

I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will ...
1
vote
3answers
83 views

bash array with space in element [closed]

I have a text log file $ cat aaa 673 20160405 root "/path_to/gis/20160401/20160301_placement_map_org.dbf" "" 673 20160405 root "/path_to/gis/20160401/...
0
votes
1answer
114 views

grep for multiple elements of an array

I am trying to write a dd migration script that will do the following: read user input (max=4) into an array, called "array". the user will enter the logical volume names to be migrated. once each ...
1
vote
2answers
85 views

grep using array values and make it faster

array[1] is a string pulled from a 30k lines CSV: example: samsung black 2014 I need match those lines with one of the values contained within an array (arrayItems). arrayItems contains 221 values ...
0
votes
1answer
56 views

Check that 2 arrays of file names are the same

So I have the following: cat file | grep -E regex which gives a list of lines Now I also have a for loop the produces a list of files So it is like I have 2 arrays. But how do I check in bash if ...
0
votes
1answer
44 views

Sed search and delete matching two patterns one of which is a variable

I have a file that contains multiple instances of code that I need to delete. Here is an example: !/bin/bash mkdir /rootdir/pipeline_runs/oncology/analysis/sample_1_NA172_1 cd /rootdir/pipeline_runs/...
0
votes
1answer
31 views

Array inside an Array: Different syntax for Array in bash

I found the following example from here. But I can't understand how is array arr is being defined. a='domain.de;de;https' $ arr=(${a//;/ }) What is the advantage of defining it like this? Actually, ...
-2
votes
1answer
31 views

Is a delimiter included in the field in front of it?

If I am correct, in bash, readarray uses some delimiter to separate the input into fields, and the delimiter defaults to a newline. Unlike a separator, a delimiter is included in the field in ...
0
votes
3answers
124 views

Why does double quoting an array expansion in here document not seem to work here?

I have two arrays $ arr1=( 1 2 2 3) $ arr2=( 2 3 3 4) Why does this generate strange output when I follow the good practice of double quoting an array expansion $ tsort << EOF > "${arr1[@]}...
9
votes
6answers
1k views

Bash sort array according to length of elements?

Given an array of strings, I would like to sort the array according to the length of each element. For example... array=( "tiny string" "the longest string in the list" "middle ...
0
votes
3answers
61 views

Duplicate elements of an array except the first and last elements

I have an array, and I would like to repeat each element except the first and last elements. For example if the array has five elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 ...
1
vote
1answer
265 views

Does readarray allow to specify line delimiter?

In bash read builtin has -d for us to specify line delimiter other than newline Does readarray provide some way to specify line delimiter? Is it correct that it has no an option for that purpose? ...
0
votes
4answers
39 views

Array + why arrays are not in the Right place

I set an array named “disks” to the following words - sdb sdc sdd in bash shell: declare -A disks Machine=worker01 disks[$Machine]="sdb sdc sdd" now we print all words as the following: echo ${...
0
votes
2answers
49 views

Bash - How to put an array inside values of variable?

I have a web.txt file which contains: youtube facebook google My sh file contains: readarray web < web.txt url = www.website.com Commands I have used: readarray web < web.txt url = www."$...
1
vote
2answers
56 views

How can I provide an array as a file argument to a script, so that each element is like a line in a file?

Suppose a bash script expects an argument to be a file. But we have a bash array, and would like to provide the array in some way as an argument to the script, as if there were a file whose lines are ...
1
vote
2answers
1k views

How to Use an array of strings in bash

I writte the following script load function should set the array disk[a]=1 and disk[b]=2 and so on Then the function out should print the array ${disk[a]} , and ${disk[b]} , and so on But what we ...
0
votes
1answer
35 views

bash share array in “for do () & wait” loop

I have an issue with the script below. It uses a few sql statements in a for loop to set variables and produces a log file. The current db produces +- 1200 lines of output. The original version is ...
3
votes
2answers
88 views

assign output of command to array

When I run this command: cat output | grep -i state | sort | uniq | awk '{print $ 3}') the output is: 00x1 00x5 0080 To assign them to an array, I did this: STATUS_ARRAY=($(cat output | ...
17
votes
2answers
828 views

What type of operation is /#/- in “${my_array[@]/#/-}”?

The accepted answer to Transform an array into arguments of a command? uses the following Bash command: command "${my_array[@]/#/-}" "$1" I'm trying to figure out what the /#/- part does, exactly. ...
1
vote
2answers
417 views

tar “Cannot stat: No such file of directory” when passing array variables [closed]

I'm trying to write a bash script that will create an archive (tar) of files in a directory. The file extensions need to be passed in as arguments when calling the bash script (./backup.bash pdf txt ...
-1
votes
1answer
186 views

How to put whiptail parameters into an array / loop? [closed]

I have a small but embarrassing / annoying issue with coding whiptail command. I am passing "$x" parameters to function for whiptail to process. It works as expected , but looks ugly. I would like ...
3
votes
2answers
212 views

Bash scripting Array concepts

I have two separate lists. One contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our ...
1
vote
2answers
58 views

to run a command for various variables in shell script

So, i'm storing the values of the first column returned from a sql query in a variable(var)..it's supposed to be an array type, because the sql query returns multiple rows, and later,these values ...
1
vote
1answer
34 views

How to delete existing elements from an array and keep appending new elements afterwards

Below is an example, where I am using two arrays which could be initiated with many elements, for now having "None" as the only element, is there any way to delete existing element/elements and then ...
1
vote
4answers
491 views

How to iterate through an array of numbers in shell-script?

I am trying to iterate through an array created after some command execution. The code used is: #!/bin/bash mailx -H|grep '^ [UN]'>ListOfMessages.txt msgNumbers=`cut -c 4-5 ListOfMessages.txt` ...
1
vote
1answer
57 views

Bash array using system positions limits? [closed]

Here is my array declaration arraySYSTEM=( "$@" ) It can have up to 10 "system position(?)" members. I'm using it to build text parameters passed to whiptail function. Then I select the parameter ...
0
votes
2answers
79 views

Find overlap/intersection of lists with bash

Say I have these two "lists": #!/usr/bin/env bash git fetch origin; first_list=( ); second_list=( ); git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do first_list+=(...
0
votes
1answer
62 views

command not found assigning value to array in bash

I'm trying to assign a value if the array at a certain index is empty, but I'm getting a command not found error. while IFS=$';' read -r -a array do # if empty set Not Available if [[ -z ${array[6]} ...
3
votes
2answers
3k views

jq - add objects from file into json array

I want to add an array with elements and value into an existing json file using jq. I already have a file (input.json) with { "id": 9, "version": 0, "lastUpdTs": 1532371267968, "name": "...