Visit Our UNIX and Linux User Community


Explain this AWK script plz


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Explain this AWK script plz
# 1  
Old 08-03-2009
Explain this AWK script plz

Hi frnds,

one my frnds has given resolution for my problem as below. it working great , but i couldnt understand somethings in the script.
Why ++ operator after the function calling. how these each block working. will each run for each input line sequencially or one block for all the lines and next block execeted? plz clarify.

The script is

Code:
 
# ora.sh
 
awk -v Outfile=ora.txt '
/^ORA-/ {
split($0, f, "\"");
Target_schema = f[2];
Target_table = f[4];
Ora_error = $0;
}
 
/Targ Rowid=/ { 
Rejected_count++ ;
Get_detail++;
printf "#%d => %s\n", Rejected_count, Ora_error;
next;
}
 
/^)/ && Get_detail {
print Rejected_count,Values_detail > Outfile;
Values_detail = "";
Get_detail = 0;
next;
}
 
Get_detail {
sub(/\(.*\):/,"=");
Values_detail = Values_detail $0 "|"
}
 
END {
print "Total Lines in Session Log",FILENAME,"is :",NR;
print "Total Nr of Rejected Records Captured in Sesslog is :",Rejected_count;
} 
' ora.log


# 2  
Old 08-03-2009
So, what is the problem u have to resolve ??? It's better to know the requirements (input/output) and the solution than just the solution, right ?
plz clarify ^__^
# 3  
Old 08-03-2009
Quote:
Originally Posted by thanhdat
So, what is the problem u have to resolve ??? It's better to know the requirements (input/output) and the solution than just the solution, right ?
plz clarify ^__^
[Code
Hi thanhdat
Plz look into my actual posting and plz explan me the AWK script that one of our frnd given as a solution.Plzzz

Script Performance problem . urgent frnds - The UNIX and Linux Forums

PLzz

[/Code]
# 4  
Old 08-03-2009
Code:
awk -v Outfile=ora.txt '
. . .
' ora.log

Execute awk script for the input file ora.log.
The variable Outfile is initialized, before executing the awk script, with the output file name

Code:
/^ORA-/ {
   split($0, f, "\"");
   Target_schema = f[2];
   Target_table = f[4];
   Ora_error = $0;
}

Records starting with 'ORA-' are selected..
The Oracle error text is memorized in the variable Ora_error

Code:
/Targ Rowid=/ { 
   Rejected_count++ ;
   Get_detail++;
   printf "#%d => %s\n", Rejected_count, Ora_error;
   next;
}

Records containing 'Targ RowId=' are selected.
The number of rejected records found is incremented (variable Rejected_count).
The following records will contain the columns details ending with the ')' record.
The variable Get_detail, which is set to TRUE (value 1),indicates that we are now reading for columns details.

Code:
/^)/ && Get_detail {
   print Rejected_count,Values_detail > Outfile; 
   Values_detail = "";
   Get_detail = 0;
   next;
}

Records starting with ')'' are selected if we are reading columns details.
There is no more columns details records for this rejected record, so the memorized values of columns are writen in the output file.
The variable Get_detailis set to FALSE (value 1),

Code:
Get_detail {
   sub(/\(.*\):/,"=");
   Values_detail = Values_detail $0 "|"
}

Records are selected if we are reading columns details.
The value of the column is memorized in the [/I]Values_detail[/I] variable.

Code:
END {
   print "Total Lines in Session Log",FILENAME,"is :",NR;
   print "Total Nr of Rejected Records Captured in Sesslog is :",Rejected_count;
}

Display statistics at end of file.


My apologies for my poor English.
Jean-Pierre.
# 5  
Old 08-03-2009
Quote:
Originally Posted by aigles
Code:
awk -v Outfile=ora.txt '
. . .
' ora.log

Execute awk script for the input file ora.log.
The variable Outfile is initialized, before executing the awk script, with the output file name

Code:
/^ORA-/ {
   split($0, f, "\"");
   Target_schema = f[2];
   Target_table = f[4];
   Ora_error = $0;
}

Records starting with 'ORA-' are selected..
The Oracle error text is memorized in the variable Ora_error

Code:
/Targ Rowid=/ { 
   Rejected_count++ ;
   Get_detail++;
   printf "#%d => %s\n", Rejected_count, Ora_error;
   next;
}

Records containing 'Targ RowId=' are selected.
The number of rejected records found is incremented (variable Rejected_count).
The following records will contain the columns details ending with the ')' record.
The variable Get_detail, which is set to TRUE (value 1),indicates that we are now reading for columns details.

Code:
/^)/ && Get_detail {
   print Rejected_count,Values_detail > Outfile; 
   Values_detail = "";
   Get_detail = 0;
   next;
}

Records starting with ')'' are selected if we are reading columns details.
There is no more columns details records for this rejected record, so the memorized values of columns are writen in the output file.
The variable Get_detailis set to FALSE (value 1),

Code:
Get_detail {
   sub(/\(.*\):/,"=");
   Values_detail = Values_detail $0 "|"
}

Records are selected if we are reading columns details.
The value of the column is memorized in the [/i]Values_detail[/i] variable.

Code:
END {
   print "Total Lines in Session Log",FILENAME,"is :",NR;
   print "Total Nr of Rejected Records Captured in Sesslog is :",Rejected_count;
}

Display statistics at end of file.


My apologies for my poor English.
Jean-Pierre.
Hi Thanks for you reply
but tell me what you mean by "column details"?
# 6  
Old 08-03-2009
Quote:
Originally Posted by Gopal_Engg
Hi Thanks for you reply
but tell me what you mean by "column details"?
Means values of columns
Code:
SMA_FEED_RECEIVED_ID (SMA_FEED_RECEIVED_ID:UniChar.40:): "MASTER_MATERIAL_ATTR_222222"
SMA_SOURCE_SYSTEM_INSTANCE (SMA_SOURCE_SYSTEM_INSTANCE:UniChar.40:): "BRE0222202"
SMA_MATERIAL (SMA_MATERIAL:UniChar.18:): "2222B"
SMA_OBJVERS (SMA_OBJVERS:UniChar.1:): "2222A"
SMA_CHANGED (SMA_CHANGED:UniChar.1:): ""
SMA_DIVISION (SMA_DIVISION:UniChar.2:): "02221"
SMA_MATL_GROUP (SMA_MATL_GROUP:UniChar.9:): "2227131"
SMA_MATL_TYPE (SMA_MATL_TYPE:UniChar.4:): "ZSTK"

Jean-Pierre.
# 7  
Old 08-04-2009
Quote:
Originally Posted by Gopal_Engg
Hi Thanks for you reply
but tell me what you mean by "column details"?
Code:
 
Hi frnd,

My main problem is that

We need to identify the keyword Targ Rowid=  and “)” . Then we need to read the block of data (column values ) resides between those search pattern. Ok.

And we hav to loop through the entire log file to pick all required column values. Fine

Now in ur script, you have onse function named “Get_detail” which replace unwanted data with = symbol and make the block of data into single line with | delimited. Fine

When ur calling the function , y hv you mention ++ symbol. (ie., Get_detail++) . whether it is calling the function or just a normal variable declaration. If its declaration whats the use of it. 
What is the use of next; how its helping us to fulfill our requirement here.?

In the 3rd , we have like  /^)/ && Get_detail { . whats the use of Get_detail here. Is it a function calling? If so how its working. And why get_detail=0;


my main question is that,

we want to identify the both seach pattern and get the block of data and the block of data with pipe delimited. And need to loop through the entire log file.

You hv achived it in ur script. That is great. But I want understand how it capture the block of data .how this script loop through the session log for same purpose.

Plz explin the control flow in the script( ie., when ,which line ll get executed ?) explain me line by line. Plz frnd plzz .. even I couldn't sleep well since I couldn't understand the script.

Note: plz don't give overview explaination of the scriopt as you gave earlier. Plz little pit deeper. Line by line I want . I want control flow in the script.Plz dont ignor this post.


Send me the explination to my mail id:   gopal.narayanan@tcs.com


Previous Thread | Next Thread
Test Your Knowledge in Computers #820
Difficulty: Easy
Before CSS, nearly all presentational attributes of HTML documents were contained within the HTML markup.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Explain this awk

found this handy one liner in another thread which is closed, it does what i need but im trying to understand it. it basically matches the field that contains the value v and prints its position awk -F, '{for(i=1;i<=NF;i++)if($i==v)print i}' v=yourfield inputfile my understanding is assign... (3 Replies)
Discussion started by: jack.bauer
3 Replies

2. Shell Programming and Scripting

Plz explain the process of this code....

Hi Unix Gurus, Please explain the processing done by the code mentioned below: for (i=1;i<=59;i++) { sub(/ *$/,"", $i) } newrec_key = $2 new_line = $0 if (empty_oldfile==1) { #newly added record ++num_add print "Added key: ", newrec_key > LogFile #print out... (4 Replies)
Discussion started by: ustechie
4 Replies

3. Shell Programming and Scripting

plz explain the execution flow

CODE file=/tmp/rap54ibs2sap.txt trap "rm $file; exit" 0 1 2 3 15 trap rm $file execution result trap -- 'rm /tmp/rap54ibs2sap.txt; exit' EXIT trap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGHUP trap -- 'rm /tmp/rap54ibs2sap.txt; exit' SIGINT trap -- 'rm /tmp/rap54ibs2sap.txt;... (1 Reply)
Discussion started by: rrd1986
1 Replies

4. Shell Programming and Scripting

explain plz

can u explain this step by step........plz...will it do the same for I in *.tar.gz; do A=`basename $I .tar.gz` mkdir $A cp marking-guide ${A}/$A cd $A gunzip -c ../$I | tar xf - cd.. done thnx __________________ (2 Replies)
Discussion started by: avikal
2 Replies

5. Shell Programming and Scripting

perl doubt plz explain....

hi all i wrote a shell script which uses perl script my code is : >cat filename | while read i >do >perl -e 'require "/home/scripts/abc.pl" ; abc("$i")' >done perl script used will simply check syntax of Cobol programs but it didn't work for me so i asked my colleague he suggested... (1 Reply)
Discussion started by: zedex
1 Replies

6. Shell Programming and Scripting

plz help in writing awk script

hi buddies pls help in this matter i have file like this input file -------------------------- (PARTITION PARTITION_1 VALUES LESS THAN (101, 16383 ) TABLESPACE PART_1 ,PARTITION PARTITION_2 VALUES LESS THAN (101, 32766 ) TABLESPACE PART_2 ,PARTITION PARTITION_3 VALUES LESS THAN (101,... (3 Replies)
Discussion started by: LAKSHMI NARAYAN
3 Replies

7. Shell Programming and Scripting

help in writing awk script, plz urgent

I have a file like this I have to I have input file this , I want to give the out put in the below input file (NARAYANA 1 ENDING AT (100, 16383) ,NARAYANA 2 ENDING AT (100, 32766) ,NARAYANA 3 ENDING AT (100, 49149) ,NARAYANA 4 ENDING AT (100, 65535) ,NARAYANA 5... (8 Replies)
Discussion started by: LAKSHMI NARAYAN
8 Replies

8. UNIX for Dummies Questions & Answers

explain command plz

echo -n "1. Updating Password Policy in OID..." | tee -a $logfile set ldap_v1 = `ldapsearch -b "" -h $oid_host -p $oid_port -D "cn=orcladmin" -w $admin_pwd -s sub "cn=PwdPolicyEntry" dn | head -1` echo "dn:$ldap_v1" > ldap1.out echo "changetype:modify" >> ldap1.out echo... (2 Replies)
Discussion started by: maoro
2 Replies

9. UNIX for Advanced & Expert Users

Can anyone explain plz

HI, Can anyone explain to me how does the following command work - > current_dir=`cd \`/usr/bin/dirname $0\` && pwd` Regards, Ranga (3 Replies)
Discussion started by: r_W213
3 Replies

10. Shell Programming and Scripting

Explain awk

Hi, I found this command in this forum, but, couldnt understand much from it. could any one help me understand that??? the commands are : awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2 awk '{sub(/~ */,x);printf $0(/\|$/?ORS:x)}' awk '{sub(/~ */,x);sub(/\|$/, "|\n")}8'... (4 Replies)
Discussion started by: hitmansilentass
4 Replies

Featured Tech Videos