reading a file inside awk and processing line by line

Hi Sorry to multipost. I am opening the new thread because the earlier threads head was misleading to my current doubt.
and i am stuck.

 
list=`cat /u/Test/programs`;
psg "ServTest" | awk -v listawk=$list '{
        cmd_name=($5 ~ /^[A-Z]/)? $9:$8
        for(pgmname in listawk)
        {
               if (pgmname== cmd_name)
               { kill = 1;}
        }
  }'

this code is not working

tried this inside the awk its compiling but not giving the desired result it goes in an infinite loop

 
while (read "/u/Test/programs" )
{

getline pgmname< "/u/Test/programs"
        if (pgmname == cmd_name)
        { kill=1}
 }

I am a novice in shell.if you can help me in any way.it would be greatful

thanks.

---------- Post updated at 06:05 PM ---------- Previous update was at 05:07 PM ----------

another way i figured out was.

list=`cat /u/Test/programs`;
for line in $list
do 
psg "ServTest" | awk -v pgmname=$line '{
        cmd_name=($5 ~ /^[A-Z]/)? $9:$8
        if (pgmname== cmd_name)
    }' 
done

but here to desired result is not coming...

what are you trying to do?

Really not able to understand the purpose of the program...
if you want to read the filecontent of a file provide the filename to the awk at the end it read it line by line only to access the specific file inside the awk use FILENAME variable inside awk..(see man page)..

i have done what i was trying to do..

 
list=`cat /u/Test/programs`;
for line in $list
do
psg "ServTest" | awk -v pgm_name=$line '{
        cmd_name=($5 ~ /^[A-Z]/)? $9:$8
       gsub(/[[:space:]]*/,"",pgm_name)
       gsub(/[[:space:]]*/,"",cmd_name)

                       if (pgm_name== cmd_name)
               { kill = 1;}     
 #further code is for kill if kill = 1                
        
  }'
done

thankss and regards

---------- Post updated at 06:40 PM ---------- Previous update was at 06:35 PM ----------

i have a file name programs in which there is a list of program names.
i want to kill all those process who are listed on this file and have no tty id

 
list=`cat /u/Test/programs`;
for line in $list
do
psg -t "?" | awk -v pgm_name=$line '{
        cmd_name=($5 ~ /^[A-Z]/)? $9:$8
       gsub(/[[:space:]]*/,"",pgm_name)
       gsub(/[[:space:]]*/,"",cmd_name)

                       if (pgm_name== cmd_name)
               { kill = 1;}     
#further code is for kill if kill = 1                
        
  }'
done

this code is working fine. if you find any better way to do this please tell

regards

#!/bin/ksh

myFile='/u/Test/programs'

psg -t "?" | awk -v myFile="${myFile}" '
    BEGIN {
       while (getline < myFile > 0) {
          # skip comments in the config file
          if ( $0 ~ /^[#].*/ ) continue;
          names[$0]
       } 
       close(myFile);
    }
    {
        cmd_name=($5 ~ /^[A-Z]/)? $9:$8
        if (cmd_name in names) kill = 1  
       #further code is for kill if kill = 1                
        
    }'

hi thanks
what does this regex mean..?
$0 ~ /[1].*/

$0 is the associative array of the awk. so wont it be containing the ouput of the psg command.?


  1. # ↩︎

Just as the comment line says:
line starting (^) with '#' followed by any character (.) repeated 0 or more times (*)

no, because we're inside the 'while' loop reading the file containing your process names to be kill-ed.


  1. # ↩︎