Questions tagged [array]

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

Filter by
Sorted by
Tagged with
0
votes
0answers
14 views

Attempting to write PHP array to file in Linux PHP interactive shell produces Uncaught Error and empty file

After executing the following commands: [root@server test]# php -v PHP 7.2.25 (cli) (built: Dec 17 2019 22:57:09) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-...
1
vote
2answers
38 views

bash array not be properly processed when mapfile is used to read in values

this one has me baffled. Here are 2 versions of loading a bash array, one works, the other does not. If I explicitly initialize the array within the script, it works fine: zz=( 1 26 32 54 71 13 30 ...
2
votes
1answer
116 views

How to extract data more Efficient in an array?

I have a directory with some text files that have data like below; I read them via a loop and put each line in an array! I don't know the correct way and if you know any better way, let me know. ...
0
votes
1answer
62 views

Read csv from shell script and print the column header based on closest match to user input

I Have a csv file. It has multiple columns. I want to develop a shell script which prompts the use for a numerical value, find the closest match in the csv and print the header of the column. For ...
0
votes
3answers
89 views

Mathematical operations - Bash script

I am trying to write a script(script1.sh) that gives the sum of each digits in the first number, raised to the power of second number. So ./script1.sh 12345 2 should output 55 (because 1+4+9+16+...
0
votes
1answer
35 views

Work around spaced-filenames in array construction

I'm using the command files=( $(ls "/path/wanted") ) to store all filenames from a directory in an array. However filenames that contain space character result in multiple array entries. To figure ...
0
votes
4answers
148 views

Convert JSON Array to Bash

I'm using JQ to fetch some JSON from a quiz database and I want to parse the results. I'm trying to save a resulting array in Bash as shown below but the format is that used in JavaScript/Python with ...
1
vote
2answers
71 views

Sum occurrences of string for each row of a matrix using awk

I have a very long data frame (~11 million x 130) for which is sparsely filled with percentages (0.00 - 100.00, float). The entries that are missing are labeled "NA", like so: cat1 NA NA ...
0
votes
2answers
40 views

Error while using array in AIX : syntax error error at line 81 : '(' unexpected

I am trying to execute one .sh script in AIX environment, but it's giving an error at below line tableList = ( Value1 Value2 Value3 ) I have tried executing the script like below: sh file1.ksh I ...
0
votes
1answer
27 views

More efficient zsh string parsing / array handling

Is there any more efficient way to do the following in zsh? I imagine that there might be ways to get rid of the intermediate array parameters a and/or b. The script gets some output from a command. ...
0
votes
1answer
24 views

In zsh, what is the difference between (${(@s.\n.)s}) & (${(@s/\n/)s})

In zsh, what is the difference between (${(@s.\n.)s}) (dots) & (${(@s/\n/)s}) (slashes), e.g., in: s=a\\nb\\nc\\nd a=(${(@s.\n.)s}) b=(${(@s/\n/)s})
2
votes
2answers
109 views

Iterate through array of strings applying a Regex to each element in Korn Shell

I have an array of strings called names containing names with some subsequent garbage data. Like this Jill Shortz, City Contractor, America Bill Torts, Family Doctor, Canada Will Courtz, Folk DJ, ...
0
votes
1answer
53 views

mdadm RAID1 array - is it required to partition?

I've been using an mdadm RAID1 array with 2 2TB disks for a few years without issues. Recently I needed to upgrade it to 4TB so I bought 2 new disks of 4TB and plan to replace one, wait for the ...
0
votes
1answer
50 views

Why do these rsync filter args fail in bash when passed in array?

Why does this rsync command work when I give it literally but not when I create it from variables? Here are the variables - first the options I'm passing to rysnc, as an array: $ echo "${options[@]}"...
8
votes
3answers
2k views

Bash to check if directory exist. If not create with an array

I want to write a Bash script, which checks if all directories, stored in an array, exist. If not, the script should create it. Is this a correct way to do it? array1=( /apache /apache/bin /apache/...
1
vote
1answer
46 views

Bash Syntax for Using GREP to Extract Text from File and Store in an Array

I have recently upgraded from bash 4.2.46 to bash version 4.2.50 I had a script with the line: mapfile -t my_array < <(grep ${ID} -w /etc/passwd | cut -d ":" -f 1,4,5) where ID is an ...
1
vote
2answers
64 views

I am having difficulties with back-reference in awk

Recently, I am into security logs and want to make it better way on bash-shell. I found out in awk back-references are only stored by 9. But I need to use 10 back-references. Tried awk '{print ...
-1
votes
1answer
42 views

UNIX AIX using Array to store name and path then get them using for loop separately

I have a script in which i need to create an array with the Path and Name of some files, then i need to pass the Path and Name separately to another script after checking the user's arg matching one ...
0
votes
1answer
53 views

Awk function, syntax error near unexpected 'myArr,'

I am not sure why my simple function causes "Syntax error near unexpected 'myArr,'. I taking an array, lookup key value and a line item section name as arguments. Each row in the array is "," ...
2
votes
2answers
397 views

bash array of arrays

Trying to write some nested loop, and I'm not getting how to write it. Perhaps I'm looking in a wrong direction but what I'm trying to write is: declare -a bar=("alpha" "bravo" "charlie") declare -a ...
1
vote
1answer
28 views

Array only returns one element

i'm trying to generate a script that ftp some files to a server using lftp. when i run these commands in shell: DBNAME=TESTDB ls -t /data*/${DBNAME,,}Backup/$DBNAME.0.db21.DBPART000.`date +%Y%m%d`...
0
votes
2answers
157 views

Bash - sort and uniq on array

This is a similar question to this one I want to do the word count but this time, using an array. For example, I have the following IPs inside a bash array called IPS. IPS=("1.1.1.1" "5.5.5.5" "3.3....
0
votes
0answers
54 views

Bash array length - my code always gives 1 or 0 but should have infinite varaibility

Yes similar questions have been asked, but none of those answers have shown me what I am missing here. I think my problem is in not fully understanding what I am generating with my code... ...
1
vote
0answers
33 views

Performing job array by reading in file paths from a text file

I am trying to perform a job array using script below. #!/bin/bash --login #$ -cwd #$ -t 1-4 module load apps/bioinf module load apps/trimmomatic/0.36/noarch file=`awk "NR==$SGE_TASK_ID" ~/scratch/...
-1
votes
1answer
98 views

I am trying to get a value the i just want the last date and time only [closed]

Trying to get a value date and time not the whole log ${1} - Command Line Arguments in Bash ${i} - Array Item in for Loop grep -ERh "stopped" ems_*/SystemOut*.log | tail -1 Now the value is like ...
6
votes
1answer
887 views

BASH script: How to assign each line of command output to values in an array?

When running a command, I get 10 lines of output. I want to take lines 2-4-6-8-10 and put them into an array. Every time I run my command, the order changes so I need to do this in one go. I had ...
1
vote
0answers
24 views

Set i'th value of kernel module array with command line arguments

Given a kernel module arraymod.c with the following array declaration: static int myintArray[2] = { -1, -1 }; static int arr_argc = 0; module_param_array(myintArray, int, &arr_argc, 0000); ...
0
votes
1answer
82 views

Bash get array length with offset

I'm doing a bash script and I need to get the length of an array starting from an element. Let's say that the array is: array=(1 2 3 4 5) It is possible to print the array with an offset of 2 using:...
1
vote
1answer
239 views

how to combine 2 arrays into one associative array

i need to combine ARRAY1 and ARRAY2 into an associative array like ARRAY. i'm using this code: mapfile -t ARRAY1 < <(/bin/awk '{ print $ 1 }' /output/gen_branch) mapfile -t ARRAY2 < <(...
2
votes
2answers
54 views

bash: Match and replace user input with another array

First of all, sorry for the heading. I'm not sure how to place my ambition in a few words. However, from a coding/scripting standpoint it should be pretty basic. Though, I somehow fail to achieve it :/...
0
votes
3answers
173 views

Find command: name in list

I have a file with a list of names: john paul ringo george ... Then I have a bunch of files with a lot of different names: maria.iso jose.iso pablo.iso john.iso jack.iso jane.iso ... I want to ...
2
votes
3answers
124 views

How to convert file of strings separated by a new line to array

I have a file with data like this: UserName UserName UserName Each piece of data is separated by a new line. I need that data to be converted to an array of strings and stored in a variable. How ...
0
votes
1answer
84 views

tcsh: search entire array for a variable

I am modifying a suite of scripts written in tcsh. Unfortunately, these scripts can't be rewritten because the program they interface with is also written in tcsh. I just want to get out of the way ...
4
votes
4answers
392 views

Just one file echoed from an array of files

I try to copy 2 or more files from one directory to another with cp using an array. I executed: files=( LocalSettings.php robots.txt .htaccess ${domain}.png googlec69e044fede13fdc....
1
vote
2answers
197 views

What's the idiomatic way of returning an array in a zsh function?

I have this function, rpargs () { local i args=() for i in "$@" do test -e "$i" && args+="$(realpath --canonicalize-existing -- "$i")" || args+="$i" done } And I ...
1
vote
2answers
73 views

How can I set default value in parameter substitution as an array of elements?

I'm using zsh, and I have this function: function timer-raw() { # Just think `loop fsayd` is `echo hi` for the purposes of this question. eval "sleep $((($1)*60))" && eval ${(q+@)@[2,-...
3
votes
1answer
79 views

Struggling with output of grep into an array

Please note I'm not a developer but I have requirements where I'm trying to search an SVN repository for files containing certain table names. I have built this script on the basis of many things I ...
12
votes
1answer
782 views

What is the difference between $path and $PATH (lowercase versus uppercase) with zsh?

In FreeBSD 12, using the zsh shell, I noticed this difference when looking at $path (lowercase) versus $PATH (uppercase). echo $path /sbin /bin /usr/sbin /usr/bin /usr/local/sbin /usr/local/bin /...
1
vote
1answer
56 views

Assoc. Array not being redeclared?

I have a program that acts like a menu. It has an associative array called config such as: declare -A config=( [h]="?" [c]="?" [x]="?" [l]="?" [t]="?" [n]="?" ) In the main loop there's a check to ...
0
votes
1answer
182 views

no matches found when using associative arrays in zsh

I am encountering no matches found when using map in zsh: #!/bin/zsh declare -A map=(["8761"]="Eureka服务" ["11001"]="用户微服务") Why would this happen, and how can I fix it? This is the error: ~/source/...
2
votes
2answers
293 views

Convert array type variable to string type with space delimiter

If i have an array like arr[0]=2019-06-26 arr[1]=15:21:54 How can i convert that into a string whose value is '2019-06-26 15:21:54'
2
votes
3answers
737 views

Generic function to loop over inputs and execute a command in bash?

I was trying to create a function that loops over inputs and executes a command - regardless of how they are delimited. function loop { # Args # 1: Command # 2: Inputs for ...
0
votes
0answers
45 views

Comparing Timestamps

My users sometimes get alerts with a timestamp, (from another system, via email) but no hostname. I want to give them a function that can compare timestamps in my system that's designed like this: /...
0
votes
2answers
1k views

bash syntax error: invalid arithmetic operator (error token is “.google.com”) when parsing through array

I have an array of domains that I am attempting to iterate through and check if a string from a file matches any of the domains in the array. However, when the for loop starts, I get an error line ...
1
vote
2answers
202 views

Merge duplicate keys in associative array BASH

I've got an array that contains duplicate items, e.g. THE_LIST=( "'item1' 'data1 data2'" "'item1' 'data2 data3'" "'item2' 'data4'" ) Based on the above, I want to create an associative array that ...
0
votes
1answer
28 views

calling associative arrays [closed]

Team, am setting some variables in an associative array but its output is not resulting anything.. any hint?> #/bin/bash #IOEngine="psync" #TestType="read" IOEngine="libaio" TestType="randread" ...
1
vote
2answers
557 views

How to split string by string (multi-character) separator into an array?

Basically I'd like to split a string into an array by a string separator like sep. for example. Is there native bash way to do this without using tools like sed, awk, etc? I know I can split a string ...
-1
votes
1answer
272 views

How to Force String Type on a Variable?

I have this script: #!/bin/bash USERS=("user1.com" "user2.co.uk" "user3.com") PASSES=("pass1" "pass2" "pass3") for i in ${USERS[@]}; do echo ${PASSES[i]}; done running this script outputs: ...
0
votes
0answers
12 views

Why is my array declaration incorrect? [duplicate]

I'm trying to do some shizzle with SFTP and SSH but it would seem I'm getting hung up on super-simple array declaration. According to this, I should be able to use the following syntaxes to create an ...
7
votes
1answer
556 views

Does `declare -a A` create an empty array `A` in Bash?

Does declare -a A create an empty array A in bash, or does it just set an attribute in case A is assigned to later? Consider this code: set -u declare -a A echo ${#A[*]} echo ${A[*]} A=() echo ${#A[*...