help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Function or Direct Call to Awk Commands


From: Heime
Subject: Function or Direct Call to Awk Commands
Date: Sun, 08 Jun 2025 00:55:48 +0000


I wonder if it is legit to have a awk file call a function or process lines 
directly using



function print_dk_sections(line)
 {
   if (line ~ /^### DK/) {
      printing = 1
      empty_count = 0
      return 0
    }
    
   if (printing) {
     print line
       if (line ~ /^$/) {
         empty_count++
         if (empty_count == 2) { printing = 0 }
       }
    }
    return 1
 }



{
  
  ## Preferred default mode due to better modularity and 
  ## maintainability using a dedicated function to process lines.
  if (mode == "fn") {
     print_dk_sections($0)
   }

  ## ------------------------------------------------------------
  ## Idiomatic and efficient but less modular than using a function.
  ## Uses pattern-action blocks directly inside the condition.
  else if (mode == "direct-pa") {
    
    /^### DK/ {
       printing=1;
       empty_count=0;
       next;  # Skip printing this line
     }

    printing {
      print
      if ($0 ~ /^$/) {
         empty_count++
         if (empty_count == 2) { printing = 0 }
       }
     }
   
   }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]