Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

search and replace string with backslash

I have a file which contains relative paths like so:

# source.txt
./somefile1
./somefile2
..

what id like to do is replace the ./ with the base dir like so:

# result.txt
/home/foo/bar/somefile1
/home/foo/bar/somefile2

source.txt has some 15 million entries and Id like to replace all the ./ at the start to have the base dir like the above.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Python would be too slow for this .replace("./","/home/foo..") and I am looking for a sed or grep solution.

>Solution :

Like this

$ sed "s@^\./@$/base/dir/@" file
# source.txt
/tmp/somefile1
/tmp/somefile2

To replace in-line:

sed -i "s@^\./@/base/dir/@" file
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading