Skip to content

Instantly share code, notes, and snippets.

@fabiolimace
Last active April 5, 2025 21:39
Show Gist options
  • Save fabiolimace/2f3cf066eb3ec3c61b70eb93869c8646 to your computer and use it in GitHub Desktop.
Save fabiolimace/2f3cf066eb3ec3c61b70eb93869c8646 to your computer and use it in GitHub Desktop.
Join numeric tokens with AWK. Moved to https://github.com/fabiolimace/awk-tools
function join_numbers( i, n, x) {
while (match($0, /[ ][0-9,.()-]+[ ][0-9,.()-]+[ ]?/)) {
i = RSTART;
n = RLENGTH;
x = substr($0, i, n);
gsub(" ", "", x);
$0 = substr($0, 0, i-1) " " x " " substr($0, i+n)
}
}
{
print;
join_numbers();
print;
}
Meu telefone é 85 988887777.
Meu telefone é (85) 98888-7777.
Na guerra morreram 1 500 soldados.
@fabiolimace
Copy link
Author

fabiolimace commented Jan 16, 2025

Example:

awk -f join-numbers.awk text.txt
Meu telefone é 85 988887777.
Meu telefone é 85988887777. 

Meu telefone é (85) 98888-7777.
Meu telefone é (85)98888-7777. 

Na guerra morreram 1 500 soldados.
Na guerra morreram 1500 soldados.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment