reading a file inside awk and processing line by line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting reading a file inside awk and processing line by line
# 1  
Old 06-22-2009
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.



Code:
 
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

Code:
 
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.

Code:
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...

Last edited by Anteus; 06-22-2009 at 09:05 AM..
# 2  
Old 06-22-2009
what are you trying to do?
# 3  
Old 06-22-2009
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)..
# 4  
Old 06-22-2009
Quote:
Originally Posted by vidyadhar85
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..

Code:
 
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 ----------

Quote:
Originally Posted by ghostdog74
what are you trying to do?
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

Code:
 
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
# 5  
Old 06-22-2009
Code:
#!/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                
        
    }'

# 6  
Old 06-22-2009
Quote:
Originally Posted by vgersh99
Code:
#!/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 ~ /^[#].*/

$0 is the associative array of the awk. so wont it be containing the ouput of the psg command.?
# 7  
Old 06-22-2009
Quote:
Originally Posted by Anteus
hi thanks
what does this regex mean..?
$0 ~ /^[#].*/
Just as the comment line says:
line starting (^) with '#' followed by any character (.) repeated 0 or more times (*)
Quote:
Originally Posted by Anteus
$0 is the associative array of the awk. so wont it be containing the ouput of the psg command.?
no, because we're inside the 'while' loop reading the file containing your process names to be kill-ed.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

3. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

4. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

5. Shell Programming and Scripting

[awk] line by line processing the same file

Hey, not too good at this, so I only managed a clumsy and SLOW solution to my problem that needs a drastic speed up. Any ideas how I write the following in awk only? Code is supposed to do... For every line read column values $6, $7, $8 and do a calculation with the same column values of every... (6 Replies)
Discussion started by: origamisven
6 Replies

6. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

awk, perl Script for processing a single line text file

I need a script to process a huge single line text file: The sample of the text is: "forward_inline_item": "Inline", "options_region_Australia": "Australia", "server_event_err_msg": "There was an error attempting to save", "Token": "Yes", "family": "Family","pwd_login_tab": "Enter Your... (1 Reply)
Discussion started by: hmsadiq
1 Replies

9. Shell Programming and Scripting

Reading a file line by line and processing for each line

Hi, I am a beginner in shell scripting. I have written the following script, which is supposed to process the while loop for each line in the sid_home.txt file. But I'm getting the 'end of file' unexpected for the last line. The file sid_home.txt gets generated as expected, but the script... (6 Replies)
Discussion started by: sagarparadkar
6 Replies
Login or Register to Ask a Question