AWK is small but very useful Unix scripting language that is mainly aimed at text files filtering and modification. If you're on embedded device you might expect bigger brothers (as Perl / Python) are not available, but AWK is usually shipped with busybox (= small).
One of missing functionalities is join() function (the opposite of splitting string by regular expression). One can implement it pretty easily, however:
function join(array, sep,
if (sep == "")
sep = " "
else if (sep == SUBSEP) # magic value
sep = ""
result = array
for (i = 2; i in array; i++)
result = result sep array[i]
split(s, arr, "|")
output = join(arr, "|")
As a result input string s will have the same contents as output. The function is useful for scripted modification of CSV files.
Sometimes you have to update many files at once. If you are using an IDE there is option during replace "search in all files". I'll show you how make those massive changes faster, using just Linux command line. Place this file in ~/bin/repl
FILES=$(grep -le "$FROM" `find . -name "$PATTERN"`)
echo Files: $FILES
sed -i "s!$FROM!$TO!g" $FILES
Sample usage is:
repl "*.cpp" "loadFiles" "retrieveFiles"
Some explanation about script:
- grep -l: shows on stdout only filenames of matching files
- grep -e: accepts pattern as argument
- sed -i: changing files "in place"
- grep -le: we want only files that will match $FROM to be updated (minimizes build time after change)
Pretty easy and powerful. Enjoy!
Status command is very important part of any VCS (Version Control System) local interface. It allows you to check your workspace state and ensure correct commit will be created.
Perforce is a commercial VCS that is similar to CVS (revisions per file) and SVN (global revisions of whole tree). It's support for status command is very clumsy. Let's check how we can emulate status with this tool using small script:
echo === extra files not tracked by Perforce ===
find . '!' -type d '!' -executable | p4 -x - have 2>&1 | grep 'not on client' | \
sed '/\/moc_/d;/\.so/d;/\.o /d;/Makefile /d;/\.a /d'
echo === Current changelist ===
p4 changelist -o
As you can see we implemented "ignore" mechanism in above script (by sed filtering). "Extra files not tracked by Perforce" reflects "?"-status from CVS/SVN. "Current changelist" reflects "A,D,U"-statuses from CVS/SVN.
By using such script you can ensure your commit contains all files from your workspace, thus will be buildable on other developers' machines.
Good software developers are lazy persons. They know that any work that can be automated should be automated. They have tools and skills to find automated (or partially automated) solution for many boring tasks. As a bonus only the most interesting tasks (that involve high creativity) remain for them.
I think I'm lazy . I hope it's making my software development better because I like to hire computer for additional "things" apart from typewriter and build services. If you are reading this blog probably you are doing the same. Today I'd like to focus on C++ build process example.