Remove Elements from Array Using Linux AWK delete

In this tutorial, you’ll learn how to remove elements from arrays in Linux using  delete statement in awk.

You’ll learn how to remove elements by index or value, using regular expressions, and remove elements from nested arrays.

 

 

Remove Element from Array

First, consider a sample dataset like this:

101,120
102,250
103,180
104,300

Here, each line represents a user with their ID and usage separated by a comma.

To remove the user with ID 103, you can use the awk delete statement like this:

echo -e "101,120\n102,250\n103,180\n104,300" | 
awk -F, '{ usage[$1] = $2 } 
END { delete usage["103"]; 
for (id in usage) 
print id, usage[id] }'

Output:

101 120
102 250
104 300

The awk command creates an array usage.

The delete usage["103"] removes the element with the key “103”.

Finally, the for loop prints the remaining elements in the array.

 

Remove Elements Based on Conditions

You can remove elements that meet the condition (usage less than 200 minutes):

echo -e "101,120\n102,250\n103,180\n104,300" |
awk -F, '{ usage[$1] = $2 } 
END { for (id in usage) 
if (usage[id] < 200) 
delete usage[id]; 
for (id in usage) 
print id, usage[id] }'

Output:

102 250
104 300

Here we iterate over the array and check the condition if (usage[id] < 200).

If the condition is true, the delete statement removes that element. The remaining records are then printed.

 

Remove Elements by Index

To remove elements by index, you can specify the array element index in the delete statement.

Suppose you need to remove the user with a specific index, say index 102.

echo -e "101,120\n102,250\n103,180\n104,300" |
awk -F, '{ usage[$1] = $2 } 
END { delete usage["102"]; 
for (id in usage) 
print id, usage[id] }'

Output:

101 120
103 180
104 300

The awk command creates an associative array usage with user IDs as keys.

The delete usage["102"] statement then removes the element with the key “102”.

The final loop prints out the remaining elements in the array.

 

Remove First/Last Element

Remove First Element

To remove the first element, we need to identify it as we read through the data:

echo -e "101,120\n102,250\n103,180\n104,300" | 
awk -F, 'NR == 1 { first = $1 } { usage[$1] = $2 } 
END { delete usage[first]; 
for (id in usage) 
print id, usage[id] }'

Output:

102 250
103 180
104 300

In this command, NR == 1 { first = $1 } captures the first record’s user ID.

The delete usage[first] command then removes this first element.

Remove Last Element

Removing the last element requires keeping track of all records and identifying the last one:

echo -e "101,120\n102,250\n103,180\n104,300" | 
awk -F, '{ usage[$1] = $2; last = $1 } 
END { delete usage[last]; 
for (id in usage) 
print id, usage[id] }'

Output:

101 120
102 250
103 180

This command works by updating last with the current record’s user ID in each iteration.

When the end of the file is reached, last holds the ID of the last record, which is then removed with delete usage[last].

 

Remove Element by Value

To remove elements by value, you can iterate over array items and check the value using if statement and remove the element if it’s true.

Suppose you want to remove all records where the usage is exactly 180 minutes.

echo -e "101,120\n102,250\n103,180\n104,300" | 
awk -F, '{ usage[$1] = $2 } 
END { for (id in usage) 
if (usage[id] == 180) 
delete usage[id]; 
for (id in usage) 
print id, usage[id] }'

Output:

101 120
102 250
104 300

The awk command creates an associative array usage and iterates over it.

It checks for the condition if (usage[id] == 180) and if it’s true, the delete usage[id] statement removes that element.

 

Remove Element Using Regular Expressions

Let’s say you want to remove users whose ID starts with ’10’ and ends with ‘1’:

echo -e "101,120\n102,250\n103,180\n104,300" | 
awk -F, '{ usage[$1] = $2 } 
END { for (id in usage) 
if (id ~ /^10.*1$/) 
delete usage[id]; 
for (id in usage) 
print id, usage[id] }'

Output:

102 250
103 180
104 300

In this command, awk first creates an associative array usage with all user IDs and their respective usage minutes.

In the END block, it iterates over the array and checks each key (user ID) against the regular expression ^10.*1$.

If a key matches, the delete usage[id] statement removes that element from the array.

 

Remove Elements from Nested Arrays

Consider the following dataset:

101,Internet,120
101,Phone,180
102,Internet,250
103,Phone,180
104,Internet,300

Suppose you want to remove all ‘Phone’ service records for all users:

echo -e "101,Internet,120\n101,Phone,180\n102,Internet,250\n103,Phone,180\n104,Internet,300" | 
awk -F, '{ usage[$1][$2] = $3 } 
END { for (id in usage) 
delete usage[id]["Phone"]; 
for (id in usage) 
for (service in usage[id]) 
print id, service, usage[id][service] }'

Output:

101 Internet 120
102 Internet 250
104 Internet 300

In this command, awk first constructs a nested array usage, where each user ID maps to another associative array of services and their usage.

In the END block, it iterates over the usage array and applies the delete statement to remove the ‘Phone’ service records for each user.

 

Remove All Elements

To remove all elements from an array, you can use the delete statement on the array itself:

echo -e "101,120\n102,250\n103,180\n104,300" | 
awk -F, '{ usage[$1] = $2 } 
END { delete usage }'

In this command, the delete usage statement is used in the END block.

Leave a Reply

Your email address will not be published. Required fields are marked *