Monday 23 November 2009

Sed script for swapping 2 strings

This is quite a nifty wee sed script - it simply swaps text strings in targeted files within the current directory.

In this case, the files are all C code files (.c extension) and we are swapping TRACE_LOW string and TRACE_HIGH string. TRACE_REPLACE is a temporary string used to help with this.

#!/bin/sh

#for each .c file in the current directory carry out the replacement.
for fl in *.c;
do
echo $fl
echo "Replacing text"

#Take a backup of the file first and then replace LOW with REPLACE
sed -i.backup -e 's/TRACE_LOW/TRACE_REPLACE/g' $fl

#Next, replace HIGH with LOW
sed -i -e 's/TRACE_HIGH/TRACE_LOW/g' $fl

#Finally, replace REPLACE with HIGH
sed -i -e 's/TRACE_REPLACE/TRACE_HIGH/g' $fl
echo "Replacing text...complete"
done

No comments: