12

I am trying to get specific information from a bunch of files. Iterating over a list of files,greping for what I need. I know for sure that each grep will give more than 1 result and I want to store that result in an array. After finishing the work specific to file, I want to erase everything from arrays and start afresh for new file.

files_list=`ls`

for f in $files_list
do
        echo $f
        arr1=`cat $f | grep "abc" | grep "xyz"`
        arr2=`cat $f | grep "pqr" | grep "mno"`
        arr3=`cat $f | grep "df"`
        for ((i=0; i<${#arr1[@]}; ++i)) 
        do
            printf "%s  %s %s\n" "${arr1[i]}" "${arr2[i]}" "${arr3[i]}"
        done
        unset $arr1
        unset $arr2
        unset $arr3
done

So I used unset to empty the array but it's giving error.

line 49: unset: `x': not a valid identifier

I don't want to delete a particular member/index of array but entire array itself. Can anyone tell me how to do it?

0

2 Answers 2

28

unset works with variable names, not the values they keep. So:

unset arr1

or, if you want to empty it:

arr1=()
Sign up to request clarification or add additional context in comments.

Comments

0

unset Versus Wiping an Array

As @'oguz ismail' mentioned, the OP can either use the unset command or wipe the array (empty it).

There are important nuances between the two methods.

unset completely removes the array and its structure (type of array, all its keys, all values) from memory. The array no longer exists. If one wants to use it again, then one would need to declare it again.

versus

myArray=() retains the array type (indexed or associative), but wipes out all keys and values. The array itself continues to exist.

When to Use Either Method

This is where things get nuanced.

Pros and Cons of Unset

If your needs are basic; let's say you want to wipe the contents of an array in a script, and the array and its content will never be needed after that point. Well, then you might as well use unset IMHO, though you could use either. Technically, unset is the superior choice because it releases the memory back to the kernel. But for most applications, it really doesn't matter unless your array is very large.

Where the OP may run into problems is when downstream in the script, certain commands that are looking for the array or one of its elements (keys). Which commands? Any line that requires the array or one of its elements to be actioned somehow.

For example, a command such as this: if [ -n "${my_array[$key]}" ]; then ... will work just fine and will not complain if the array doesn't exist or the element $key doesn't exist. The test will fail, and the script will move on. No errors. No complaining from the SHell.

However, in the OP's scenario they clearly had some sort of command on line 49 that does expect the array to exist, as you wrote:

So I used unset to empty the array but it's giving error.

line 49: unset: x': not a valid identifier`

Pros and Cons of Wiping the Array

Alternatively, wiping the array causes the contents of the array to be wiped out (its keys and their values), while retaining the array definition itself.

So, in the OP's scenario you'd possibly want to go this route in order to avoid the error you encountered on line 49. However, this would not necessarily solve your error message issue. It would depend on what caused the error. Since the OP did not share the entire script, it's not clear whether this approach would make a difference or not.

The bottom line is the upside of this approach (wiping the array, but leaving its structure intact) will not cause commands to fail that check for or require the presence of the array, though this comes at the expense of increased memory utilization (potentially a waste).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.