Questions tagged [array]

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

Filter by
Sorted by
Tagged with
8 votes
2 answers
1k views

In bash, how do I get the index of the last element of an array without a loop

In bash, is it possible to get the index of the last element of an array (that might be sparse) without looping through the entire array like so: a=( e0 e1 ... ) i=0 while [ "$i" -lt $(( ${#...
user avatar
  • 367
3 votes
1 answer
108 views

Cut or Awk to save lines on array in bash

I have a scenario that I have to put lines of a file .txt inside an array, example of file content; type of host | | first host | | | | second host | | | | | | same ip ...
user avatar
1 vote
1 answer
40 views

Ansible convert dictionary to mix of dictionary/array

I use following files: variable file: db.yml x86_64: alpine: version: 3.15.0 debian: version: 11.3.0 aarch64: alpine: version: 3.15.0 debian: version: 11.3.0 playbook: ...
user avatar
0 votes
1 answer
35 views

convert a uint8 array ( javascript new Uint8Array() ) to binary format (or binary mode ?) in shell / bash?

The question is about convert from new Uint8Array() generated from javascript into a normal file (file that can be used with other programs like ffmpeg) ** I don't know the right term to use if it's ...
user avatar
  • 115
0 votes
1 answer
82 views

Counting file extensions in a string array

I have a $FILES array which consists of strings of 4 filenames: FILES=$(ls /my/directory | shuf -n 4) The contents of the array look something like: file1.jpg file2.jpg file3.gif file4.mp4 I want to ...
user avatar
  • 11
2 votes
5 answers
254 views

match value2 in 2 files if value 1 is exact match

I have 2 files containing list. Column 1 is userIds & column 2 is associated values # cat file1 e3001 75 n5244 30 w1453 500 #cat file2 d1128 30 w1453 515 n5244 30 e3001 55 Things to consider. ...
user avatar
  • 1,613
5 votes
2 answers
403 views

how to test if index exists in array

I'm writing a Git Bash utility that copies a project folder from one location to another. There are multiple destinations to which the user may want to copy the project, though only one location per ...
user avatar
  • 157
3 votes
1 answer
125 views

Trying to put output of grep command into a bash array

If I run the following grep command I get the output I am expecting: $ grep -o -E '/ |:|\\|<|>|\*|\?|\"| $' <<< '/home/ttwaro/temp/Test/This is a Test: Testing Number 1?/This is ...
user avatar
  • 31
0 votes
2 answers
62 views

awk matching fields in 2 separate files using command containing {BEGIN FS = "|"} returns output of blank lines

Two files: data1 Name |formula |no. |dose|days|cost |msg|em|notes Fname-Lname|BXXXT+GG |8262|4 |14 |57.78 | | |sq Fname-Lname|SJXXT+GG |8263|4¾ |...
user avatar
  • 25
1 vote
0 answers
121 views

Create a tsv file with data from several files

How can I create a tsv file from multiple files? I want to be able to create a tsv file from several different files. One sample is associated with five 'region' files that have data in them. For ...
user avatar
  • 23
0 votes
1 answer
103 views

Getting the first/last/nth result from a jq select result

I am trying to find a way to pipe the result of my current script into another command which will leave me with just the last result of the array I get in my select here: jq -r --arg name "$1&...
user avatar
  • 1
-1 votes
1 answer
40 views

write bash code loop a file line by line that have values, first word as a row and create csv [closed]

i have this sample data dn; uid=456456456,cn=users,dc=efsfb,dc=org,dc=co; objectclass; top; objectclass; person; objectclass; organizationalPerson; objectclass; inetOrgPerson; uid; 456456456; ...
user avatar
  • 101
-3 votes
1 answer
43 views

Difference between two array printing methods in bash [duplicate]

I have an array declared in my script. NAME[0]=Deepak NAME[1]=Renuka NAME[2]=Joe NAME[3]=Alex NAME[4]=Amir echo "All Index: ${NAME[*]}" echo "All Index: ${NAME[@]}" There are two ...
user avatar
  • 5
3 votes
1 answer
70 views

passing tab separated entries to readarray

Obviously I'm not doing it right, but I think the intended outcome is clear (${#arr[@]}=3) $ readarray -d "\t" arr < <(printf "%s\t%s\t%s" "x" "y" "z&...
user avatar
  • 481
1 vote
2 answers
330 views

How to pass an array as function argument but with other extra parameters?

The following post solution works as expected: How to pass an array as function argument? Therefore - from his answer: function copyFiles() { arr=("$@") for i in "${arr[@]}"...
user avatar
0 votes
1 answer
23 views

bash :: use imported array

myVAR='myArr' . ./myArraysFile # external file with all my arrays, one of the arrays is myArr=('one' 'two' 'three') I need to use this arrey with further scripting Examples: echo ${!myVAR}[1] # I ...
user avatar
  • 3
0 votes
1 answer
113 views

What is the difference between ${array[*]} and ${array[@]}? When use each one over the other? [duplicate]

With the following code: #! /bin/bash declare -a arr=("element1" "element2" "element3&...
user avatar
1 vote
1 answer
47 views

AWK: Add missing lines in csv file and set specific column to "0"

(Note: this question is heavily shortened because the detailed question was blocked with "This looks like spam.") I'm having a hard time adding missing lines to a series of data in a ...
user avatar
3 votes
3 answers
114 views

Store expanded array in a variable using a brace expansion

I want to store node1 node2 node3 in a variable so that it may be used as an input to other scripts. However, I want to do so by using brace expansion given by bash like so: node{1..3} I tried to have ...
user avatar
0 votes
0 answers
21 views

Bash array append - removes existing elements [duplicate]

I tried: $ a=("-f" "apple" "c") $ a+=("-e" "banana" "d") $ echo "${a[@]}" -f apple c -e banana d $ a=("-e" "apple&...
user avatar
  • 1,362
0 votes
3 answers
56 views

Two arrays in nested for loop over ssh lead to unwanted connection-user permutations

I am trying to update some files on 3 nodes, where I want to combine every connection with a specific user path. As I am very new to this, I extended a for loop I used to connect as root, into a ...
user avatar
7 votes
1 answer
292 views

Unary test -v on an array element

In bash, the conditional expression with the unary test -v myvariable tests wether the variable myvariable has been set. Note that myvariable should not be expanded by prefixing it with a dollar, so ...
user avatar
  • 277
0 votes
3 answers
216 views

How do I return an element from a bash array using a partial string match?

I have a bash array containing several elements, only one of which will be in the format nn.wr , where nn is a variable integer. eg array=( "abcd" "01.wr" "ef_gh" ) ...
user avatar
0 votes
2 answers
76 views

For-loop - appending to arrays with iterator in the array name

I have the following problem. I have an array arr with some values. I want to sort each value into a set of different - and already declared - arrays earr$j, i.e. arr[0] into earr1, arr[1] into earr2 ...
user avatar
2 votes
0 answers
66 views

for loop and appending over list of arrays

I am new to Bash scripting and presently find myself dealing with a small problem in working with for loops, arrays and variable assignment/substitution which I do not know how to solve. Since I am ...
user avatar
2 votes
1 answer
91 views

zsh pipestatus disappears in the following prompt

I am on zsh 5.8 (arm-apple-darwin20.2.0). Unlike the way PIPESTATUS in bash is available to read during the next shell prompt, pipestatus of pipe in zsh seems to disappear in the following prompt. ❯ ...
user avatar
0 votes
1 answer
77 views

Find and manipulate array element in KSH

I have an array that reads like this: # echo "${ARRAY[@]}" 1=Napp Slice 4=NNN Issue 3=GG Allow 2=CO-OP 1=Quit And I want to loop through it to see if any contain the word Quit and if so, ...
user avatar
2 votes
1 answer
24 views

Loop through file with unique identifer and compute cumulative sum

I have a large text file, as shown below, where column1-colum3 define a region and fourth column is unique identifier. The column 6 is assigned value of the region. chr6 26204834 26204839 ...
user avatar
  • 105
0 votes
3 answers
58 views

How i should change this to use array?

i just writing a script for make a Symbolic Links, but i think i have too much line can be much less with array, but i don't find an example with an explaniation for put multiples path in array. The ...
user avatar
  • 29
0 votes
0 answers
14 views

How to extract only not qualified files based on fileformats in array

I have trouble with the code below. The first part of the IF-statement works fine. All valid files that has file formats listed in the array arr are printed on the screen. However the ELSE-statement ...
user avatar
1 vote
0 answers
29 views

Is it possible to mount an ATA over Ethernet (AOE) block device on multiple clients, if so how?

I have a lab consisting of 3 machines connected with 2 10gbe links on 2 segregated networks. Each device has 100tb in block storage connected to it. I want to use ATA over Ethernet to create a storage ...
user avatar
  • 111
0 votes
1 answer
58 views

Access element of array

I have the following script which retrieves info from mysql #!/bin/bash set -f IFS=$'\n' arr=($(sudo mysql -u root -h localhost -e "USE mydb;SELECT * FROM users")) for i in "${...
user avatar
  • 3
8 votes
2 answers
3k views

how to jq nested array without key (only value) and convert to csv

I hope someone can guide me how do I convert below json to csv that I'm expecting for. Much appreciated in advance. Update: thanks for the solutions provided, but I found that sometimes array does not ...
user avatar
0 votes
1 answer
294 views

Why is echo needed for IFS to take effect in zsh?

I am trying to read members of the PATH into an array using zsh 5.8. I am used to Bash, where the following normally works: IFS=: p=($PATH) However with zsh, this actually gives me an array with only ...
user avatar
  • 1,843
1 vote
1 answer
390 views

Bash - mix/merge/combine two different arrays with same length

I have two different arrays with the same length: s=(c d e f g a b c) f=(1 2 3 1 2 3 4 5) how can I mix/merge/combine this two arrays, so I would get this output: c1 d2 e3 f1 g2 a3 b4 c5
user avatar
  • 4,740
1 vote
1 answer
555 views

Putting string with newline separated words into array

I currently have a a bash variable that holds a string similar to this one, where each different phrase is separated by a newline: var="1st word 2nd word 4th word" Note there is an empty ...
user avatar
  • 77
1 vote
2 answers
312 views

Finding duplicates and their indices in an array in Bash

I want to find the duplicates in an array and their indices using bash. For example, I have this array: arr=("a" "b" "c" "a" "c") In this case, "...
user avatar
  • 77
0 votes
2 answers
375 views

Is it possible to use "xargs" to append elements to an array?

I have an empty array arr=() Can I use xargs to append elements to this array? Like this: echo "test" | xargs arr+=()
user avatar
  • 1
0 votes
1 answer
50 views

Appending to local array

Have defined an array local to a function isufx+=( -name "*$fltyp" -o ) When I call the function again, the previous values in array isufx are not being cleared and the array unset. ...
user avatar
  • 317
0 votes
2 answers
695 views

Removing last element from bash array

I am calling find by constructing an array isufx containing filename suffixes. Thusly, I have echo "isufx: ${isufx[*]}" that results in -name *.texi -o -name *.org -o Finally I got the ...
user avatar
  • 317
2 votes
2 answers
496 views

Mapfile not removing trailing newline

Sample Data: Tab separated tsv file a.1.58 fadado/CSV https://github.com/fadado/CSV a.1.63 jehiah/json2csv https://github.com/jehiah/json2csv a.1.80 stedolan/jq https://github.com/stedolan/jq/...
user avatar
  • 1,362
0 votes
0 answers
34 views

Removing delimiters from bash array elements, then transferring result to another array

I am using an option variable array incl that stores filename extensions. ("--incl") local incl+=("$2") ; shift 2 ;; The function can then be called using --incl .texi --...
user avatar
  • 317
2 votes
1 answer
80 views

Can't access elements of an array built from readarray

I'm trying to build a basic REPL in bash. The script dynamically populates a list of files in a directory for the user to run. File space: | |\ scripts/ || script1.sh || script2.sh | \ shell/ | ...
user avatar
1 vote
1 answer
164 views

string from array

I have an array named nmser in a bash script and I would like to make a string from it and store it in the variable flsel. flsel=echo "${nmser[*]}"
user avatar
  • 317
1 vote
1 answer
408 views

Write values with embedded commas from arrays into CSV file using BASH

I am trying to write different arrays into a csv file ($file) columns. Each array has the same number of values. For example, arr1 and arr2 have 3 values each arr1=( 23 45 34 ) arr2=( "abc,d"...
user avatar
  • 11
0 votes
1 answer
66 views

Bash array which contains environment variables to be used as function arguments

I am trying to store some function parameters in a bash array, but some of them are defined from the content of environment variables and they do not actually "expand" to their actual values ...
user avatar
  • 213
0 votes
1 answer
68 views

Write a bash script that displays, for each file given as an argument, the line that appears the most

Write a bash script that displays, for each file given as an argument, the line that appears the most. The display will be made in descending order of the number of occurrences. I'm not sure how to ...
user avatar
  • 3
7 votes
2 answers
607 views

Bash variables do not expand inside array if declare is used

Recently I decided to read a bit more on the bash built-ins declare, local, and readonly, which led me to switch from: local variable_name variable_name='value' readonly variable_name To: ...
user avatar
0 votes
0 answers
16 views

i can't add the fourth drive in an md array rebuild ans can't find how/why i'm screwed. i would prefer to use the four members than rebuilding 3tb one

sooo, every attempt to mdadm --manage /dev/md0 -f --re-add /dev/loop12p3 ends with mdadm: hot add failed for /dev/loop12p3: Read-only file system but all the other loop members are read-only too and ...
user avatar
4 votes
4 answers
563 views

Read file and store as array without skipping empty strings

File.tsv is a tab delimited file with 7 columns: cat File.tsv 1 A J 1 2 B K N 1 3 C L O P Q 1 The following reads File.tsv which is tab delimited file ...
user avatar
  • 1,362

1
2 3 4 5
12