bsteo
bsteo

Reputation: 1779

GNU sed remove spaces from digit number in text file

I have the following bogus data:

Dominik Dryja|4111 2386 0873 0189|0315
Laivonen Eero|5111 0620 0750 8041|0813
Jukka Valimaa|5111 6500 0489 0035|0415
Rafael Diaz de Leon|4111 3036 6209 4796|0516
Mr Jonathan Bird|4111 6150 0291 7415|0215
ERRANTE VINCENZO|4222 6111 0038 6639|0114
YOSHIO MOTOKI|5222 3200 0374 7129|0513
I. A. VLACHOGIANNIS|4333 0115 6936 2003|0315
Soumya Kanti Deb|4333 0590 0165 4877|1019
WU KE ZHAN|5444 8213 7236 0431|0716

I try to strip the space ONLY from the digit number to look like this:

Dominik Dryja|4111238608730189|0315
Laivonen Eero|5111062007508041|0813
Jukka Valimaa|5111650004890035|0415
Rafael Diaz de Leon|4111303662094796|0516
Mr Jonathan Bird|4111615002917415|0215
ERRANTE VINCENZO|4222611100386639|0114
YOSHIO MOTOKI|5222320003747129|0513
I. A. VLACHOGIANNIS|4333011569362003|0315
Soumya Kanti Deb|4333059001654877|1019
WU KE ZHAN|5444821372360431|0716

Tried sed -r '#|([0-9]{4})\ ([0-9]{4})\ ([0-9]{4})\ ([0-9]{4})|#\1\2\3\4#g' for some reason without success. Any idea where I'm mistaken?

Thanks!

Upvotes: 3

Views: 342

Answers (2)

anubhava
anubhava

Reputation: 784958

You can simplify your sed:

sed 's/\([0-9]\{4\}\) /\1/g' inFile

Upvotes: 2

aragaer
aragaer

Reputation: 17848

Assuming there's always a single space between numbers:

sed 's/\([0-9]\) \([0-9]\)/\1\2/g'

Works with your example. The code is simple - remove all single spaces if they happen between two digits.

Upvotes: 1

Related Questions