The Linux find replace

Lately I need to write a lot of Linux scripts. And it came to my attention that it is a pain in the a#@! to replace a certain series of characters in a file. That’s why I’ll give you a quick and very basic explanation on how to do this.

I literally only have a command line available on my server, so that’s also how I am going to explain. no GUI all commands.

Right so suppose you want to replace the word PUPPY in the file test.txt with let’s say.. DOG. I’m not original in creating examples.
Alright, now in order to do this, you can use the command/tool SED. The exact command to do this is:

sed ‘s/PUPPY/DOG/g’ test.txt

This just means… sed substitute (s) PUPPY to DOG globally (g) in the file test.txt.
So your command will always be something like sed ‘s/OLDSTRING/NEWSTRING/[g]’ file(s). The g is optional.

Now if you want to change PUPPY to DOG in all txt files in the folder you can use the same command with just a small change:

sed ‘s/PUPPY/DOG/g’ *.txt

The * is a substitute for ALL. So in this case all files with the extension txt.

So far so good, nothing hard there. But the thing that I’ve been struggling with is replacing a string (har har har..) with the value of a environment variable.
Let me quickly explain, I declared a variable that contains the location of a certain file. My variable name is RLOC. And if I want to see the value of this variable I use this command:

echo $RLOC

The result will be the location of my file. Now, I have a script that contains a value NEWFILE and I want to replace it with the content of the variable. Not with the variable name itself! I really need to value.
If we use the command sed ‘s/NEWFILE/$RLOC/g’ test.txt, the result will be that NEWFILE is replaced by $RLOC. Exactly what I don’t want…
If you wanna do something like this, the command needs to be altered a bit.

sed ‘s/NEWFILE/’$RLOC’/g’

Sed expects a string, so in order to get the value of the variable in this string, you need to put the variable outside of the string value. Ironically enough..
Now the word NEWFILE will be replaced by the path of the file. OR NOT.. because I encountered another issue.

The path in the variable is something like /var/bin/. Since sed used slashes to locate the old string and the new string it gets confused because there are now 4 slashes in the command in stead of 2.
So in order to really “print” the slashes I need to use an escape character. This is a character that tells a command or a program that a “special” character will follow, like for example a / or $.
The escape character needed here is \.
What I did is change the value of my variable from /var/bin/ to \/var\/bin\/ and run the command again. That works like a charm.

Didn’t I tell you it is a pain?

And this is just very basic stuff, you have a lot of other options available for this command. Maybe I’ll explain this more in detail in a next post.

But right now, I had it with this sed stuff :P…


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: