Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

how to delete all characters in file except numbers and "." , each word (numbers/dot) should be in new line in file see example2

  • the solution can be with sed or awk or ksh syntax

remark - the solution must be according to the example 2

example 1

file before edit

example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line
  32 5.500.5.5
  3.3.3 .3.
share|improve this question
Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line? – jw013 Nov 15 '12 at 19:15
This looks an awful lot like this question on ServerFault -- did you not get a good answer there? – glenn jackman Nov 15 '12 at 21:29
up vote 3 down vote accepted

This is a classic tr use case, so the simplest way is:

tr -cs '[:digit:].' '[\n*]' < input > output

The [:digit:]. argument specifies the characters to match (digits and dot). The [\n*] specifies the characters to replace with (replace everything with newline). The -c option inverts the first argument since we want everything except digits and dot. The -s squeezes consecutive newlines from the second string into one.

share|improve this answer

You can use sed to replace any unwanted character to a newline, and then grep to get rid of empty lines:

sed 's/[^0-9.]/\n/g' | grep . 

Note that the result is different from the one you posted: 32 5.500.5.5 is split to two lines, as well as the last line.

Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.

perl -ne 'print "$_\n" for grep /./, split /[^0-9.]+/'
share|improve this answer
Note that \n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character. – Stéphane Chazelas Nov 15 '12 at 19:39

Here's one way with GNU sed:

sed ':a;{N;s/[^\.0-9]\+/\n/g};ba' file

Here's how it works:

  1. Create a branch label;
  2. Append current/next line to register;
  3. Branch if not last line;
  4. Replace all groups that aren't matching with newlines.

Using the branch avoids spurious newlines.

share|improve this answer

grep can do it:

grep -o '[0-9.]\+'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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