To remove all blank spaces from a string or field in AWK, you can use the gsub
function.
Here is an example of how you can remove all blank spaces from a string:
# Define the string str = " This is a string with multiple spaces " # Remove all blank spaces from the string gsub(/ /, "", str) # Print the modified string print strSource:www.lautturi.com
This will output the string "Thisisastringwithmultiplespaces"
.
You can also use the gsub
function to remove all blank spaces from a field. For example, consider the following input file:
field1 field2 value1 value2
To remove all blank spaces from the second field, you can use the following AWK script:
# Read the input file { # Remove all blank spaces from the second field gsub(/ /, "", $2) # Print the modified fields print $1, $2 }
This will output the following:
field1 field2 value1 value2
Overall, the gsub
function is a useful tool for removing all blank spaces from a string or field in AWK. It allows you to easily modify strings and fields and perform other tasks based on the modified values.