1

The file contents similar to below

abc.w.f.com
asdff.d.f.com
..........

I want to remove everything after dot and changes should be overwrite in same file

abc
asdff

Tried below , but this requires redirect to another file, which i dont want

sed "s/\..*//" Filename  > File2

2 Answers 2

1

Use -i:

sed -i 's/\..*$//' Filename

-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)

0

You can't redirect to the same file: the obvious problem with sed "s/\..*//" Filename > Filename is that the shell opens Filename for writing (and overwrites it) before the file is read by sed.

You'd need to redirect output to a different file, delete the original file, and then rename the newly created file the same as the original. Sed, Perl and AWK can do this for you:

  • sed -i
  • perl -i
  • awk -i inplace

Alternatively, you can use sponge:

  • sed "s/\..*//" Filename | sponge Filename

sponge keeps buffering what it receives from standard input, and only then writes it to Filename.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .