#!/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...
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.
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.
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.
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.
{
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;}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With