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

How to Avoid Pushing In-development Changes to The Production Repository

Once a code base is sophisticated enough to require both a public, customer-facing production environment and private development environment, it becomes vital to prevent potentially breaking development changes from being accidentally pushed to production. This post will go over three methods of preventing development changes from being pushed to production: Changing the Remote’s URL Using Git Hooks Adding Branch Protection Changing the Remote’s URL to Prevent Accidental Pushes to the Production Branch This method prevents accidentally pushing in-development changes to

Read More »
Python logo

Useful Python Packages For Parsing HTML Report

Static analysis tools such as IAR C-STAT, Cppcheck, and Flawfinder are able to generate reports in the form of HTML files. To learn how and why it is necessary to add a static analysis tool to your firmware builds, check out our 10 Easy Steps to Add Static Analysis to Your Firmware Build and Static Analysis: Is It Necessary for Your Automatic Build? posts. Although the generated reports can be uploaded to the build platform as artifacts, your team will

Read More »
Depiction of data being sent around the globe

Simulators in EmbedOps: Another Reason DevOps is Beneficial to your Embedded Development Workflow

Arm is no stranger to DevOps or Continuous Integration (CI). They have  several pipeline scripts available  for Travis CI and outline how they use Jenkins to build and test Mbed OS. But so far, there has not been a demonstration of how to use a simulator in pipelines. We partnered with Arm to show how to use Fixed Virtual Platforms (FVPs) to test your code in a CI pipeline. We are using EmbedOps, a CI tool compatible with GitLab that

Read More »