How files can be processed with regex?
There are several ways to apply regular expressions to files, eg. SED or Perl (official Perl).
I have the feeling that Perl is more Powerful than SED. Certainly, I'm more familiar with Perl.
This following example bash script
- reads all the page-*.html files in the site folder,
- backups the html files to .bak,
- processes the files by pipes,
- adds a empty line at the end of the file due to
(cat "$file.bak"; echo -e "\n")
, - replaces the text regex with replacement using Perl,
- and adds a comment after the head tag.
#!/bin/bash
for file in site/page-*.html
do
echo "Process $file";
mv "$file" "$file.bak";
# Read file, process regex and write file
(cat "$file.bak"; echo -e "\n") \
| perl -p -e"s/regex/replacement/g" \
| perl -p -e"s/(<head>)/\1\n<!--Automatically processed-->/" \
> "$file";
done
There are other ways for looping through files with bash.
This script is short, simple and powerful. Very handy.