Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I remove the . character(s) that start in the beginning of each number or end on each number?

Remark – perl one liner also good alternative for sed.

Example input:

 .23.12.44.5.
 .233.3.3.3
 23.4.5.3.2..
 ....33.2.3.45.5
 .3.3.2.....

Expected output:

 23.12.44.5
 233.3.3.3
 23.4.5.3.2
 33.2.3.45.5
 3.3.2

(note that the lines may end and/or begin with blanks (spaces or tabs) which should be preserved).

share|improve this question
    
Is your input file really having a leading space in each line, as you posted? –  manatwork May 12 '13 at 17:20
    
Could be spaces or TAB , –  yael May 12 '13 at 17:21
add comment

3 Answers

up vote 3 down vote accepted

Using the standard syntax (since the OP mentioned Solaris):

sed 's/^\([[:blank:]]*\)\.*/\1/;s/\.*\([[:blank:]]*\)$/\1/'

On Solaris, as usual, you may need to call /usr/xpg4/bin/sed or command -p sed

share|improve this answer
add comment

Here you go:

sed -e 's/^\(\s*\)\.\+/\1/;s/\.\+\s*$//' your_file

This will print the modified file to standard output. To write to another file, use:

sed -e 's/^\(\s*\)\.\+/\1/;s/\.\+\s*$//' your_file >new_file

To modify the file in place, use

sed -i -e 's/^\(\s*\)\.\+/\1/;s/\.\+\s*$//' your_file

EDIT

  • Modified the regexes to account for (possible) leading or trailing whitespace.
  • Modified the regexes to preserve leading whitespace as per manatwork's comment.
share|improve this answer
1  
Note that according to the example, the leading whitespaces should be kept. –  manatwork May 12 '13 at 17:24
    
Right you are. Regexes modified. –  Joseph R. May 12 '13 at 17:27
1  
That's GNU syntax, the OP has a /solaris tag. –  Stephane Chazelas May 12 '13 at 20:00
add comment
$ cat foo
.23.12.44.5.
 .233.3.3.3
line1
 23.4.5.3.2..
 ....33.2.3.45.5
 .3.3.2.....

line2
$ sed 's/^ *\.*//;s/\.* *$//' foo
23.12.44.5
233.3.3.3
line1
23.4.5.3.2
33.2.3.45.5
3.3.2

line2
share|improve this answer
    
This only deals with spaces not TABs. It also eats leading whitespace, which is not required (see OP and manatwork's comment on my answer). –  Joseph R. May 12 '13 at 17:49
add comment

Your Answer

 
discard

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.