How to Replace a Path Name String Containing a . (dot) & / (slash) with a / Inside Text-file?


If you have ever come across a text file that has some unnecessary characters added before file/directory paths & you need to get rid of that, how would you do it? E.g.

./opt//home/shashank/scripts

to

/home/shashank/scripts

Easy way, just edit the file and that’s it 😀

Good one 🙂 But what if the file has hundreds of lines? 😉

So, below is how to do it inside Vi editor 🙂 Open the file & issue this –

%s/.\/opt\/\//\/

Mission accomplished 😀

Since there was ./ in path, we escaped that using \ Then there were two // so we escaped them using \/\/ And since we had to replace it with a /, we added another /\/

Syntax of replacing text in Vi is –

:%s/text_to_be_replaced/text_to_be_replaced_with
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s