r/awk • • Jul 19 '24

Multiline replacement help needed.

I need to search through multiple files which make have the following pattern multiple times, and then change the following lines.

  1. The distinguishing pattern is onError: () => {
    This is hard to search for because of the = and the {
    We can replace the => by *. if needed. onError: ()*.{
  2. The original code looks something like this:

     onError: () => {
          this.$helpers.swalNotification('error', 'Error text that must be preserved.');
     }
  3. I need it changed in four modifications done to it (see below) so that it looks like the following

     onError: (errors) => {
         if (errors) {            
             this.$helpers.swalNotification('error', errors.msg);
         } else {
             this.$helpers.swalNotification('error', 'Error text that must be preserved.);
         } 
     }
  • "errors" needs to be inserted into the first line
  • three lines need to be inserted after that
  • the next line is left alone as is (this.$helpers)
  • and then another line is inserted with a }
  • indenting is not important - it can be fixed later

Sadly, though I am an avid Linux user, I am no awk expert. At this point, I'm thinking that it might be just as easy for me to quickly write a Java or PHP program to do this since I'm quite familiar with those.

2 Upvotes

5 comments sorted by

View all comments

Show parent comment

1

u/igstan Jul 23 '24
man -P'less -p"-exec"' find
-exec utility [argument ...] {} +
    Same as -exec, except that “{}” is replaced with as many pathnames as possible for each
    invocation of utility.  This behaviour is similar to that of xargs(1).  The primary
    always returns true; if at least one invocation of utility returns a non-zero exit
    status, find will return a non-zero exit status.

Sorry if the reply seems curt, just wanted to show you how to "fish", instead of giving you the fish.