How to Modify a File Using SED

Close up motherboard shot

Let’s take a look at a microchip project that was found to cause issues in the past. Upon trying to build the problematic example it was found that one of the Makefiles generated throughout the use of the Makefile generator command, “prjMakefilesGenerator“ contained invalid paths. Rather than modify the make file manually every time the Makefile generator command was called, it was decided that SED could be used to automate the process.

prjMakefilesGenerator -v <PATH_TO_MICROCHIP_PROJECT>

# Example
PROJ_PATH="projects/microchip/same70_xult/mplab/aws_demos/firmware/aws_demos.X"
prjMakefilesGenerator -v ${PROJ_PATH

The following commands are what were used to attempt to build the project.

pushd ${PROJ_PATH}
make clean
make all
popd

Those commands then produced the following error messages indicating the build failed.

User defined post-build step: [/opt/microchip/xc32/v2.41/bin\\xc32-objcopy -p -I ihex -O binary dist/same70_xult_nvm/production/aws_demos.X.production.hex dist/same70_xult_nvm/production/aws_demos.X.production.bin]
/bin/sh: 1: /opt/microchip/xc32/v2.41/binxc32-objcopy: not found
nbproject/Makefile-same70_xult_nvm.mk:108: recipe for target '.build-conf' failed
make[2]: *** [.build-conf] Error 127
make[2]: Leaving directory '/microchip-mqtt/projects/microchip/same70_xult/mplab/aws_demos/firmware/aws_demos.X'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make[1]: *** [.build-impl] Error 2
make[1]: Leaving directory '/microchip-mqtt/projects/microchip/same70_xult/mplab/aws_demos/firmware/aws_demos.X'
nbproject/Makefile-impl.mk:54: recipe for target '.all-impl' failed
make: *** [.all-impl] Error 2
Book a Call with Dojo Five Embedded Experts

From the first line of the log, it can be seen that the path to xc32-objcopy was not correctly converted to forward slash. Fortunately, SED was able to be leveraged to automatically fix this problem in future attempts to build the project. The SED command is a stream editor that can perform insertion, deletion, and substitution of text. What was needed from SED was essentially to search for the incorrect path and replace it with the valid path.

# sed command for substituing string
$sed "s/<SEARCHING_PATTERN>/<REPLACEMENT_STRING>/" <PATH_TO_THE_FILE>

# Example
$cat example.txt
This file has invalid paths

$ sed "s/invalid/valid/" example.txt
This file has valid paths

The s character in the example above demonstrates the substitution operation. The forward slashes / are used as delimiters in the replacement command. When running the command above, it would only print out the content with the substituted string. To modify the file itself, the -i option is needed for in-place substitution.

if [[ -e ${FILE_TO_BE_MODIFIED} ]]; then
    sed -i "s/<SEARCHING_PATTERN>/<REPLACEMENT_STRING>/g" ${FILE_TO_BE_MODIFIED}
fi

Before these concepts could be applied to the problematic build commands, a few more things were needed. First was to check if the file that needed to be modified existed, and if it did, then the file would be modified in-place. Secondly the g character at the end of the string represents a global replacement. In this case, the desired change was to change the back-slashes in the Makefile to forward-slashes. Finally, the completed SED command could be pasted above the make command as seen below. In doing so, all subsequent attempts to build the project were successful.

PROJ_PATH="projects/microchip/same70_xult/mplab/aws_demos/firmware/aws_demos.X"
PROJ_CONF="same70_xult_nvm"

pushd ${PROJ_PATH}
if [[ -e nbproject/Makefile-${PROJ_CONF}.mk ]]; then
    sed -i "s/\\\\\\/\\//g" nbproject/Makefile-${PROJ_CONF}.mk
fi
make clean
make all
popd

SED is a powerful tool that was able to assist in solving the build errors described above. Aside from this example, there may be numerous other times when files or configs may need to be modified such as a Doxyfile output directory and project name. In those cases, consider using SED as it will most definitely come in handy.

Alternatives to SED

Is SED not your style? Not to worry, there are many other ways to accomplish the same thing. Tools like awk, may be used to accomplish a similar task to what was demonstrated above, and if you are more familiar with some other tools you should still be able to incorporate what was discussed above.

What about Windows?

While Windows may be lacking some of the tools found in a UNIX based system, many people online have had success using features of powershell to act in a similar way to SED. Also consider installing WSL to get access to a CMD-line interface that may be interacted with as if it was a Linux system.

Next Steps

We have talented engineers on hand ready to help you with all aspects of your EmbedOps journey. You can Book a Call with us to discuss your embedded project, or sign up for free for our EmbedOps CI and build management product.

Sign up to get our content updates!

Unlock the full potential of your embedded projects with our expert insights! Dive into our comprehensive resources to stay ahead in firmware development. Subscribe now to get the latest best practices and guides delivered straight to your inbox.

Sign Up for Updates

Discover why Dojo Five EmbedOps is the embedded enterprise choice for build tool and test management.

Sign up to receive a free account to the EmbedOps platform and start building with confidence..

  • Connect a repo
  • Use Dev Containers with your Continuous Integration (CI) provider
  • Analyze memory usage
  • Integrate and visualize static analysis results
  • Perform Hardware-in-the-Loop (HIL) tests
  • Install the Command Line Interface for a developer-friendly experience

Subscribe to our Monthly Newsletter

Subscribe to our monthly newsletter for development insights delivered straight to your inbox.

Interested in learning more?

Best-in-class embedded firmware content, resources and best practices

Laptop with some code on screen

I want to write my first embedded program. Where do I start?

The boom in the Internet of Things (IoT) commercial devices and hobbyist platforms like the Raspberry Pi and Arduino have created a lot of options, offering inexpensive platforms with easy to use development tools for creating embedded projects. You have a lot of options to choose from. An embedded development platform is typically a microcontroller chip mounted on a circuit board designed to show off its features. There are typically two types out there: there are inexpensive versions, sometimes called

Read More »
Medical device monitoring vitals

IEC-62304 Medical Device Software – Software Life Cycle Processes Primer – Part 1

IEC-62304 Software Lifecycle requires a lot of self-reflection to scrutinize and document your development processes. There is an endless pursuit of perfection when it comes to heavily regulated industries. How can you guarantee something will have zero defects? That’s a pretty hefty task. The regulatory approach for the medical device industry is process control. The concept essentially states that if you document how every step must be completed, and provide checks to show every step has been completed properly, you

Read More »
Operating room filled with medical devices

IEC-62304 Medical Device Software – Software Life Cycle Processes Primer – Part II

Part I provides some background to IEC-62304. Part II provides a slightly more in-depth look at some of the specifics. The IEC 62304 Medical Device Software – Software Lifecycle Processes looks into your development processes for creating and maintaining your software. The standard is available for purchase here. So what activities does the standard look at? Here are some of the major topics. For any given topic, there will be a lot more specifics. This will look at a few

Read More »