Skip to main content
Some versions of awk don't support making the parameter name adjacent to -v
Source Link
jesse_b
  • 41.6k
  • 14
  • 109
  • 163

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city"v city="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it wasn't doing anything useful given that $1 is ACTIVE so adding it makes no sense. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it wasn't doing anything useful given that $1 is ACTIVE so adding it makes no sense. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -v city="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it wasn't doing anything useful given that $1 is ACTIVE so adding it makes no sense. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities
deleted 44 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it was i) wrong,wasn't doing anything useful given that $1 is ACTIVE so adding it makes no sense and ii) the result of SUM is not available outside the while loop. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it was i) wrong, $1 is ACTIVE so adding it makes no sense and ii) the result of SUM is not available outside the while loop. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it wasn't doing anything useful given that $1 is ACTIVE so adding it makes no sense. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities
added 17 characters in body
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it was i) wrong, $1 is ACTIVE so adding it makes no sense and ii) the result of SUM is not available outside the while loop. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it was i) wrong, $1 is ACTIVE so adding it makes no sense and ii) the result of SUM is not available outside the while loop. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE"){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities

You have two basic choices: i) use -v to pass the variable to awk or ii) close the ' around the awk script, use the shell variable and continue the ' again.

  1. Use -v

     while read city
     do
         awk -vcity="$city" -F, '{
                                   if ( $1 == "ACTIVE"  && $2 == city ){print $1}
                                  }' siteDBName >> count
     done < city_name
    
  2. Close the quote

     while read city
     do
         awk  -F, '{
                      if ( $1 == "ACTIVE"  && $2 == "'$city'" ){print $1}
                   }' siteDBName >> count
     done < city_name
    

I removed the SUM= line since it was i) wrong, $1 is ACTIVE so adding it makes no sense and ii) the result of SUM is not available outside the while loop. See my alternate approach below for a way to do this correctly.

Also, note that with this approach, you need to read the siteDBName file multiple times. A more efficient version would be:

$ awk -F, '{
             if(NR==FNR){cities[$1]++;}
             else if($1=="ACTIVE" && $2 in cities ){sum++}
            } END{print sum,"active cities"}' city_name siteDBName 
3 active cities
Source Link
terdon
  • 252.7k
  • 69
  • 481
  • 719
Loading