A array is the simplest data-structure for storing items in continuously memory
0
votes
2answers
40 views
Bash Array Contains false positives
This is pretty simplistic sounding, but I'm banging my head off the wall trying to figure out what's going on. I'm trying to see if a value exists in a bash array. If so, do something. Else, do ...
3
votes
1answer
16 views
Slicing an array that contains empty strings
Consider the array foo, initialized like this:
$ foo=( a b '' d e f g )
foo contains 7 elements, one of which is an empty string.
Below are a few ways to print out the contents of foo, using the ...
-1
votes
0answers
20 views
how can i make case use $count in both case $count in ; “$count”) … ;; esac done
i have tried doing the following but all i get is the "printf ... " (it is 64 instead of 17 possibly due to there being two "case $stateB in" in the script)
linegen() {
array=()
index=-1
ignore=1
...
3
votes
3answers
119 views
Can it be explained; the difference in array behavior between the use of array=$( command ) and array=( $( command ) )?
I understand command substitution. I understand subshells. I do not understand why using a subshell changes the structure of my array.
Given this command output:
(use of openstack command is not ...
2
votes
1answer
33 views
Why can't I delete these array items in jq?
Given the command:
echo "[\"tag1\", \"[[Super\", \"Duppa\", \"Database\", \"Analyst]]\", \"tag2\"]" | jq -f ~/somefilter.jq
And the filter:
def hasOne(x): x | (startswith("[[") or endswith("]]") ); ...
3
votes
2answers
32 views
Find number of elements in array that is referenced by a dynamically created name
I found a similar question here, but it does not seem to be identical, and none of the answers provide the output that I want. I have an array, for which I want to find how many elements it contains ...
1
vote
1answer
44 views
How to work with multiple arrays in pure sh?
There is this machine where only sh is available, and cut doesn't have --output-delimiter option but I need to work with arrays, I tried this way, it works on my work machine, but the lack of --output-...
4
votes
4answers
321 views
BASH associative array printing
Is there a way to print an entire array ([key]=value) without looping over all elements?
Assume I have created an array with some elements:
declare -A array
array=([a1]=1 [a2]=2 ... [b1]=bbb ... ...
3
votes
3answers
85 views
Bash while loop search and replace using sed
I have a problem with my following script (this is the relevant part of it):
#!/bin/bash
OLD=(
"_MAIN1_"
"_MAIN2_"
)
NEW=(
"#111"
"#222"
)
length=${#OLD[*]}
i=0
while (( i < length ))
do
...
0
votes
0answers
39 views
file or directory not found message while iterating over a filenames array
I am making a script for analyzing the syslog files and the script should iterate over old script files,but when i run the script i get a file or directory not found in the line 16 even if i am only ...
0
votes
1answer
38 views
Redirect the output of a command with variables to an array
I am trying to redirect the output of a command that contains couple of user input variables to an array. I first tried this script -
echo "Type the ACL name"
read acl
echo "Type the DATACENTER name"
...
1
vote
1answer
66 views
Save grep result to array
I want to save all filenames that matches the pattern in bash array.
My solution does not work. I think the problem is because of pipe usage, but I don't know how to fix it.
i=0
find . -type f | ...
1
vote
1answer
48 views
How many elements can an array store in unix script?
I need to check the file permissions of directories /usr, /tmp, /var and their contents of sub directories. I have taken the file permissions of the directory and its sub directory files in an array ...
3
votes
2answers
88 views
Portable check for array
Gawk has "isarray":
if (isarray(x))
print "is array"
else
print "is scalar"
However Mawk and "gawk --posix" do not:
fatal: function 'isarray' not defined
This can cause problems:
x
x[1]
fatal:...
1
vote
0answers
56 views
Can I introspect the type of a bash variable?
I'm writing a function that outputs dates. I'd like to allow the user to customize the output by supplying arguments to date with an environment variable. To preserve white space in format strings, I'...
0
votes
0answers
40 views
Array called via for file in via read [duplicate]
I'm trying to achieve the following:
#!/bin/bash
echo "Enter the log name array, e.g. logs_2017-03-{24..28}.tar.bz2"
read -a logname
for file in $logname
do
echo "Doing" $file
done
Output:
./...
0
votes
1answer
28 views
Why does an element of an array constructed with `readarray` gain a fictitious `\n` when it's enclosed in double quotes?
I found a behavior of readarray that I can't get to the bottom of myself.
The following code:
readarray array < <(echo -e "Jenny\nJane\nJessica")
echo "* Not enclosed:"
for ((i=0; i<${#...
3
votes
4answers
59 views
summing two matrixes each one in a different file
i have 2 files each contains like the following:
file 1:
data : [
56, 34, 23, 54,
90, 234, 53, 12
]
file 2:
data : [
42, 56, 23, 98,
90, 23, 53, 32
]
i want to sum 1st value from ...
3
votes
1answer
41 views
bash add value to array with embedded variable and single quotes
I'm sure it's completely clear from the subject :) Joking aside, I think part of the problem I'm having finding an answer is writing the search terms.
I have had good luck when issuing commands in a ...
0
votes
2answers
43 views
Bash - Problems creating array from a command output which has quoted text with spaces
For an script I'm making I need to convert the output of a command to an array.
For simplifying I have made an example using echo:
arr=( $(echo '"test example" "test2 example"') )
What I want is ...
0
votes
1answer
68 views
How to split string by end of line delimiter in bash?
Let's say I have string x:
/media/root/persistence/file
/media/root/persistence/anotherfile
/media/root/persistence/(copy) file
I'd like to get array, where each file will be listed. My current code ...
0
votes
3answers
96 views
AWK Compare Column 1 from Two Files Print append column to third in output
Looking to compare the first column of two input files that have an identical format. The format looks like the following:
FILE1:
0000abc5abc3 GR096
0000def5ae87 GR001
0000cab5aea3 GR001
...
0
votes
1answer
46 views
Going through 2D array
I have an expression
ls -l `find . -type f` | tr -s [:space:] | cut -d ' ' -f 5 | sort | uniq -c | tr -s [:space:] | sort -k 2n
which creates a 2D array like:
xx yy
xx yy
xx yy
where xx is ...
5
votes
2answers
242 views
How can I get the positional parameters, starting from two, or more generally, `n`?
($@) Expands to the positional parameters, starting from one.
How can I get the positional parameters, starting from two, or more generally, n?
I want to use the positional parameters starting from ...
5
votes
1answer
64 views
Assigning a new value directly into a character index of a value in an array with zsh
If I have an array of strings and I want to change a single character in one value I could do this:
$ array=(hello world)
$ array[2]=${array[2]:0:2}X${array[2]:3}
$ echo $array[2]
woXld
Even though ...
1
vote
3answers
112 views
How to expand array content from its name? [duplicate]
I have an array
declare -a arr0=("'1 2 3'" "'4 5 6'")
and a variable
x=0
Then I create the new variable with the array's name
tmp="arr$x"
and I'd like to be able to expand arr0 content from ...
0
votes
2answers
46 views
Shell scripting help text file into array
#!/bin/bash
function guest {
i=0
while IFS = $'\n' read -r name; do
GUESTS[i]="${name}"
((i++))
done<filename.txt
}
function print{
i=0
while ((${GUESTS[@]} > $i)) do
echo "${GUESTS[i++]}\n"
...
-3
votes
1answer
83 views
Read data from unknown file name into an array? [closed]
So I'm studying for an exam coming up and this was a question asked that I tried doing but failed to do so
file name: Celebs
inside the file:
Beyonce
Brittney
Kevin
George
And I have to create ...
1
vote
1answer
66 views
How to give multidimensional array a value for each 'cells'
I'm not a programmer at all, but I need to write a script to cut a file and produce a multidimensional array from this file.
I'm a chemist, so my array should be something like:
[pe][pH][element][...
1
vote
2answers
200 views
Bash - Passing an array to remote hosts via ssh
I wrote a Bash script that logs in to multiple remote hosts and runs smartctl (from the smartmontools package) on disks defined in the array DISKS. I'm able to pass the array to the remote hosts, but ...
0
votes
0answers
17 views
Find results to variable array being broken by whitespace [duplicate]
To the seasoned nix admin, please don't laugh. :). Also, let me start by saying this script will run against OS X machines. I understand that it changes context a little but for this, I think we are ...
2
votes
4answers
124 views
array as value for tar --exclude
I want to write a little backup script, but I need to exclude some directories. So I decided to set all my excluded directories in a array like this.
exclude[0] = '/home/user/test1'
exclude[1] = '/...
0
votes
1answer
37 views
How to store output of awk to array with empty string on null values? [duplicate]
I have a variable animals.
animals=lion*tiger*elephant**cat***dog
I just want to split by the delimiter * and store it into an array.
Expected:
animals[0]="lion"
animals[1]="tiger"
animals[2]="...
5
votes
3answers
3k views
Print all words on lines of a file in reverse order
I don't know how to make my code working for more lines.
This is the original file t.txt:
Hello Earth
Hello Mars
But I get the following output:
Mars Hello Earth Hello
My expected output is this:
...
1
vote
4answers
635 views
Bash - assign array into variable as string
I have this code, it prints the correct result, but I can't figure out how to get the echo from the last line into a variable.
# hostname is 'tech-news-blog-324344' . Setting it into array ...
2
votes
2answers
71 views
bash - Separate “table” values into strings in array
EDIT: I'm sorry, the output I claimed is wrong. There are more spaces than I previously thought (something happened when the output was saved to html file to remove these) The real output is as ...
2
votes
2answers
42 views
Weird output when using ssh inside a loop over a file
The script is to read a file that contains multiple lines, each line containing a tab-delimited array. I want to execute some remote commands that take those array elements as arguments, with sudo ...
1
vote
3answers
82 views
Obtain the index of the array
I'm trying to obtain the index of the array I'm iterating within a for loop. I am doing something like
arr=( foo bar baz )
for i in $arr; do
echo "index ${#arr}";
done
But that only gives me the ...
1
vote
2answers
175 views
move subshell output into an array in bash 3
How do I import the results of a subshell command into an array in bash 3?
I have been using mapfile to accomplish this in bash 4 as follows:
mapfile -t myarray < <(someScript.sh)
However, ...
0
votes
3answers
153 views
how to get bash indexes of parameters array
i want indexes of parameters,
and can get it by dummy var:
dummy=( $@ )
echo ${!dummy[@]}
but is there straight way to get them, something like
$!@ ... not working
$!* ... not working
... or ...
0
votes
2answers
216 views
Print files in reverse order from an associative array in bash
for key in ${!current_file[@]}
do
echo $key
done
I declare current_file like below in bash:
declare -A current_file
insert key as a file and size as value in current_file. ...
4
votes
1answer
62 views
Sorting an array based on substring of each element
How does one sort an array based on a substring of each element ?
e.g. given an array like
arr=( 2some05stuff 4more02stuff 3evenmore01stuff 1no04stuff )
I'd like to sort the elements by the ...
3
votes
1answer
146 views
Add text to each value while looping thru and printing them in a array?
I'm trying to add a text to every value in array while I loop thru them.
I tried this:
for value in "${array[@]}"
do
echo "--" "$value"
done
It will only add "--" one time. And that's in the ...
0
votes
0answers
476 views
how to add new value to beginning of array in bash?
I have an array containing some element ,but i want to push new items to beginning of array .
How to achieve it ?
4
votes
2answers
334 views
Filter items from an array based on input with wildcard
I've an array coming from the output of a command:
array=(saf sri trip tata strokes)
Now I want to filter items based on user input. The user can also use wildcards, so if the user enters *tr*, the ...
2
votes
1answer
34 views
How to read variables from stdin dynamically and store them into an array
The problem is to read variables with read command dynamically from a read command in bash without knowing how many they are in advance and store them into an array .
I tested with :
read -p "array ...
0
votes
1answer
78 views
How do I store bash env vars in array then access/modify?
I want to store environment variables in a bash array, and then use them in cases where I perform the same operation on all of them (unset, export an so on). For example, if I want to print all env ...
1
vote
2answers
102 views
combining 3 separate arrays to one multidimensional array in bash
Is possible to create multidimensional arrays in bash scripting?
These are my 3 arrays:
arrayCITY=( NewYork LasVegas Detroit )
arraySTREET=( RoadStreet TreeStreet HighStreet )
arrayNUMBER=( 20 455 ...
1
vote
1answer
68 views
Problem for filtering sequences array to remove duplicates with shifted sequences
I had some trouble to do an array of lists comparison to remove duplicates.
My array is composed of sequences of ternary values like this:
{0, 0, 1, 0, 1, 1, 1, 2, 2, 0, 1, 2, 1}
{0, 0, 2, 0, 2, ...
1
vote
0answers
28 views
Iterate over array which name is in another variable in bash [duplicate]
I want to iterate over an array which name is in another variable. This is what I tried so far:
NTASKSLIST=(384 768 1536 3072)
# there are potentially more arrays like these, each custom defined for ...