Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

  192.0.22.1++0.1
  e32)5.500.5.5*kjcdr
  ##@$1.1.1.1+++jmjh
  1.1.1.1333
  33331.1.1.1
  @5.5.5.??????
  ~3de.ede5.5.5.5
  1.1.1.13444r54
  192.9.30.174
  &&^#%5.5.5.5
  :5.5.5.5@%%^^&*
  :5.5.5.5:
  **22.22.22.22
  172.78.0.1()*5.4.3.277
  3.3.3ki.3.

the target file after delete all characters except numbers and "." charter

  192.0.22.1 0.1
  32 5.500.5.5
  1.1.1.1
  1.1.1.1333
  33331.1.1.1
  5.5.5.
  . 5.5.5.5
  1.1.1.13444 54
  192.9.30.174
  5.5.5.5
  5.5.5.5
  5.5.5.5
  22.22.22.22
  172.78.0.1 5.4.3.277
  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 professional 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.

1  
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
add comment

2 Answers

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

Edit

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
2  
@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
1  
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
show 6 more comments
sed 's/[^.0-9]\+/ /g;s/^ //g;s/ $//g' in-file > target-file

explanation:

  • 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
add comment

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