Using sed in ba.sh

Posted in bash

I just worked on a new setup shell script and observed that it’s not quite self-explaining how to use

sed

command to paste a specific path into a file already existing. So let me recap the details for you in this post.

Remember the following while using sed: If you want to make permanent changes you must use -i after the sed command. Second you should always use single quotes ‘. Otherwise sed may interpret the input variables for itself instead paste them into the file presented.

Case 1 - Replace line with matching pattern

To replace an existing line with a search pattern you can just use:

sed 's/searchvalue/replacevalue/' filename

where you replace the “searchvalue” with your pattern (like a variable in a config file) and the “replacevalue” with the new one. Please remember that the “s” prefix indicates that you want to replace the matching line. “filename” could be an absolute path or a variable you have pre-defined and pointing to a specific file.

The use case is pretty simple: Imagine you are writing a bash script to install an application or kind of a config tool. In this case you may face a config file that sets default values during installation like

Config: Default

In this case you could use the following sed command to change the config in your script:

sed 's/Config: Default/Config: NewValue/' /path/to/file.conf

Case 2 - Insert line after pattern

If you want to insert a line instead of replacing you can easily modify the code:

sed '/searchvalue/a replacevalue/' filename

So you can see that we don’t use “s” but “a” after the second delimiter. This indicates to insert after the pattern.

Case 3 - Insert line before pattern

In case you want to insert a line before the pattern you can modify the code as:

sed '/searchvalue/i replacevalue/' filename

So we just use “i” instead of “a” from the code before.

And whats about the path?

After I solved the issue how to enter a line before the matching line I was wondering how to enter a path value that contains also slashes as the delimiter. If you just enter it as “replacevalue” you would observe that sed will interpret them as own delimiter instead of pasting them into the file. To get rid of this behaviour you have basically two options: Change the delimiter that sed uses or prefix the slashes.

I would strongly recommend to use prefixes as you might not know which symbols are used in the expression that you parse to sed and you might face an issue if they match your new delimiters.

So to insert a path into a file you can just use:

sed '/searchvalue/i replace\/path\/value/' filename

So you observe that we just use the backslash within the path to tell sed that this is not a delimiter but part of the value we would like to insert (or replace).

Recap

There are a lot of manipulations you can do with sed. It’s much more powerful as just echo the lines into a file. However it could get really confusing from time to time and if you are searching for complex pattern. I hope this short excurse saved you some time in research for that specific case.


macandtech.com