-3

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.
yael
  • 2,363
  • 4
  • 28
  • 41

2 Answers2

2

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.

Thor
  • 465
  • 5
  • 14
  • 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. – oHo 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
  • dear Olibre - I need the space because this space is a separator , and by this separator we can verify each number , so without the separator we can't identify the number because the new number will combine from two numbers !!! - please tell me if my explain is clearly ? – yael Nov 15 '12 at 12:07
  • @MADHatter see my update – yael Nov 15 '12 at 12:14
  • yael, line 1 of your example contradicts your updated spec (two plusses have turned into six spaces). This is rapidly turning into a non-question. – MadHatter Nov 15 '12 at 12:35
  • no I neeed only one sopace ( see my update ) I wrie some space s only to clear that we have spaces between number/dot to other number/dot ( space is like seperator ) – yael Nov 15 '12 at 12:41
  • 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
  • ok thx allot , but still we not have solution yet -:( – yael Nov 15 '12 at 13:03
  • @yael: see edit. – Thor Nov 15 '12 at 13:04
2
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
oHo
  • 515
  • 1
  • 6
  • 14