Consider the following code that does a simple loop over code files:
#!/bin/bash
dir="."
find $dir -name *.cpp -o -name *.h | while read file; do
echo "processing: "$file
# Process file here
done
Every file start with messy includes like this
#include<iostream>
#include <vector>
#include"this_is_file1.h"
#include "this_is_file2.h"
#include "This_Is_File3.h"
#include"thisIsFile4.h"
#include "ThisIsFile5.h"
#include"dir/thisIsFile6.h"
#include "dir/ThisIsFile7.h"
#include "dir/ThisIsFile8.txx"
#include "dir/ThisIsFILe9.txx"
that I would like to transform like this (and overwrite the old file)
#include <iostream>
#include <vector>
#include "this_is_file1.h"
#include "this_is_file2.h"
#include "this_is_file3.h"
#include "this_is_file4.h"
#include "this_is_file5.h"
#include "this_is_file6.h"
#include "this_is_file7.h"
#include "this_is_file8.txx"
#include "this_is_file9.txx"
More formally:
- There should be exactly one space between the include and the file name
- There should not be any capital letter, and every consecutive sequence of capital letters that has been replaced should be preceded by exactly one underscore, except if it's the beginning of the file name
- There should not be any directory name
How to do that with bash?
*
has to be escaped when part of a name pattern infind
commands and alsowhile..read
is the worst way of doing this.