search and replace string with backslash

Advertisements

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.

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

Leave a ReplyCancel reply