Process files with regex

Estimated reading time of this article: 0 minutes 47 seconds

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

  1. reads all the page-*.html files in the site folder,
  2. backups the html files to .bak,
  3. processes the files by pipes,
  4. adds a empty line at the end of the file due to (cat "$file.bak"; echo -e "\n"),
  5. replaces the text regex with replacement using Perl,
  6. and adds a comment after the head tag.

for file in site/page-*.html
    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";

There are other ways for looping through files with bash.

This script is short, simple and powerful. Very handy.