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

0
votes
1answer
31 views

List with two values in every List Item

I want to store a list with name and email address and use it in the single for loop. And I want to do it in Shell. Knowing that shell list has a white space separator list = "john, john@gmail.com" "...
1
vote
1answer
20 views

korn shell - if a variable exists in an array stop processing without exiting

How would one evaluate priority of items in an array such that you only process the highest priority item that exists? Let me try to explain, if I had an array set -A array low medium none high ...
3
votes
4answers
80 views

bash command to create array with the 10 most recent images in a dir?

I'm writing a bash script and I need to create an array with the 10 most recent image files (from new to old) in the current dir. I consider "image files" to be files with certain extensions, like ....
1
vote
1answer
33 views

make an array from a dynamic column in the middle of text

I need to know how many connection from an application is connected to my database. for that i should run an stored procedure. the output is like: CMD_EXEC: 1 IFCA_RET: 0 IFCA_RES: 0 ...
0
votes
1answer
49 views

printf outputs rows, not columns

I have two arrays, which I want to print out in two separate columns from a single printf call. The following: printf "%-${padding}s→ %s\n" "${lnFrom[@]}" "${lnTo[@]}" Outputs: dotfiles/git/...
0
votes
2answers
33 views

Pass the name of an array in command line to reference the array in a function

I'm trying to learn more bash by updating my bash_profile so that I can quickly do some adb commands that I usually have to copy-paste. I found I was creating many similar functions that all looked ...
0
votes
1answer
64 views

Storing output of awk to an array and print it to a file with comma delimiters

I am trying to use bash to go through multiple directories (sims) and search for a given string, setting each index of an array to its relevant output, then print that output with comma delimiters for ...
6
votes
5answers
607 views

Using parameter substitution on a Bash array

I have file.txt that I need to read into a Bash array. Then I need to remove spaces, double quotes and all but the first comma in every entry. Here's how far I've gotten: $ cat file.txt 10,this 2 0 ...
1
vote
2answers
43 views

using a variable as array name in TCL script (array)

The question is about TCL Arrays. In my TCL script, variable1 is a variable value obtained from other functions(value is something like PATH_xyz.) variable2 is another variable obtained from other ...
0
votes
2answers
51 views

Bash not appending array to new index. Just extends first index

I am using arrays in bash and one particular array is behaving unusually. I am using a function and calling an external script which returns a value to be appended to an array as follows: function ...
1
vote
1answer
307 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 ...
2
votes
2answers
87 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
177 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 ...
6
votes
2answers
281 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
486 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
42 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
116 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
47 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
86 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
33 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
43 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
87 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
35 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
387 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
482 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
46 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
23 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
158 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
39 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
199 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
180 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
264 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
114 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
62 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
91 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
53 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
32 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
186 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
77 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 ...
2
votes
1answer
533 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
40 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
57 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
2k 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
38 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
132 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
839 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
588 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 ...