Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

please advice how to delete all characters in file except numbers and "."

  • Should be implemented by sed or awk or perl ( one line ) or any other idea , so I will add the syntax in my ksh script

remark - the solution must be according to the example in target file ( see spaces in this example ) , so the other characters ( non dot or numbers ) will be replaced by one space

for example the original file:

the target file after delete all characters except numbers and "." charter 0.1
  32 5.500.5.5
  . 54
  3.3.3 .3.
share|improve this question

closed as off topic by MadHatter, Greg Askew, Magellan, mdpc, voretaq7 Nov 15 '12 at 19:40

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

is it your homework? – olibre Nov 15 '12 at 11:41
no I need to find solution for my script this isnt homework ! – yael Nov 15 '12 at 11:48
up vote 2 down vote accepted

Use a negated group [^.0-9] which means anything except dot and numbers. To cleanup infile do:

sed 's/[^.0-9][^.0-9]*/ /g' infile


To replace sequences of the negated group with one space you can use the updated answer.

share|improve this answer
not good - see for example the last line , need to be space between 3.3.3 to .3. – yael Nov 15 '12 at 11:56
@yael you never say to insert spaces. The answer of Thor is correct and meets your specifications (except non-commented spaces in example file) => +1 Please, explain why spaces must be inserted. Edit your question. – olibre Nov 15 '12 at 11:57
yes but see my example - target file - you can see there the space – yael Nov 15 '12 at 12:00
Yael, the fact that your example disagrees with your written specs isn't a reason to argue, it's a reason to fix one or the other so we know which is correct. – MadHatter Nov 15 '12 at 12:04
OK, now line 2 (amongst others) contradicts your spec; the leading 'e' has disappeared instead of being replaced by a space. You really, really need to think hard about what you want before you write it down; an example can clarify a spec, but it can't update or extend it. – MadHatter Nov 15 '12 at 12:54
sed 's/[^.0-9]\+/ /g;s/^ //g;s/ $//g' in-file > target-file


  • s/[^.0-9]\+/ /g replaces consecutive other characters by a single space
  • s/^ //g removes leading spaces
  • s/ $//g removes trailing spaces
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.