Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using variables in search pattern in awk script

Tags:

shell

awk

#!/usr/local/bin/gawk -f  `

{  
awkvar2="/id=22/";  
awkvar3="/end/";  


if ($0 ~ awkvar2) {  
    triggered=1;  
  }  
  if (triggered) {  
     print;  
     if ($0 ~ awkvar3) {  
        triggered=0;  
        print "\n-----------------------------------------------\n"  
     }  
  }  
}  

this awk script is not working for me i am trying to search from one line to another i.e id=22 till end (the reason i am not using /<string>/,/<string>/ is because i want a big line after each block of search) and i want this using variables only.
i could directly use the patterns if ($0 ~ /end/) { but i dont want to do that, i want to use the variables inside the search pattern (reason is i will be getting the values in the variables dynamically thorough the shell)

please advise me how to use variables inside the search pattern for awk

thanks...

like image 627
Omkar Avatar asked Nov 24 '10 12:11

Omkar


People also ask

Can you use variables in awk?

Variables are used to store any value temporary in any programming language. Defining the variable in awk command is similar to bash scripting language and it works like bash when the shell variable is used with a single quote and double quote. Awk command has many built-in variables for various purposes.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.

What is awk '{ print $1 }'?

awk treats tab or whitespace for file separator by default. Awk actually uses some variables for each data field found. $0 for whole line. $1 for first field. $2 for second field.

How do I use NF in awk?

NF is a predefined variable whose value is the number of fields in the current record. awk automatically updates the value of NF each time it reads a record. No matter how many fields there are, the last field in a record can be represented by $NF . So, $NF is the same as $7 , which is ' example.


1 Answers

{
awkvar2="id=22";
awkvar3="end"; 
if ($0 ~ awkvar2) {
        triggered=1;
         }
if (triggered) {
         print;
         if ($0 ~ awkvar3) {
              triggered=0;
              print "\n-----------------------------------------------\n"
         }
}
} 

Edit

Modified per request to print the line before "id=22"

{
  awkvar2="id=22";
  awkvar3="end"; 
  if ($0 ~ awkvar2) {
          print prev;
          triggered=1;
  }
  if (triggered) {
          print;
          if ($0 ~ awkvar3) {
              triggered=0;
              print "\n-----------------------------------------------\n"
          }
  }
  {prev=$0;}
}  

Or, more awkish

BEGIN {awkvar2="id=22";awkvar3="end"}

($0 ~ awkvar2),($0 ~ awkvar3) { if ($0 ~ awkvar2) {print prev;}
                                print; 
                                if ($0 ~ awkvar3) {
                                    print "\n---------------\n"
                                }
                               }
{prev=$0;}
like image 72
Dr. belisarius Avatar answered Oct 02 '22 09:10

Dr. belisarius