Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: insert text in a specific position

Tags:

text

grep

bash

I would like to find a solution to add some characters in a string

Example

folder/dir/directory/file.txt
shares/page.html
etc/downloads/torrent.torrent

Should become

.folder.old/dir/directory/file.txt
.shares.old/page.html
.etc.old/downloads/torrent.torrent

How can i do that? 'grep' would be a correct choose?

Thank you

like image 984
Vittorio Chierici Avatar asked May 07 '26 01:05

Vittorio Chierici


1 Answers

Use sed instead of grep:

sed -i.bak 's#^\([^/]*\)#.\1.old#'

Note that -i will save the result in the original file itself. -i.bak will save your original file with .bak extension in case something goes wrong.

like image 149
anubhava Avatar answered May 08 '26 17:05

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!