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
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.

1

Use -i:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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