Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
2
votes
1
answers
65
views
Why can't I have a single quote in a bash associative array index when testing with `test -v`
When I run the following code ```bash declare -A X # get an associative array index="a'b" # a somewhat weird index X[$index]="this is set" echo " >" if test -v X[$index]; then # behaves in an unexpected way echo indeed, this is set else echo no, it is not fi ``` the result is `no, it is not` in my b...
When I run the following code
declare -A X # get an associative array
index="a'b" # a somewhat weird index
X[$index]="this is set"
echo ">"
if test -v X[$index]; then # behaves in an unexpected way
echo indeed, this is set
else
echo no, it is not
fi
the result is no, it is not
in my bash version 5.1.16(1). As soon as i remove the single quote from the index
, it works as expected, telling that indeed, this is set
.
From the bash manual :
> Indexed arrays are referenced using integers [...]; associative arrays use arbitrary strings.
Given that I can properly use the index
to set and retrieve the value, I wonder if this is a bug in test -v
or if there is some explanation why this does not work or whether different quoting can get test -v
to work. (I could use test -z
in this particular case, but the question is about -v
).
Harald
(1030 rep)
Apr 24, 2025, 12:43 PM
• Last activity: Apr 24, 2025, 05:29 PM
1
votes
4
answers
3506
views
bash loop associative array with variable array name
I have a lot of associative arrays and i want to use only 1 loop. select the array for loop by a given name I want to select/build a part of the arrayname with a variable and than loop with that name, but it doesn't work. Like in OUTPUT3 and OUTPUT4, but the syntax is wrong. For output 3 i receive:...
I have a lot of associative arrays and i want to use only 1 loop. select the array for loop by a given name
I want to select/build a part of the arrayname with a variable and than loop with that name, but it doesn't work.
Like in OUTPUT3 and OUTPUT4, but the syntax is wrong.
For output 3 i receive:
bash wrong substitution
For output 4 i receive: only the arrayname and 0
#!/bin/bash
clear
declare -A a1 a2 a3
a1['1']="1-1V"
a2['1']="2-1V"
a2['2']="2-2V"
a3['1']="3-1V"
a3['2']="3-2V"
a3['3']="3-3V"
# 1 OUTPUT WORKS
for i in ${!a1[*]}
do
echo -e "$i : ${a1[$i]}"
done
# 2 OUTPUT WORKS
for i in ${!a2[*]}
do
echo -e "$i : ${a2[$i]}"
done
# 3 OUTPUT - WRONG SYNTAX
selectkey="3"
for i in ${!a$selectkey[@]}
do
echo -e "$i : ${a$selectkey[$i]}"
done
# 4 OUTPUT - WRONG SYNTAX
key="3"
aselect="a${key}[*]"
# THIS ECHO WORKS
echo -e "ARRAY: ${!aselect}"
for i in ${!aselect[@]}
do
echo -e "$i : ${aselect[$i]}"
done
ReflectYourCharacter
(8157 rep)
Jan 17, 2023, 11:39 AM
• Last activity: Mar 19, 2025, 04:33 PM
41
votes
6
answers
59084
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 ... [f500]=abcdef) I can print back the entire array with for i in "${!array[@]}" do echo "${i}=${array[$i]}"...
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 ... [f500]=abcdef)
I can print back the entire array with
for i in "${!array[@]}"
do
echo "${i}=${array[$i]}"
done
However, it seems bash already knows how to get all array elements in one "go" - both keys
${!array[@]}
and values ${array[@]}
.
Is there a way to make bash print this info without the loop?
Edit:
typeset -p array
does that!
However I can't remove both prefix and suffix in a single substitution:
a="$(typeset -p array)"
b="${a##*(}"
c="${b%% )*}"
Is there a cleaner way to get/print only the key=value portion of the output?
Dani_l
(5157 rep)
May 22, 2017, 04:13 PM
• Last activity: Jan 5, 2025, 05:02 PM
-2
votes
1
answers
79
views
Associative Array contains filenames and paths
I have a pipeline that deploys scripts into a temporary dir. I need to rellocate them into their actual proper directory. ``` #!/usr/bin/sh DEPLOY_HOME=/opt/xxx function getDeployedFiles { ls ${DEPLOY_HOME} | grep -v pipeline.properties | grep -v Deploy.tmp > ${DEPLOY_HOME}/Deploy.tmp chmod 700 ${DE...
I have a pipeline that deploys scripts into a temporary dir. I need to rellocate them into their actual proper directory.
#!/usr/bin/sh
DEPLOY_HOME=/opt/xxx
function getDeployedFiles {
ls ${DEPLOY_HOME} | grep -v pipeline.properties | grep -v Deploy.tmp >
${DEPLOY_HOME}/Deploy.tmp
chmod 700 ${DEPLOY_HOME}/Deploy.tmp
readarray -t depFilesArr < ${DEPLOY_HOME}/Deploy.tmp
}
function setDict {
declare -A dict
dict=(
["a.script.100"]="/opt/xxx/firstpath/100"
["b.script.200"]="/opt/xxx/secondpath/200"
["c.script.300"]="/opt/xxx/thirdpath/300"
)
}
function itArr {
for i in "${depFilesArr[@]}"; do
FILENAME="${i}"
if [[ -v "${dict['${i}']}" ]]; then
echo "Value is present"
FILEDEST="${dict['${i}']}"
mv ${DEPLOY_HOME}/${FILENAME} ${FILEDEST}/${FILENAME}
else
echo "Value not found, file will remain in ${DEPLOY_HOME}"
fi
done
}
#MAIN PROGRAM
getDeployedFiles
setDict
itArr
The current error I am getting is:
"script.sh: line x 'a.script.100': syntax error: operand expected (error token is "'a.script.100'")
It doesn't seem to evaluate the "if" properly as it's not printing any of the subsequent echos.
Idk wether I'm incorrectly managing the strings or something about my logic is wrong, any help appreciated. BTW I'm using bash 4.4.
KrOo Pine
(107 rep)
Oct 31, 2024, 07:27 PM
• Last activity: Nov 1, 2024, 03:13 PM
2
votes
3
answers
2138
views
How can I create a JSON object from an associative array in shell using jo?
I know about https://unix.stackexchange.com/questions/625501/how-to-create-json-from-associative-array but that's not my problem. I have this associative array: ``` declare -A aliases aliases[Index]=components/Index/Exports aliases[Shared]=components/Shared/Exports aliases[Icons]=components/Icons/Ex...
I know about https://unix.stackexchange.com/questions/625501/how-to-create-json-from-associative-array but that's not my problem.
I have this associative array:
declare -A aliases
aliases[Index]=components/Index/Exports
aliases[Shared]=components/Shared/Exports
aliases[Icons]=components/Icons/Exports
Now I need to convert this associative array into this JSON:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"Index": ["components/Index/Exports"],
"Shared": ["components/Shared/Exports"],
"Icons": ["components/Icons/Exports"],
}
}
}
I want to use jo
and jq
. But I can't come up with the nesting.
I tried this code:
jo -p compilerOptions[baseUrl]=. compilerOptions[paths]="$(jo -p a ${Aliases[@]})"
But it does not even run.
Saeed Neamati
(841 rep)
Jun 30, 2022, 01:38 PM
• Last activity: Jun 22, 2024, 07:48 PM
10
votes
4
answers
2417
views
Inverting an associative array
Let's say I have an associative array in `bash`, declare -A hash hash=( ["foo"]=aa ["bar"]=bb ["baz"]=aa ["quux"]=bb ["wibble"]=cc ["wobble"]=aa ) where both keys and values are unknown to me (the actual data is read from external sources). How may I create an array of the keys corresponding to the...
Let's say I have an associative array in
bash
,
declare -A hash
hash=(
["foo"]=aa
["bar"]=bb
["baz"]=aa
["quux"]=bb
["wibble"]=cc
["wobble"]=aa
)
where both keys and values are unknown to me (the actual data is read from external sources).
How may I create an array of the keys corresponding to the same value, so that I may, in a loop over all unique values, do
printf 'Value "%s" is present with the following keys: %s\n' "$value" "${keys[*]}"
and get the output (not necessarily in this order)
Value "aa" is present with the following keys: foo baz wobble
Value "bb" is present with the following keys: bar quux
Value "cc" is present with the following keys: wibble
The important bit is that the keys are stored as separate elements in the keys
array and that they therefore do not need to be parsed out of a text string.
I could do something like
declare -A seen
seen=()
for value in "${hash[@]}"; do
if [ -n "${seen[$value]}" ]; then
continue
fi
keys=()
for key in "${!hash[@]}"; do
if [ "${hash[$key]}" = "$value" ]; then
keys+=( "$key" )
fi
done
printf 'Value "%s" is present with the following keys: %s\n' \
"$value" "${keys[*]}"
seen[$value]=1
done
But it seems a bit inefficient with that double loop.
Is there a piece of array syntax that I've missed for bash
?
Would doing this in e.g. zsh
give me access to more powerful array manipulation tools?
In Perl, I would do
my %hash = (
'foo' => 'aa',
'bar' => 'bb',
'baz' => 'aa',
'quux' => 'bb',
'wibble' => 'cc',
'wobble' => 'aa'
);
my %keys;
while ( my ( $key, $value ) = each(%hash) ) {
push( @{ $keys{$value} }, $key );
}
foreach my $value ( keys(%keys) ) {
printf( "Value \"%s\" is present with the following keys: %s\n",
$value, join( " ", @{ $keys{$value} } ) );
}
But bash
associative arrays can't hold arrays...
I'd also be interested in any old school solution possibly using some form of indirect indexing (building a set of index array(s) when reading the values that I said I had in hash
above?). It feels like there ought to be a way to do this in linear time.
Kusalananda
(354171 rep)
Mar 17, 2019, 10:48 PM
• Last activity: May 22, 2024, 05:28 PM
13
votes
4
answers
44436
views
Associative Arrays in Shell Scripts
I saw a trick for implementing associative arrays in a shell script. For example `print array["apples"]` could be scripted as `echo \$array$key` where key=apples. However, there was no mention of how to generate the keys to iterate over the array. The only way I could think of was to store the keys...
I saw a trick for implementing associative arrays in a shell script. For example
print array["apples"]
could be scripted as echo \$array$key
where key=apples.
However, there was no mention of how to generate the keys to iterate over the array.
The only way I could think of was to store the keys in a variable delimited by spaces so I could use a for-loop to iterate over the array.
So, is there some other way to store the keys for later use?
EggHead
(275 rep)
Jan 28, 2014, 10:35 PM
• Last activity: Feb 20, 2024, 10:14 PM
0
votes
2
answers
241
views
Iterating arrays in TCL where array names have a numeric suffix
In TCL, I have a few arrays whose names have a numeric suffix (i.e., whose names end with a number), like below: ```tcl array set ps0 [ list 0 15.885 1 55.43 1 0.254 2 0.227 3 0.177 ] array set ps1 [ list 0 6.585 1 56.43 1 0.254 2 0.227 3 0.177 ] array set ps2 [ list 0 32.485 1 4...
In TCL, I have a few arrays whose names have a numeric suffix
(i.e., whose names end with a number), like below:
array set ps0 [ list 0 15.885 1 55.43 1 0.254 2 0.227 3 0.177 ]
array set ps1 [ list 0 6.585 1 56.43 1 0.254 2 0.227 3 0.177 ]
array set ps2 [ list 0 32.485 1 43.13 1 0.254 2 0.227 3 0.177 ]
I need to iterate over these in TCL and get the values,
but, no matter what I do to escape the numeric suffix,
I cannot get both the array and the contents.
Everything I've tried, such as:
ps$i($i)
"ps$i($i)"
or even using a set like:
set p ps$i
...does not work to get the array with the number index.
I get the error:
Original error: can't read "ps": no such variable
...for all possible combinations.
How can I do this (in TCL)?
poppycock
(3 rep)
Feb 16, 2024, 04:06 AM
• Last activity: Feb 16, 2024, 01:58 PM
19
votes
1
answers
2583
views
How to use associative arrays safely inside arithmetic expressions?
A few Bourne-like shells support associative arrays: `ksh93` (since 1993), `zsh` (since 1998), `bash` (since 2009), though with some differences in behaviour between the 3. A common use is for counting occurrences of some strings. However, I find that things like: typeset -A count (( count[$var]++ )...
A few Bourne-like shells support associative arrays:
ksh93
(since 1993), zsh
(since 1998), bash
(since 2009), though with some differences in behaviour between the 3.
A common use is for counting occurrences of some strings.
However, I find that things like:
typeset -A count (( count[$var]++ ))Don't work for some values of
$var
and I hear it even constitutes an __*arbitrary command execution* vulnerability__ if the contents of $var
is or may be under the control of an attacker.
Why is that? What are the problematic values? And how do I work around it?
Stéphane Chazelas
(579252 rep)
Jan 4, 2021, 03:12 PM
• Last activity: Dec 24, 2023, 08:50 PM
5
votes
4
answers
10096
views
Making associative array based on another associative array
I made an associative array as follows. To give a few details, the keys refer to specific files because I will be using this array in the context of a larger script (where the directory containing the files will be a getopts argument). declare -A BAMREADS echo "BAMREADS array is initialized" BAMREAD...
I made an associative array as follows. To give a few details, the keys refer to specific files because I will be using this array in the context of a larger script (where the directory containing the files will be a getopts argument).
declare -A BAMREADS
echo "BAMREADS array is initialized"
BAMREADS[../data/file1.bam]=33285268
BAMREADS[../data/file2.bam]=28777698
BAMREADS[../data/file3.bam]=22388955
echo ${BAMREADS[@]} # Output: 22388955 33285268 28777698
echo ${!BAMREADS[@]} # Output: ../data/file1.bam ../data/file2.bam ../data/file3.bam
So far, this array seems to behave as I expect. Now, I want to build another associative array based on this array. To be specific: my second array will have the same keys as my first one but I want to divide the values by a variable called $MIN.
I am not sure which of the following strategies is best and I can't seem to make either work.
**Strategy 1**: copy the array and modify the array?
MIN=33285268
declare -A BRAMFRACS
echo "BAMFRACS array is initialized"
BAMFRACS=("${BAMREADS[@]}")
echo ${BAMFRACS[@]} # Output: 22388955 33285268 28777698
echo ${!BAMFRACS[@]} # Output: 0 1 2
This is not what I want for the keys. Even if it works, I would then need to perform the operation I mentioned on all the values.
**Stragegy 2**: build the second array when looping through the first.
MIN=33285268
declare -A BRAMFRACS
echo "BAMFRACS array is initialized"
for i in $(ls $BAMFILES/*bam)
do
echo $i
echo ${BAMREADS[$i]}
BAMFRACS[$i] = ${BAMREADS[$i]}
done
echo ${BAMFRACS[@]}
echo ${!BAMFRACS[@]}
#When I run this, I get the following error which I am unsure how to solve:
../data/file1.bam
33285268
script.bash: line 108: BAMFRACS[../data/file1.bam]: No such file or directory
../data/file2.bam
28777698
script.bash: line 108: BAMFRACS[../data/file2.bam]: No such file or directory
../data/file3.bam
22388955
script.bash: line 108: BAMFRACS[../data/file3.bam]: No such file or directory
Thanks
mf94
(219 rep)
Aug 24, 2018, 11:54 AM
• Last activity: Dec 5, 2023, 09:44 PM
6
votes
3
answers
11666
views
linux bash dictionary check if empty
How to check if a dictionary (associative array) is empty? I just declare one using `declare -A dict`. I want to know if it is just declared but not have any key.
How to check if a dictionary (associative array) is empty? I just declare one using
declare -A dict
. I want to know if it is just declared but not have any key.
focus zheng
(186 rep)
Dec 7, 2018, 09:56 AM
• Last activity: Aug 25, 2023, 07:50 PM
2
votes
2
answers
1361
views
Bash pass an associative array to/from a background function
I am trying to pass a bash associative array by reference into a function and then be able to see the changed content back in the main script after the function is complete. I have found what seems to be the most straightforward way to do it [here](https://unix.stackexchange.com/a/462089/21348) exce...
I am trying to pass a bash associative array by reference into a function and then be able to see the changed content back in the main script after the function is complete. I have found what seems to be the most straightforward way to do it [here](https://unix.stackexchange.com/a/462089/21348) except in my case the function is being run ***in the background***. It seems that no matter what I do, I can't get the linked solution above to work in this scenario.
In the snippet below, I've taken the working example code from the link above and simply added a "&" to the function call and a "wait" on the following line to demonstrate the issue as simply as possible.
I suspect bash is trying to prevent the main script and the background function from stepping on each other, but I don't know how to solve it.
**Example Code:**
foo () {
declare -n fooarray="$1"
fooarray["fookey"]=foovalue
}
declare -A myarray
myarray["mainkey"]=mainvalue
foo myarray &
wait
for key in "${!myarray[@]}"; do
printf '%s = %s\n' "$key" "${myarray[$key]}"
done
**Output:**
bash-4.4$ ./test.sh
mainkey = mainvalue
Any help would be appreciated. I know I could potentially do silly things like writing the contents of the array out to a file and then parsing it back in, but I'm hoping there's a more elegant solution than that.
demarcmj
(231 rep)
Feb 24, 2022, 09:16 PM
• Last activity: Jul 30, 2023, 04:32 PM
2
votes
1
answers
572
views
What is difference between these two declarations of associative arrays in Bash?
I am playing a bit with associative arrays in Bash and I found the following difference when declaring the exact same associative array with and without `declare`. The code is as follows: ``` #!/usr/bin/env bash echo -e "\n\nASSOCIATIVE ARRAY\n" declare -A MY_MAP=( [Madrid]="Spanish" [London]="Engli...
I am playing a bit with associative arrays in Bash and I found the following difference when declaring the exact same associative array with and without
declare
. The code is as follows:
#!/usr/bin/env bash
echo -e "\n\nASSOCIATIVE ARRAY\n"
declare -A MY_MAP=(
[Madrid]="Spanish"
[London]="English"
[Paris]="French"
=2
=3.14
)
echo "First element: $MY_MAP"
echo "Whole content as single string: ${MY_MAP[*]}"
echo "Whole content quoted separately: ${MY_MAP[@]}"
echo "List of indices: ${!MY_MAP[@]}"
echo "Array length: ${#MY_MAP[*]}"
echo "Array length: ${#MY_MAP[@]}"
echo "Second element: ${MY_MAP[London]}"
echo "Last 2 elements: ${MY_MAP[@]:1:3}"
echo -e "\n\nASSOCIATIVE ARRAY - 2\n"
MY_MAP2=(
[Madrid]="Spanish"
[London]="English"
[Paris]="French"
=2
=3.14
)
echo "First element: $MY_MAP2"
echo "Whole content as single string: ${MY_MAP2[*]}"
echo "Whole content quoted separately: ${MY_MAP2[@]}"
echo "List of indices: ${!MY_MAP2[@]}"
echo "Array length: ${#MY_MAP2[*]}"
echo "Array length: ${#MY_MAP2[@]}"
echo "Second element: ${MY_MAP2[London]}"
echo "Last 2 elements: ${MY_MAP2[@]:1:3}"
When I execute the previous script I do get the following output:
ASSOCIATIVE ARRAY
First element:
Whole content as single string: French 3.14 2 English Spanish
Whole content quoted separately: French 3.14 2 English Spanish
List of indices: Paris 3 1 London Madrid
Array length: 5
Array length: 5
Second element: English
Last 2 elements: French 3.14 2
ASSOCIATIVE ARRAY - 2
First element: French
Whole content as single string: French 2 3.14
Whole content quoted separately: French 2 3.14
List of indices: 0 1 3
Array length: 3
Array length: 3
Second element: French
Last 2 elements: 2 3.14
My question is what is declare -A
doing that makes the output different?
A detailed insight of what is happening here will be appreciated.
Thanks a lot in advance.
chemacabeza
(89 rep)
May 20, 2023, 08:27 PM
• Last activity: May 20, 2023, 09:30 PM
1
votes
1
answers
58
views
What <COMMAND> will make `declare -A ASSOCIATIVEARRAY=( $( <COMMAND> ) )` work?
`bash` evaluates the following expression without any objections: declare -A SPANISH=( [rojo]=red [verde]=green [azul]=blue ) ...but it does not like this one one bit: declare -A SPANISH=( $( echo "[rojo]=red [verde]=green [azul]=blue" ) ) bash: SPANISH: $( echo "[rojo]=red [verde]=green [azul]=blue...
bash
evaluates the following expression without any objections:
declare -A SPANISH=( [rojo]=red [verde]=green [azul]=blue )
...but it does not like this one one bit:
declare -A SPANISH=( $( echo "[rojo]=red [verde]=green [azul]=blue" ) )
bash: SPANISH: $( echo "[rojo]=red [verde]=green [azul]=blue" ): must use subscript when assigning associative array
I have tried many variations, but the result is always one or more instances of the must use subscript when assigning associative array
error shown above.
This example is silly, of course. Its only purpose is to illustrate the problem of *initializing a **bash** associative array using a command substitution*.
Can this be done?
To be concrete, and to continue with the silly example above, what would be a command `` such that
declare -A SPANISH=( $( ) )
...produces the same final result as that produced by
declare -A SPANISH=( [rojo]=red [verde]=green [azul]=blue )
above?
Incidentally, zsh
handles this situation without any problem:
$ declare -A SPANISH=( $( echo "rojo red verde green azul blue" ) )
$ echo "${SPANISH[azul]}"
blue
kjo
(16299 rep)
May 11, 2023, 05:39 PM
• Last activity: May 11, 2023, 06:18 PM
3
votes
1
answers
321
views
duplicated entries of an array in awk
I have a file with 4 columns. When I put these 4 columns into an array using `NR` as the index, the entries are duplicated somehow. See below for an elaboration of the issue. The first 5 lines of the file look like this -bash-4.2$ cat -ve file | head -n 5 chr start end p$ 13 59341171 59343427 1.8664...
I have a file with 4 columns. When I put these 4 columns into an array using
NR
as the index, the entries are duplicated somehow. See below for an elaboration of the issue.
The first 5 lines of the file look like this
-bash-4.2$ cat -ve file | head -n 5
chr start end p$
13 59341171 59343427 1.86642E-18$
10 72886545 72888679 1.13636E-09$
16 81900987 81902805 6.79697E-09$
1 46797890 46800143 2.24436E-08$
I assigned each line as an entry of an array indexed by the NR
, then the print out of the array looks like this (using the first 5 lines as an example):
-bash-4.2$ awk 'NR for(x in a)
> print x, a[x]}' file
1 chr start end p
1 chr start end p
2 13 59341171 59343427 1.86642E-18
1 chr start end p
2 13 59341171 59343427 1.86642E-18
3 10 72886545 72888679 1.13636E-09
4 16 81900987 81902805 6.79697E-09
1 chr start end p
2 13 59341171 59343427 1.86642E-18
3 10 72886545 72888679 1.13636E-09
4 16 81900987 81902805 6.79697E-09
5 1 46797890 46800143 2.24436E-08
1 chr start end p
2 13 59341171 59343427 1.86642E-18
3 10 72886545 72888679 1.13636E-09
I can see that the 5 lines of the file are there, but entries are duplicated a few times. I wonder what the problem is and how to fix it. Thanks in advance.
Xuan
(45 rep)
Apr 3, 2023, 12:01 PM
• Last activity: Apr 3, 2023, 12:07 PM
1
votes
1
answers
151
views
index of an array is not recognized in awk
I have two tab separated files, each with two columns. I want to create a file which contains overlapping elements by column 1 of the two files. To do so, I put file 1 in an array first then scanned the array to check against file 2 for overlaps. However, somehow the index of the array cannot be rec...
I have two tab separated files, each with two columns. I want to create a file which contains overlapping elements by column 1 of the two files. To do so, I put file 1 in an array first then scanned the array to check against file 2 for overlaps. However, somehow the index of the array cannot be recognized. See below for the elaboration of the problem.
The first 3 lines of the files look like this:
File 1:
90001 raw acceleration data
2634 Heavy DIY
1011 Light DIY
File 2:
2634 218263
25680 44313
25681 44313
To show that there are overlaps in column 1 of the two files:
user@cluster:~> grep 90001 file2
90001 103662
user@cluster:~> grep 2634 file2
2634 218263
To create file 3, I tried this first, which yielded an empty file.
awk 'BEGIN {FS = "\t"; OFS= "\t"}
NR==FNR {a[$1]=$2; next}
{ if($1 in a) print $1, a[$1]}' file1 file2 > file3
The following code confirmed the issue is the index of the array was not recognized; because adding the
else
line actually prints file2 into file3.
awk 'BEGIN {FS = "\t"; OFS= "\t"}
NR==FNR {a[$1]=$2; next}
{if($1 in a)
print $1, a[$1]
else
print $1, $2}' file1 file2 > file3
I am quite puzzled. I wonder what might have caused the issue and how I can fix it?
Thanks in advance.
Xuan
(45 rep)
Mar 29, 2023, 08:54 AM
• Last activity: Mar 29, 2023, 11:51 AM
1
votes
2
answers
1371
views
How to count the number of lines per file in a directory, then create summary of number of files with n lines
I'm trying to create a summary of how many files in a directory have n number of lines in them. I'm using `wc -l * | sort` to print out the number of lines against the name of each file. What I'm trying to achieve is a summary of the directory which would produce: 56 5 60 6 3 7 etc, where the first...
I'm trying to create a summary of how many files in a directory have n number of lines in them. I'm using
wc -l * | sort
to print out the number of lines against the name of each file. What I'm trying to achieve is a summary of the directory which would produce:
56 5
60 6
3 7
etc, where the first column represents number of files and the second represents number of lines.
I've looked at an awk command awk 'END{print NR}' directory/*
which returns the number of lines in the entire directory. I have considered a printf
solution to format the wc -l
output . Any assistance would be greatly greatly appreciated! Thank you
Doodling
(19 rep)
Aug 27, 2022, 06:19 AM
• Last activity: Aug 27, 2022, 10:15 AM
8
votes
2
answers
3324
views
How to set environment variables dynamically for command?
I would like to use some kind of minimalistic template engine with pure bash and `envsubst`: ```sh user@host:~$ env -i FOO=foo BAR="bar baz" envsubst '$FOO,$BAR' \ Reason: `set -x` shows that the argument for `env` becomes one giant string : ```sh + env -i 'FOO='\''foo'\''' 'BAR='\''bar' 'baz'\''' e...
I would like to use some kind of minimalistic template engine with pure bash and
envsubst
:
user@host:~$ env -i FOO=foo BAR="bar baz" envsubst '$FOO,$BAR' \
Reason: set -x
shows that the argument for env
becomes one giant string:
sh
+ env -i 'FOO='\''foo'\''' 'BAR='\''bar' 'baz'\''' envsubst #...
env: ‘baz'’: No such file or directory
```
I must have missed a shell escape lession (again...). How might I rewrite last example to work properly?
user00705358365913
(83 rep)
Jul 22, 2022, 05:30 PM
• Last activity: Jul 22, 2022, 07:56 PM
0
votes
4
answers
456
views
Can't seem to access associative array with named reference in bash
# SETUP PHP81 SYMLINKS declare -A cgi=([path]="/opt/remi/php81/root/usr/bin/php-cgi" [filename]="php-cgi81") declare -A config=([path]="/opt/remi/php81/root/usr/bin/php-config" [filename]="php-config81") declare -A phpize=([path]="/opt/remi/php81/root/usr/bin/phpize" [filename]="phpize81") declare -...
# SETUP PHP81 SYMLINKS
declare -A cgi=([path]="/opt/remi/php81/root/usr/bin/php-cgi" [filename]="php-cgi81")
declare -A config=([path]="/opt/remi/php81/root/usr/bin/php-config" [filename]="php-config81")
declare -A phpize=([path]="/opt/remi/php81/root/usr/bin/phpize" [filename]="phpize81")
declare -A pecl=([path]="/opt/remi/php81/root/usr/bin/pecl" [filename]="pecl81")
declare -A pear=([path]="/opt/remi/php81/root/usr/bin/pear" [filename]="pear81")
declare -a symlinks=("cgi" "config" "phpize" "pecl" "pear")
echo -en "[INFO]: Setting up PHP 8.1 symlinks\n"
cd /usr/bin
for symlink in "${symlinks[@]}";
do
echo -en "[INFO]: Creating symlink for ${symlink}\n"
# Get the array that matches the string in symlink
props="$symlink[@]"
# Print out the filename property eg cgi[filename], config[filename], phpize[filename]
echo ${!props[filename]}
echo -en "\n"
done
There is the section of code giving me issues. It currently outputs
./upgrade.sh
php-cgi81 /opt/remi/php81/root/usr/bin/php-cgi
php-config81 /opt/remi/php81/root/usr/bin/php-config
phpize81 /opt/remi/php81/root/usr/bin/phpize
pecl81 /opt/remi/php81/root/usr/bin/pecl
pear81 /opt/remi/php81/root/usr/bin/pear
The expected output is
./upgrade.sh
php-cgi8=
php-config81
phpize81
pecl81
pear81
This is now solved I posted the solution below but someone down voted it because they dislike my solution but the solution I posted is working. But you decide what you want to use. Thanks for the help.
Corey Rosamond
(1 rep)
Jul 5, 2022, 04:02 PM
• Last activity: Jul 6, 2022, 03:13 PM
0
votes
1
answers
444
views
How does awk handle collisions in a hashmap?
Does `awk` use separate chaining, open addressing or does it have its own way of handling collisions in a hashmap? Do `gawk` and `nawk` implement the same algorithm? Thank you.
Does
awk
use separate chaining, open addressing or does it have its own way of handling collisions in a hashmap?
Do gawk
and nawk
implement the same algorithm?
Thank you.
Zidani Mehdi
(11 rep)
Jun 17, 2022, 09:52 AM
• Last activity: Jun 17, 2022, 05:28 PM
Showing page 1 of 20 total questions