I am trying to edit linux file using sed or other command.

This is my File t:


I want to change File t to be:


So text after the dot should be added to start of line followed by a forward slash.

What is sed or other command I can use to make this change?


2 Answers 2


You can do this change with

sed 's|.*\.\(.*\)|\1/&|'

which uses | as the delimiter, so we can use / as a literal character without escaping it.  To change the file t in place, use

sed -i 's|.*\.\(.*\)|\1/&|' t
  • Thanks so much! It works like a charm. Updated file t after running sed -i 's|.*\.(.*)|\1/&|' t: xlsx/first.xlsx txt/second.txt ksh/third.ksh
    – wes_k
    Commented Jan 29, 2022 at 11:46
  • Beware that assumes the text is encoded as per the locale's charmap. On the output of printf 'St\351phane.Chazelas.R\351sum\351.pdf\n' (which is Stéphane.Chazelas.Résumé.pdf but encoded in iso8859-1), and in a UTF-8 locale, you'd get St$'\351'R/phane.Chazelas.R$'\351'sum$'\351'.pdf (at least with GNU sed). You could run that under LC_ALL=C to avoid this kind of problem. Commented Jan 29, 2022 at 12:59

Using awk instead of sed:

awk -F . '{ printf "%s/%s\n", $NF, $0 }' file

or, using / as the output field separator,

awk -F . -v OFS=/ '{ print $NF, $0 }' file

These treat . as a field delimiter and output the last such field ($NF) followed by a slash and the original line ($0).

With a recent release of GNU awk, you could make the modification to the file in-place using

awk -i inplace -F . '{ printf "%s/%s\n", $NF, $0 }' file

You must log in to answer this question.

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